简体   繁体   English

从R Shiny中的selectInput过滤

[英]Filtering from selectInput in R shiny

I'm trying to have the user upload a .csv file, then take a column (factor) of that .csv file and create user input of to determine which of the unique names in that field will be selected for the data frame. 我试图让用户上载.csv文件,然后对该.csv文件进行列(因子)创建用户输入,以确定将为数据框选择该字段中的哪个唯一名称。

So if I have the following example data.frame: 因此,如果我有以下示例data.frame:

    COURSE   VALUE
1      A       7
2      C       2
3      C       2
4      B       9
...

I'd want to filter using Select_Input and the user can select select say A and C and the data frame would be filtered for just rows with A and C. Below is the code to generate the UI for the select_Input 我想使用Select_Input进行过滤,用户可以选择说A和C,数据框将仅过滤具有A和C的行。以下是为select_Input生成UI的代码

output$choose_course<-renderUI{
 # If missing input, return to avoid error later in function
if(is.null(input$model.input))
 return()

# Get the data set with the appropriate name
course.names <-c("All",as.vector(t(unique(select_(model.data0(),"COURSE")))))

selectInput("courses","Choose courses", choices=course.names, multiple=TRUE)    
}

Note that model.data0() is the reactive data entered by the user via a .csv file. 请注意,model.data0()是用户通过.csv文件输入的反应数据。 This first part of code works ok (but maybe the format is messing up the next stuff?) and displays for the user input selections. 代码的第一部分工作正常(但可能是格式弄乱了下一个内容吗?)并显示给用户输入选择。 And next we have my attempt at filtering from the selectInput... 接下来,我们尝试从selectInput进行过滤...

model.data<-reactive({
if(is.null(input$model.input))
  return()

localdata<-model.data0()
if(input$courses!="All"){
  localdata<-localdata[localdata$COURSE==unlist(input$courses),]
}
})

However, this returns an error of "argument 1 (type 'list') cannot be handled by 'cat' ". 但是,这将返回错误“参数1(类型'列表')无法由'cat'处理”。 I tried the unlist above to change it to a vector but didn't seem to work. 我尝试了上面的unlist将其更改为向量,但似乎没有用。 Any ideas how I can make this filter my data? 有什么想法可以使我过滤数据吗?

You could try doing this: 您可以尝试这样做:

require(shiny)

ui <- fluidPage( 
  sidebarLayout(
    sidebarPanel(
      uiOutput('choose_course')
    ),
    mainPanel(
      tableOutput('courseTable')
    )
  )
)

server <- function(input, output, session) {
  # Build data, would be replaced by the csv loading in your case
  n <- 10
  model.data0 <- reactive ({
    data.frame( "COURSE" = sample(LETTERS[1:3], n, replace=TRUE),
                "VALUE"  = sample(1:10, n, replace=TRUE)) 
  })

  # Render selectInput 
  output$choose_course <- renderUI({
    course.names <- as.vector( unique(model.data0()$COURSE) )
    selectInput("courses","Choose courses", choices=course.names, multiple=TRUE)    
  })

  # Subset so that only the selected rows are in model.data
  model.data <- reactive({
    subset(model.data0(), COURSE %in% input$courses)
  })

  output$courseTable <- renderTable({ model.data() })
}
runApp(shinyApp(ui,server))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM