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)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加