使用selectInput(Shiny)过滤数据框

时间:2017-01-17 19:44:28

标签: r shiny-server

我试图编写一个闪亮的应用程序,它将根据主要组件的选择(通过selectInput)显示子组件的频率。这是我第一次玩弄闪亮,所以我一直在努力学习各种教程。但是,在使用selectInput时,我似乎无法使数据集过滤正确。这是我一直遇到的错误消息:

Warning in c("A", "A", "B", "B") == list(impl = <environment>, readonly = TRUE,  :
longer object length is not a multiple of shorter object length
Warning: Error in eval: object 'SelectComponent' not found

我的代码如下:

DF看起来像:

Component    Subcomponent    Freq
 A            Blue            10
 A            Green           15
 B            Red             20 
 B            Yellow          35

ui.R

library(shiny)

shinyUI(fluidPage(
titlePanel("Components"),
sidebarLayout(
sidebarPanel(
  helpText("Create Component graphs"),

  selectInput("SelectComponent", "Choose a component to display",
              choices = c("A", "B"))

),

# Show a plot of the Subcomponents
mainPanel(
 plotOutput("SubcompPlot")
)
)
))

server.R

Component <- c('A', 'A', 'B', 'B')
Subcomponent <- c('Blue', 'Green', 'Red', 'Yellow')
Freq <- c(10, 15, 20, 25)
ExampleDF<- data.frame(Component, Subcomponent, Freq, stringsAsFactors=FALSE)

library(shiny)
library(ggplot2)
library(dplyr)

Server <- function(input, output) {

  output$SubcompPlot <- renderPlot({

    Filtered <- 
      ExampleDF %>%
      filter(Component == input&SelectComponent)

    ggplot(data=Filtered, aes(x = Filtered$Subcomponent, y = Filtered$Freq)) + 
      geom_bar(stat = "identity")
  })
}

我尝试过其他方法,例如用以下代码替换过滤:

Filtered = reactive(ExampleDF[which(ExampleDF$Component == input$SelectComponent),])

但是返回ggplot错误:

Warning: Error in : ggplot2 doesn't know how to deal with data of class reactive

有关如何使用selectInput值过滤数据的任何建议都将非常感谢!我知道我必须遗漏一些基本的东西,但我无法弄清楚。

由于

0 个答案:

没有答案