I am trying to hide and show button based on folder selection.Scenario is if the user selects the folder then Button is shown or else button should be hidden. I and trying to achieve the same using shinyjs package. Here is the piece of code which I have written:
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)
Can you please suggest the error or modification which needs to be made in this piece of code to enable hide and show functionality.
You need to add the useShinyjs() line to set up your shiny app to use shinyjs. The only thing after that is to hide the button by default. And then to show it when the pattern "leaf" is in the character string returned by get_selected(input$tree). Is this what you want ?
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(
useShinyjs(),
"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..."))
shinyjs::hide("submit")
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(!is.null(unlist(get_selected(input$tree)))){
if(length(unlist(strsplit(unlist(get_selected(input$tree)),"leaf")))>1){
shinyjs::show("submit")
}else{
shinyjs::hide("submit")
}
}
})
})
shinyApp(ui, server)