Search code examples
rshinyassign

Shiny assign dataframe based on multiple user inputs


I am trying to assign dataframes and lists (which are later used in calculations in code) based on some user inputs in a Shiny app. One of the assignments relies on two user inputs - how would I do this? I have tried to attach some reproducible code...

library(shiny)

set.seed(4)

#  lists and dataframes to be assigned based on user inputs
A <- list(rnorm(6), rnorm(6))
B <- list(rnorm(6), rnorm(6))

dfA <- as.data.frame(cbind(rnorm(6), rnorm(6)))
dfA_adj <- as.data.frame(cbind(rnorm(6), rnorm(6)))
dfB <- as.data.frame(cbind(rnorm(6), rnorm(6)))
dfB_adj <- as.data.frame(cbind(rnorm(6), rnorm(6)))



ui <- fluidPage(
  
  titlePanel(strong("Title")),
  
  sidebarLayout(
    
    sidebarPanel(
      
      #content
      h4(strong("Select data sets to use in calculations:")),
      selectInput('L', 'Select list to use', c("List A" = 'a', "List B" = 'b')),
      selectInput('P','Select Calculation method', c("Adjusted" = 'Adj', "Standard" = 'St'))
      
    ),
    
    mainPanel(
      tableOutput("table")
          )
    )
  )



server <- function(input, output) {
  
  #assign dataframes and list to use in code calcs (not shown) based on user inputs
  
  pts <- reactive({ switch(input$L, "a" = A, "b" = B)  })

  PET <- reactive({  switch(c(input$L, input$P),
                            c("a", "Adj") = dfA_adj,
                            c("a", "St") = dfA,
                            c("b", "Adj") = dfB_adj,
                            c("b", "St") = dfB   })
  
  
  output$table <- renderTable(PET())

}


shinyApp(ui = ui, server = server)  

Solution

  • We can use an if statement:

    library(shiny)
    
    set.seed(4)
    
    #  lists and dataframes to be assigned based on user inputs
    A <- list(rnorm(6), rnorm(6))
    B <- list(rnorm(6), rnorm(6))
    
    dfA <- as.data.frame(cbind(rnorm(6), rnorm(6)))
    dfA_adj <- as.data.frame(cbind(rnorm(6), rnorm(6)))
    dfB <- as.data.frame(cbind(rnorm(6), rnorm(6)))
    dfB_adj <- as.data.frame(cbind(rnorm(6), rnorm(6)))
    
    
    
    ui <- fluidPage(
      
      titlePanel(strong("Title")),
      
      sidebarLayout(
        
        sidebarPanel(
          
          #content
          h4(strong("Select data sets to use in calculations:")),
          selectInput('L', 'Select list to use', choices =  c("List A" = 'A', "List B" = 'B')),
          selectInput('P','Select Calculation method', choices =  c("Adjusted", "Standard"))
          
        ),
        
        mainPanel(
          tableOutput("table")
        )
      )
    )
    
    
    
    server <- function(input, output, session) {
      
      
      PET <- reactive({ 
        
        if (input$L == "A" && input$P == "Adjusted") {
          dfA_adj}
        else if (input$L == "A" && input$P == "Standard") {
           dfA 
        } else if (input$L == "B" && input$P == "Adjusted") {
           dfB_adj 
        } else {
           dfB
        }
        
      })
      
        output$table <- renderTable({PET()})
        
    }
    
    
    shinyApp(ui = ui, server = server)