简体   繁体   中英

R Shiny: Creating check boxes in a for loop

I would like to create a set of checkboxes that I can selectively show/hide individually using renderUI and uiOutput, and be able to change the value of using updateCheckboxInput even if the checkbox is not currently being displayed.

I tried to do this by creating a list of checkboxes and then individually displaying one of them, but only the last one will display. Here is my code:

server <- function(input, output) {
  v = list()
  for (i in 1:30){
    l = paste0("chk_", i)
    v[[i]] <- renderUI(actionButton(l, l))
  }

  output$chk <- v[[4]]
}

ui <- fluidPage(
  uiOutput("chk")
)

shinyApp(ui = ui, server = server)

It looks like you have renderUI in the wrong place. You want to make one renderUI object which includes a list of 30 checkboxes, but you're instead creating a list of renderUI objects. Try this in the server:

server <- function(input, output) {
  v = list()
  for (i in 1:30){
    l = paste0("chk_", i)
    v[[i]] <- actionButton(l, l)
  }

  output$chk <- renderUI(v)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM