This is a follow-up to a previous question.
I now wish to add an action button “Clear” that is only shown when there are actually items selected. Here is my code, where I try to use shinyjs
:
require(shiny)
require(shinyjs)
ui <- fluidPage(
shinyjs::useShinyjs(), # needed for hidden/show
shiny::selectizeInput(
inputId="letters",
label="Restrict to Letters:",
choices=letters,
selected=NULL,
multiple = TRUE,
options=list(
placeholder="(all letters)"
)
),
shinyjs::hidden(actionButton("clear","Clear")) # start hidden
)
server <- function(input, output) {
observeEvent(input$clear, {
updateSelectizeInput(inputId = "letters",choices = "",session)
shinyjs::hide(id="clear")
})
observeEvent(input$letters, { # is not called if the last selected item is removed
message("length(input$letters)=", length(input$letters))
if(length(input$subjects)>0)
shinyjs::show(id="clear")
else # no filter
shinyjs::hide(id="clear")
})
}
shinyApp(ui=ui, server=server)
The code has two problems:
-
shinyjs::show() is not working. It seems that the function IS called, but with no effect.
-
observeEvent(input$letters, …) is not called when I deselect the last item.
Any hint appreciated.