在 Shiny 中使用 radioButtons 和 selectInput 选择 variables/columns 的问题

problem with selecting variables/columns using radioButtons and selectInput in Shiny

我无法在 Shiny 中同时使用 radioButtons 和 selectInput 函数 select/unselect mtcars 数据集的不同列。 自从过去 2 天以来我一直坚持下去,有人可以帮我解决这个问题吗? 将不胜感激。

此致

data(mtcars)
#Ui
ui <- fluidPage(
sidebarLayout( 
 sidebarPanel(
  column(width = 10,
         radioButtons(inputId="variables", label="Select variables:",
                      choices = c("All","mpg","cyl","disp"),
                      selected = "All", inline = TRUE )),

  column(width = 10,
         selectInput(inputId = "level", label = "Choose Variables to 
                     display", multiple = TRUE, choices =  names(mtcars)[4:11]))),


mainPanel ( 
  h2("mtcars Dashboard"),
  DT::dataTableOutput("table"))))


#server
server<-function(input, output) {

output$table <- DT::renderDataTable(DT::datatable(filter='top', editable = TRUE, caption = 'mtcars',
                                                {  

                                    data <- mtcars
                                    data<-data[,input$variables,drop=FALSE]

                                      column = names(mtcars)
                                      if (!is.null(input$level)) {
                                          column = input$level  }

                                       data

                                                })) }
shinyApp(ui = ui, server = server)
library(shiny)
library(DT)
data(mtcars)
#Ui
ui <- fluidPage(
sidebarLayout( 
  sidebarPanel(
      column(width = 10,
      radioButtons(inputId="variables", label="Select variables:",
      choices = c("All","mpg","cyl","disp"),
      selected = "All", inline = TRUE )),
      column(width = 10,
      selectInput(inputId = "level", label = "Choose Variables to 
      display", multiple = TRUE, choices =  names(mtcars)[4:11]))),
      mainPanel ( 
      h2("mtcars Dashboard"),
      DT::dataTableOutput("table"))
  ))

#server
server<-function(input, output, session) {
  data <- mtcars
  tbl <- reactive({
    if(input$variables=='All'){
      data
    }else{
      data[,c(input$variables,input$level),drop=FALSE]
    }
  })
output$table <- DT::renderDataTable(DT::datatable(filter='top', caption='mtcars', tbl()))  
}
shinyApp(ui = ui, server = server)

以上是我从您的要求中了解到的,希望是您要找的。始终尽量避免在 render*.

内部进行计算