R Shiny - 使用 Selectinput 作为列选择来子集数据框 [英] R Shiny - Using Selectinput as column selection to subset data frame

查看:68
本文介绍了R Shiny - 使用 Selectinput 作为列选择来子集数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用接收用户 txt 文件的 Shiny.文件的组成完全取决于用户——文件没有设置标题.

I am using Shiny that takes in a user's txt file. The files composition is completely up to the user - there are no set headers for the file.

data_vals <- reactive({
    file1 <- input$file1
    if (is.null(file1))return(NULL)
    read.table(fill=TRUE,file=input$file1$datapath, header=TRUE, colClasses = 
    "factor")})

从这个文件,我从用户提交的头文件创建一个下拉列表:

From this file, I create a drop down list from the header file that the user submitted:

observe({
  req(input$file1)
  dsnames <- names(data_labels())
  cb_options <- list()
  cb_options[dsnames] <- dsnames
  output$choose_filt1<- renderUI({
   selectInput("filt1", "Filter Level 1", cb_options)
  })
 })

然后,将显示列表中唯一项目的复选框供用户取消选择.

Then, checkboxes of the unique items in the list are shown for the user to de-select.

 observe({
  filt1_data <- data_labels()[,input$filt1]
  filt1_uni <- unique(filt1_data)
  output$inCheckboxGroup1 <- renderUI({
   checkboxGroupInput("inCheckboxGroup1", "Filter Level 1 Options:",
                      choices=filt1_uni,
                      selected=filt1_uni)
  })
 })

我想使用某种类型的子集来按列(即 input$filt1)和选中的框(input$inCheckboxGroup1)过滤 data_vals.我不确定如何做到这一点,因为 subset(data_vals, NAME %in% input$inCheckboxGroup1) 正在寻找 NAME 作为列标题,而不是作为用户选择的输入.

I would like to use some type of subsetting to filter data_vals by the column (namely input$filt1) and by the boxes checked (input$inCheckboxGroup1). I am unsure how to do this, as subset(data_vals, NAME %in% input$inCheckboxGroup1) is looking for the NAME as the column header, rather than looking as the input selected by the user.

我曾尝试使用:

subset(data_vals, input$inCheckboxGroup1 %in$ input$inCheckboxGroup1)

但是,这不会生成表格(它是空白的).我还使用了:subset(data_vals, Column1Header %in% input$inCheckboxGroup1),它有效,所以我知道它不是代码的复选框部分.

However, this does not produce a table (it's blank). I have also used: subset(data_vals, Column1Header %in% input$inCheckboxGroup1), which works, so I know it's not the checkbox part of the code.

是否可以按照我描述的方式使用子集,或者是否有其他工具可以使用?

Is it possible to use subsetting in the way I'm describing, or is there another tool to use?

推荐答案

我认为您可以通过不使用 subset 来解决您的问题,因为您的列名是一个变量.请参见下面的示例:

I think you can solve your issue by not using subset, since your column name is a variable. See the example below:

A = 1
B ='am'

# This does not work
mtcars2 = subset(mtcars, B %in% A)

# This works
mtcars2 = mtcars[mtcars[,B] %in% A,]

所以在你的情况下你可以使用:

So in your case you could use:

data_vals = data_vals[data_vals[,input$filt1] %in% input$checkboxGroup1,]

希望这会有所帮助!

这篇关于R Shiny - 使用 Selectinput 作为列选择来子集数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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