filter() 函数在使用 selectinput(multiple) 过滤行时出现问题
filter() function having issues in filtering rows using selectinput(multiple)
以下代码从 'plant' 列过滤具有特定名称的 CO2 行。但是我一直无法做到这一点。当我使用 selectinput 时,我没有得到所有的行。例如在下图中,我选择了很多类型的植物,但只显示了几行。
library(shiny)
library(tidyverse)
ui <- fluidPage((fluidRow(column(6,
selectInput('co2',choices=CO2[1],multiple = T),
tableOutput('table'))
)
)
)
server <- function(input, output, session) {
output$table <-renderTable(
filter(CO2,Plant==input$co2))
}
shinyApp(ui, server)
我使您的示例可重现。这能解决您的问题吗?
library(shiny)
library(tidyverse)
CO2 <- data.frame(Plant=c("id1","id2","id3"),Info=c("plant1","plant2","plant3"))
ui <- fluidPage((fluidRow(column(6,
selectInput('co2',"selecting",choices=CO2$Plant,multiple = T),
tableOutput('table')))))
server <- function(input, output, session) {
output$table <-renderTable(
if(!length(input$co2)==0){
CO2 %>% dplyr::filter(Plant %in% input$co2)
} else CO2
)
}
shinyApp(ui, server)
我认为错误是使用运算符 ==
而不是 %in%
,这使得您的过滤器的行为很奇怪。
以下代码从 'plant' 列过滤具有特定名称的 CO2 行。但是我一直无法做到这一点。当我使用 selectinput 时,我没有得到所有的行。例如在下图中,我选择了很多类型的植物,但只显示了几行。
library(shiny)
library(tidyverse)
ui <- fluidPage((fluidRow(column(6,
selectInput('co2',choices=CO2[1],multiple = T),
tableOutput('table'))
)
)
)
server <- function(input, output, session) {
output$table <-renderTable(
filter(CO2,Plant==input$co2))
}
shinyApp(ui, server)
我使您的示例可重现。这能解决您的问题吗?
library(shiny)
library(tidyverse)
CO2 <- data.frame(Plant=c("id1","id2","id3"),Info=c("plant1","plant2","plant3"))
ui <- fluidPage((fluidRow(column(6,
selectInput('co2',"selecting",choices=CO2$Plant,multiple = T),
tableOutput('table')))))
server <- function(input, output, session) {
output$table <-renderTable(
if(!length(input$co2)==0){
CO2 %>% dplyr::filter(Plant %in% input$co2)
} else CO2
)
}
shinyApp(ui, server)
我认为错误是使用运算符 ==
而不是 %in%
,这使得您的过滤器的行为很奇怪。