本文介绍了使用闪烁的文本输入和dplyr来过滤数据帧中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图在闪亮的应用程序上使用文本输入窗口小部件来过滤数据框中的行,但是我无法使其工作。 数据集 df1 UI $( titlePanel(Sales trends),titlePanel(People score), sidebarLayout(sidebarPanel( textInput(text,label = h3(Text input),value =Enter text ...), numericInput(obs,Number of观察意见:,3), helpText(注意:虽然数据视图只会显示指定的,的观察次数,总结仍然是基于 mainPanel( h4() b h4(Top people), tableOutput(view)) )) ) 服务器 库(dplyr) df1< -data.frame(Name = c(Carlos,Pete,Carlos 卡洛斯, 卡洛斯, 皮特, 皮特, 皮特, 皮特, 荷马),销售=(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)))})输出$ volume< - renderPrint({ dataset sum(dataset $ Sales)})}) 解决方案正如aosmith所指出的那样,你需要删除用于过滤的引号。第二,你应该使用 == 而不是%在 filter()。第三,您可以在其他情况下使用 switch()(在?switch 上阅读)不需要它。 您的 server.R 应如下所示: 库(闪亮)库(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< --active({ df1%> ;%filter(Name == input $ text)%>%arrange(desc(Sales))})输出$ volume< - renderPrint({ dataset< - datasetInput ()数据集$ Sales })}) I am trying to use the text input widget on a shiny app to filter rows in a data frame but I can’t get it working. Datasetdf1<-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"))))UIshinyUI(fluidPage(titlePanel("Sales trends"),titlePanel("People score"),sidebarLayout(sidebarPanel( textInput("text", label = h3("Text input"), value = "Enter text..."), numericInput("obs", "Number of observations to view:", 3), helpText("Note: while the data view will show only the specified", "number of observations, the summary will still be based", "on the full dataset."), submitButton("Update View")),mainPanel( h4("Volume: Total sales"), verbatimTextOutput("volume"), h4("Top people"), tableOutput("view")))))Serverlibrary(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)})}) 解决方案 As aosmith pointed out, you need to remove the quotes for filtering. Second, you should use == instead of %in% inside of filter(). Third, you would use switch() in other cases (read up on ?switch), but here you don't need it.Your server.R should look like this: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来过滤数据帧中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 00:11