【问题标题】:problem with selecting variables/columns using radioButtons and selectInput in Shiny在 Shiny 中使用 radioButtons 和 selectInput 选择变量/列的问题
【发布时间】:2019-03-28 10:34:42
【问题描述】:

我无法在 Shiny 中同时使用 radioButtons 和 selectInput 函数来选择/取消选择 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)

【问题讨论】:

  • Error in DT::datatable: unused argument (editable = TRUE) 您需要从数据表中删除 editable。您还需要以不同的方式处理All,因为没有名为“All”的列
  • Suliman 能否请您更新我的查询(如果可能)
  • 你打算如何在同一个查询中使用 radioButtons 和 selectInput。
  • 你可以使用其他一些功能,目的是有两个不同的功能来选择/取消选择列
  • 非常感谢,这两个功能可以应用在一张单表上吗?我不想要 2 个单独的表格。请指导。问候

标签: r drop-down-menu datatables shiny dashboard


【解决方案1】:
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* 中进行计算。

【讨论】:

  • 非常感谢,这两个功能可以在一张单表上应用吗?我不想要 2 个单独的表。问候
  • 不客气。这就是我所说的您的计划“场景”是什么,您是否有两组列名,并且您想在从表中选择/取消选择之前将它们组合起来,或者单选按钮将在 selectInput 之前/之后工作。如果用户从 radioButtons 和 selectInput 中选择列名,很快会发生什么。
  • @Sid 一种将radioButtons 和selectInput 组合在一个表中的幼稚方法,您可以在tbl1 反应式中尝试data[,c(input$variables,input$level),drop=FALSE] 并删除tbl2 反应式和输出。
  • 非常感谢苏利曼,你真好。保持联系:)
猜你喜欢
  • 1970-01-01
  • 2019-05-01
  • 2023-04-02
  • 2019-05-22
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
  • 2019-12-19
  • 2017-04-22
相关资源
最近更新 更多