0

I am trying to build a Shiny app where a user can select multiple forms for data entry. One or more forms are displayed based on user input, and each form consists of a certain number of sliders within a certain number of boxes. Different combinations of input will result in different forms with differing numbers of boxes and sliders, and within a single form the boxes may have differing numbers of sliders. The forms also have different question text, but I have not represented this in the example. I would like to dynamically add these to simplify coding. I have seen answers which use lapply to add sliders or boxes but not both.

My attempt:

## app.R ##
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(periscope)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      menuItem("tab1", tabName = "tab1")
    )),
  dashboardBody(
    tabItems( 
      
      tabItem(tabName = "tab1",
              fluidRow(
                box(selectizeInput("input1", "input1",
                                   choices  = c("a", "b", "c")),
                    
                    box(checkboxGroupInput("input2", "input2",
                                           choices  = c("1", "2", "3")))),
                fluidRow(
                  conditionalPanel(condition="input.input1.includes('a') && input.input2 == '1'", 
                                   style = "display: none;",
                                   box(title= "Main", width = 12, collapsible=TRUE,
                                       tabsetPanel(
                                         tabPanel("first",
                                                  uiOutput("boxes1")),
                                         tabPanel("second")
                                       )
                                   )
                  )
                )
                
              )
      )
    )
  )
)





server <- function(input, output) {
  
  
  na <- 4
  
  n1 <- 3
  

  output$boxes1 <- renderUI({
    type <- input$input1
    nboxes <- eval(parse(text=paste0("n",type)))
    nslide <- eval(parse(text=paste0("n",1)))
    lapply(1:nboxes, function(a) {
      box(title = "test",
          lapply(1:nslide, function(i) {
            sliderInput(inputId = paste0("box",a,"slide",i), min=0, max=10, value=5)
          }))
    })
  })
  
  
}

shinyApp(ui, server)

Above is a reproducible example, with my feeble attempt to accomplish this. Ideally I'd be able to simplify as much as possible, including the addition of all forms with the single uiOutput call, but here I've chosen only to dynamically add boxes and nested sliders, which are used within an enclosing box within conditionalPanels for each form possibility. (This is a skill issue. Any suggestions for how to do this are also appreciated! Would modularisation help?)

Additionally the way I've done it, I would need multiple output items to account for different selections of input2 (since multiple can be selected, again a skill issue). If the above were to work I should get something that looks like this:

## app.R ##
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(periscope)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      menuItem("tab1", tabName = "tab1")
    )),
  dashboardBody(
    tabItems( 
      
      tabItem(tabName = "tab1",
              fluidRow(
                box(selectizeInput("input1", "input1",
                                   choices  = c("a", "b", "c")),
                    
                    box(checkboxGroupInput("input2", "input2",
                                           choices  = c("1", "2", "3")))),
                fluidRow(
                  conditionalPanel(condition="input.input1.includes('a') && input.input2 == '1'", 
                                   style = "display: none;",
                                   box(title= "Main", width = 12, collapsible=TRUE,
                                       tabsetPanel(
                                         tabPanel("first",
                                                  box("test",
                                                      sliderInput(inputId = paste0("box1slide1"), label="test", min=0, max=10, value=5),
                                                      sliderInput(inputId = paste0("box1slide2"), label="test",min=0, max=10, value=5),
                                                      sliderInput(inputId = paste0("box1slide3"), label="test",min=0, max=10, value=5)),
                                                  box("test",
                                                      sliderInput(inputId = paste0("box2slide1"), label="test",min=0, max=10, value=5),
                                                      sliderInput(inputId = paste0("box2slide2"), label="test",min=0, max=10, value=5),
                                                      sliderInput(inputId = paste0("box2slide3"), label="test",min=0, max=10, value=5)),
                                                  box("test",
                                                      sliderInput(inputId = paste0("box3slide1"), label="test",min=0, max=10, value=5),
                                                      sliderInput(inputId = paste0("box3slide2"), label="test",min=0, max=10, value=5),
                                                      sliderInput(inputId = paste0("box3slide3"), label="test",min=0, max=10, value=5)),
                                                  box("test",
                                                      sliderInput(inputId = paste0("box4slide1"), label="test",min=0, max=10, value=5),
                                                      sliderInput(inputId = paste0("box4slide2"), label="test",min=0, max=10, value=5),
                                                      sliderInput(inputId = paste0("box4slide3"), label="test",min=0, max=10, value=5))),
                                         tabPanel("second")
                                       )
                                   )
                  )
                )
                
              )
      )
    )
  )
)




server <- function(input, output) {
  
  na <- 4
  
  n1 <- 3
  
}

shinyApp(ui, server)

Please let me know how could this work for me, or if I should bin this entire approach to this. Thanks so much for any help!

0 Answers0