我正在尝试根据文件夹选择隐藏和显示按钮。场景是如果用户选择文件夹,则显示按钮,否则应隐藏按钮。我并尝试使用shinyjs包来实现相同的目标。这是我写的一段代码:
library(shiny)
library(shinyTree)
library(shinyjs)
ui <- shinyUI(
pageWithSidebar(
# Application title
headerPanel("shinyTree with 'selected' input"),
sidebarPanel(
helpText(HTML("An example of using shinyTree's <code>get_selected</code> function to extract the cells which are currently selected.
<hr>Created using <a href = \"http://github.com/trestletech/shinyTree\">shinyTree</a>.")),
actionButton('submit','SUBMIT')
),
mainPanel(
"Currently Selected:",
verbatimTextOutput("selTxt"),
hr(),
shinyTree("tree")
)
))
server <- shinyServer(function(input, output, session) {
log <- c(paste0(Sys.time(), ": Interact with the tree to see the logs here..."))
output$tree <- renderTree({
list(
root1 = structure("123"),
root2 = list(
SubListA = list(leaf1 = "", leaf2 = "", leaf3=""),
SubListB = list(leafA = "", leafB = "")
)
)
})
output$selTxt <- renderText({
tree <- input$tree
if (is.null(tree)){
"None"
} else{
unlist(get_selected(tree))
}})
observe({
if('leaf' %in% get_selected(input$tree)){
shinyjs::show("submit")
}else{
shinyjs::hide("submit")
}
})
})
shinyApp(ui, server)
您能否建议在这段代码中需要进行的错误或修改以启用隐藏和显示功能。