闪亮的允许用户选择要显示的列 [英] shiny allowling users to choose which columns to display

查看:70
本文介绍了闪亮的允许用户选择要显示的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想了解Shiny中的数据表功能,我感兴趣的是创建一个可列出数据表所有列并允许用户选择要在数据表上查看的列的井面板或侧面板。

I am dabbling with the datatable feature in shiny and I am interested in creating a wellpanel or a sidepanel that lists all the columns of a datatable and allows users to choose which columns they want to see on the datatable.

现在下面的代码显示玩具数据集的所有列 mtcars

Right now this code below displays all the columns of toy dataset mtcars

library(shiny)

runApp(list(
  ui = basicPage(
    h2('The mtcars data'),
    dataTableOutput('mytable')
  ),
  server = function(input, output) {
    output$mytable = renderDataTable({
      mtcars
    })
  }
))

我有兴趣向用户提供转弯的能力使用复选框打开或关闭这些列

I am interested in providing the users the ability to turn these columns either on or off using a checkbox

  [1] "mpg"  "cyl"  "disp" "hp"   "drat"
  [6] "wt"   "qsec" "vs"   "am"   "gear"
  [11] "carb"

解决这个问题的任何帮助都非常有用。

Any help on addressing this issue is much appriciated. Thanks in advance.

推荐答案

我的示例使用 checkboxGroupInput 选择多个列

My example uses checkboxGroupInput to select multiple columns

library(shiny)

vchoices <- 1:ncol(mtcars)
names(vchoices) <- names(mtcars)

runApp(list(
  ui = basicPage(
    h2('The mtcars data'),
    checkboxGroupInput("columns","Select Columns",choices=vchoices,inline = T),
    dataTableOutput('mytable')


  ),
  server = function(input, output) {

    observeEvent(input$columns,{
      cols <- as.numeric(input$columns)
      if(length(input$columns) == 1){
        df <- data.frame(mtcars[,cols])
        names(df) <- names(mtcars)[cols]
        output$mytable = renderDataTable(df)

      }else{
        output$mytable = renderDataTable(mtcars[,cols])

      }


    })

  }
))

这篇关于闪亮的允许用户选择要显示的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆