A_Skelton73
A_Skelton73

Reputation: 1190

R shiny - Generating n number of UI Elements

Title says it all really, I'm trying to think of a way to do create multiple UI elements.

So if I have n number of elements in a selector (data sets) - Each of these data sets has differently named column names and of different lengths. I'd like these column names as group inputs.

I think I could do this statically, if I knew how many datasets there were going to be (such as the example below) - but is there a way I could generate the checkboxgroupinput iteratively for example?

ui.r

library(shiny)
shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Example"),


  sidebarPanel(
    checkboxGroupInput("one", "One:",data_in[[1]]),
    checkboxGroupInput("two", "Two:",data_in[[2]]),

  ),

  mainPanel(

  )
))

Upvotes: 1

Views: 454

Answers (1)

jdharrison
jdharrison

Reputation: 30425

Yes you can use renderUI

data_in <- c("Cylinders" = "cyl",
             "Transmission" = "am",
             "Gears" = "gear")
library(shiny)
runApp(
  list(ui = pageWithSidebar(
    headerPanel("Example"),
    sidebarPanel(
      uiOutput("checkbGroups")
    ),
    mainPanel(
    )
  )
  ,
  server = function(input, output, session){
    output$checkbGroups <- renderUI({
      lapply(1:10, function(x){
        do.call(checkboxGroupInput, list(inputId = x, label = x, choices = data_in))
      }
      )
    }
    )
  }
  )
)

enter image description here

Upvotes: 3

Related Questions