【问题标题】:Allow user to complete a list of columns to display based on checkboxGroupInput允许用户根据 checkboxGroupInput 完成要显示的列列表
【发布时间】:2018-06-12 14:24:17
【问题描述】:

@XiongbingJin 在Stack Overflow 上的示例允许用户首先显示完整的数据集,然后使用 checkboxGroupInput 更改要显示的列。

我想要一些帮助来做一些不同的事情:

我想要什么:

  1. 数据表显示以任意列列表开始(例如:carbwtdrat of mtars datset),而不是完整的数据集。
  2. 用户可以使用 checkboxGroupInput 完成要显示的列表。 (例如:添加vs)。

@XiongbingJin 例子:

 library(shiny)
 runApp(list(
 ui = basicPage(
 selectInput("select", "Select columns to display", names(mtcars), multiple = 
 TRUE),
 h2('The mtcars data'),
  dataTableOutput('mytable')
),
 server = function(input, output) {
 output$mytable = renderDataTable({
  columns = names(mtcars)
  if (!is.null(input$select)) {
    columns = input$select
  }
  mtcars[,columns,drop=FALSE]
 })
 }
))

【问题讨论】:

  • 我相信在您展示的示例中,所有列都显示了,因为输入中的默认项目是names(mtcars)。将其更改为仅包含所需列的向量即可。

标签: r datatable shiny


【解决方案1】:

正如@Marc P 所建议的,您可以通过将names(mtcars) 的子集提供给selected 参数来关注它。这还有一个好处是可以摆脱input$selectnull 的情况。

library(shiny)

ui = basicPage(
  selectInput("select", "Select columns to display", 
              names(mtcars),
              selected = names(mtcars)[c(1, 3)], # display 1st and 3rd variables 
              multiple = TRUE),
  h2('The mtcars data'),
  dataTableOutput('mytable')
)

server = function(input, output) {
  output$mytable = renderDataTable({
    mtcars[, input$select, drop=FALSE]
  })
}

shinyApp(ui, server)

【讨论】:

  • 只是一个问题:我设法在我的真实数据上下文中实现了您的答案,并且当 input$select 为空(索引 = 0 列的空)时遇到错误。
  • 您是否通过闪亮应用加载数据,例如使用fileInput?如果没有,你在代码的哪一部分加载它?
  • 我在传单上下文中工作:点击形状后会显示数据表。
  • 当表格返回给我索引空错误
  • 您是否有任何存储库来托管这部分代码?因为我有点难以推断问题的根源
猜你喜欢
  • 2012-11-13
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
  • 2011-06-20
  • 2019-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多