基于多个用户输入的闪亮分配数据框

Shiny assign dataframe based on multiple user inputs

我正在尝试根据 Shiny 应用程序中的一些用户输入分配数据帧和列表(稍后用于代码计算)。其中一项作业依赖于两个用户输入——我该怎么做?我试图附上一些可重现的代码...

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)  

我们可以使用 if 语句:

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)