Hi everyone,
I am trying to depend on a checkboxInput to insert and remove a UI. The program basically goes like this; if I check the box, then a file upload input will be displayed, can if I uncheck it, the fileuploadinput will be removed. One slightly complicated feature is it supports multiple checkbox input and whichever of them being checked would trigger the insert file input UI, but there should be only one file input UI on the app the whole time. When nothing is checked, the file input UI will be remove.
Here is the program I wrote; it accomplishes basically everything I described above except that the file input UI would not be removed as expected. Can someone take a look?
## Only run this example in interactive R sessions
if (interactive()) {
# Define UI
ui <- fluidPage(
checkboxInput("indicator1", label = NULL , value = FALSE, width= NULL),
checkboxInput("indicator2", label = NULL , value = FALSE, width= NULL),
checkboxInput("indicator3", label = NULL , value = FALSE, width= NULL)
)
# Server logic
server <- function(input, output, session) {
UI_exist = FALSE
observeEvent({input$indicator1; input$indicator2; input$indicator3}, { # observe all three checkbox inputs
if(!UI_exist && sum(input$indicator1, input$indicator2, input$indicator3) == 1){ # if no UI exists and
#only one checkboxinput is checked
insertUI(
selector = "#indicator1",
where = "beforeBegin",
ui = fileInput("upload", "Upload file", multiple = FALSE),
)
UI_exist <<- TRUE
}
if(UI_exist && all(!input$indicator1, !input$indicator2, !input$indicator3)){ # if UI exists and
# all are false, i.e. no one is checked, then remove UI
removeUI(selector = "div:has(> #upload)")
UI_exist <<- FALSE
}
})
}
# Complete app with UI and server components
shinyApp(ui, server)
}