我有一个 Shiny 的应用程序如下:
我的目标是能够根据 selectInput ui 功能从 Type 列中过滤多个值。当我做selectInput(inputId, label, choices, multiple = TRUE)
,过滤不起作用,因为它期待 1 个响应。
下面是工作版本,但我不知道如何利用 SelectInput 中的多个功能。
我目前正在使用
test1<-filter(test, `Type`==input$file4)
当我将此扩展添加到
multiple=TRUE
时,这不起作用服务器
library(shiny)
library(readr)
library(dplyr)
library(DT)
actor <- c('Matt Damon','George Clooney','Brad Pitt', 'Clive Owen', 'Morgan Freeman', 'Edward Norton', 'Adrian Granier')
category<-c('action', 'action', 'noir', 'action', 'thriller', 'noir', 'action')
movie <- c('Oceans Eleven', 'Oceans Twelve', 'Fight Club', 'Children of Men', 'The Shawshank Redemption', 'American History X', 'Entourage')
movies <- c(21, 23, 26, 12, 90, 14, 1)
cost <- c(210000, 2300000, 260000, 120000, 90000, 140000, 10000)
Type <- c('A','B','C', 'A', 'B', 'C', 'A')
moviedata<-data.frame(actor, category, movie, movies, cost, Type)
shinyServer(function(input,output){
data <- reactive({
file1 <- input$file
if(is.null(file1)){return()}
read_csv(file=file1$datapath)
})
output$sum <- renderDataTable({
if(is.null(data())){return ()}
test<-subset(moviedata, category %in% data()[[1]])
test1<-filter(test, `Type`==input$file4)
test1$`BUDGET`<-input$file5
test1$CHECKING<-ifelse(test1$`BUDGET`>test1$cost,"YES", "NO")
filter(test1, CHECKING=="YES")
})
output$tb <- renderUI({
if(is.null(data()))
h5("Powered by", tags$img(src='optimatic.png'))
else
tabsetPanel(tabPanel("Summary", dataTableOutput("sum")))
})
}
)
用户界面
library(shiny)
shinyUI(fluidPage(
titlePanel("Actor Finder"),
sidebarLayout(
sidebarPanel(
fileInput("file","Upload Category List: Must have category as header"),
selectInput("file4", "Select Type", c("A" = "A",
"B" = "B",
"C" = "C"), selected = "A",
multiple=FALSE),
numericInput("file5", "Choose cost", 1000000000),
tags$hr()),
mainPanel(
uiOutput("tb")
)
)
))
任何帮助都会很棒,谢谢!
最佳答案
过滤多项选择时,您不能使用 ==
,因为有一个选定值列表。您可以使用 %in%
代替:
test1<-filter(test, Type %in% input$file4)
要在 NA 中包含缺失值,您可以向 file4 添加“缺失”选项,然后:
test1<-filter(test, Type %in% input$file4 |
(is.na(Type) & "Missing" %in% input$file4 ))
关于r - 选择输入 : Have Multiple = TRUE and filter based off that,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42261496/