Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R shiny arrangment of horizontal checkboxes

I am new to shiny applications and I cannot figure out how to arrange some of the aspects of it. I currently have the following shiny code (along with the image of the output)

library(shiny)
channels <- c("social","digital","twitter")

ui <- shinyUI(fluidPage(
  titlePanel("List Elements"),
  sidebarLayout(
    sidebarPanel(
      width = 2,
      tableOutput("listTable")
    ),
    mainPanel(
      checkboxGroupInput(inputId="test", label="Test", choices=1:4, inline = TRUE)
    )
  )
))

# server.R

server <- shinyServer(function(input, output){
  output$listTable <- renderTable({
    channels  })

})


shinyApp(ui = ui, server = server)

Original Output

I would like to have multiple lines of check boxes for each word that appears on the left hand side which can vary in number. However I cannot figure out how to do so, I only know how to hard code in a certain number of lines.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Eg For the example above I would like the output to be:

Desired Output

>Solution :

this could be achieved using lapply in combination with renderUI in ther server function and calling UIOutput in the Ui function. See example below:



library(shiny)
channels <- c("social","digital","twitter")

ui <- shinyUI(fluidPage(
  titlePanel("List Elements"),
  sidebarLayout(
    sidebarPanel(
      width = 2,
      tableOutput("listTable")
    ),
    mainPanel(
      uiOutput("CheckBox")
     
    )
  )
))

# server.R

server <- shinyServer(function(input, output){
  output$listTable <- renderTable({
    channels  })

  output$CheckBox <- renderUI({
    lapply(seq_along(channels), function(i) {
        checkboxGroupInput(inputId=paste0("test",i), label=channels[i],choices=1:4, inline = TRUE)
    })
  })
})


shinyApp(ui = ui, server = server)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading