使用闪亮文本输入和dplyr过滤数据框中的行

使用闪亮文本输入和dplyr过滤数据框中的行,r,dplyr,shiny,shinydashboard,R,Dplyr,Shiny,Shinydashboard,我试图在一个闪亮的应用程序上使用文本输入小部件来过滤数据框中的行,但我无法让它工作 数据集 df1<-data.frame (Name=c("Carlos","Pete","Carlos","Carlos","Carlos","Pete","Pete","Pete","Pete","Homer"),Sales=(as.integer(c("3","4","7","6","4","9","1","2","1","9")))) 服务器 library(shiny) library (dply

我试图在一个闪亮的应用程序上使用文本输入小部件来过滤数据框中的行,但我无法让它工作

数据集

df1<-data.frame (Name=c("Carlos","Pete","Carlos","Carlos","Carlos","Pete","Pete","Pete","Pete","Homer"),Sales=(as.integer(c("3","4","7","6","4","9","1","2","1","9"))))
服务器

library(shiny)
library (dplyr)
df1<-data.frame (Name=c("Carlos","Pete","Carlos","Carlos","Carlos","Pete","Pete","Pete","Pete","Homer"),Sales=(as.integer(c("3","4","7","6","4","9","1","2","1","9"))))
shinyServer(function(input, output) {
output$value <- renderPrint({ input$text })
datasetInput <- reactive({
switch(input$dataset,df1%>% filter(Name %in% "input$text")%>% select(Name, Sales)%>% arrange(desc(Sales)))
})
output$volume <- renderPrint({
dataset <- datasetInput()
sum(dataset$Sales)
})})
库(闪亮)
图书馆(dplyr)
df1%安排(描述(销售)))
})

输出$volume正如aosmith所指出的,您需要删除用于过滤的引号。其次,您应该在
filter()
内部使用
==
而不是%
中的
%。第三,您可以在其他情况下使用
switch()
(请阅读
?switch
),但在这里您不需要它

您的
server.R
应该如下所示:

library(shiny)
library(dplyr)
df1 <- data_frame(Name = c("Carlos","Pete","Carlos","Carlos","Carlos","Pete",
                         "Pete","Pete","Pete","Homer"),
                  Sales = c(3, 4, 7, 6, 4, 9, 1, 2, 1, 9))

shinyServer(function(input, output) {
  datasetInput <- reactive({
    df1 %>% filter(Name == input$text) %>% arrange(desc(Sales))
  })
  output$volume <- renderPrint({
    dataset <- datasetInput()
    dataset$Sales
  })
})
库(闪亮)
图书馆(dplyr)
df1%安排(描述(销售))
})

使用自己的搜索词过滤时,输出$volume,
1.使更新功能具有:
反应式

2.在
renderDT

ui = fluidPage(
        textInput('qry', 'Species', value='setosa'),
        DT::DTOutput('tbl'))
server = function(input, output){
        a = reactive({ filter(iris, Species == input$qry })
        output$tbl = renderDT(a())
shinyApp(ui,server)

祝你好运

可能尝试删除
input$text
内部
filter
中的引号?尝试将OP的ui.R与此服务器一起运行。您提供的R只会给我
错误:没有适用于类“function”
错误的对象的“filter”方法。您需要在
server.R
中定义数据。我跳过了这一部分,但我更新了我的答案以反映这一点。谢谢回复!现在我得到了
警告:当应用程序启动时,switch:EXPR中的错误必须是长度为1的向量。@MonicaHeddneck您是否重新启动了r会话并仅加载了这两个包?你能试着追踪错误发生的时间和地点吗?你是如何启动这个应用程序的?
ui = fluidPage(
        textInput('qry', 'Species', value='setosa'),
        DT::DTOutput('tbl'))
server = function(input, output){
        a = reactive({ filter(iris, Species == input$qry })
        output$tbl = renderDT(a())
shinyApp(ui,server)