I have this simple shiny app with two action buttons. The "Render" button renders a slider input control while the "Remove" button removes the same slider input. This works fine the first time but trying to rerender the same slider input the second time does not work. Any idea or explanation what may be wrong with the code?
This is the code:
ui <- fluidPage(
actionButton("render", "Render"),
actionButton("remove", "Remove"),
uiOutput("moreControls")
)
server <- function(input, output) {
observeEvent(input$render, {
if (input$render > 0) {
output$moreControls <- renderUI({
tagList(
sliderInput("n", "N", 1, 1000, 500)
)
})
}
})
observeEvent(input$remove, {
if (input$remove > 0) {
removeUI(
selector = "div[id='moreControls']"
)
}
})
}
shinyApp(ui, server)
You can set the control to NULL to remove it. See code.
library(shiny)
ui <- fluidPage(
actionButton("render", "Render"),
actionButton("remove", "Remove"),
uiOutput("moreControls")
)
server <- function(input, output) {
rv <- reactiveValues()
observeEvent(input$render, { rv$action <- 'render' })
observeEvent(input$remove, { rv$action <- 'remove' })
output$moreControls <- renderUI({
if (is.null(rv$action)) {return(NULL)}
if(rv$action == 'render'){
sliderInput(inputId = "n",label = "N", min = 1, max = 1000, value = 500)
} else {
return(NULL)
}
})
}
shinyApp(ui, server)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With