用于dplyr过滤的checkboxGroupInput值

时间:2016-12-12 10:29:01

标签: r rstudio shiny

我对checkboxGroupInput的使用有疑问。我们想制作一个过滤器,可以从数据帧中过滤掉几个月的日期。在dplyr的过滤函数中,我们必须使用c(1,2,3,4)< - 作为例子。现在我们想用checkboxGroupInput生成它,这样我们就可以在ggplot中显示选定的月份。

它适用于以下代码:

filter(Location  == input$locatie & month(Month_StartConnection) %in% c(1,2,3,4))
使用checkboxGroupInput输入时,

c(1,2,4,5)必须是动态的。

由于

## app.R ##
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Test", tabName = "KPI", icon = icon("dashboard")),
      menuItem("Widgets", tabName = "widgets", icon = icon("th"))
    )
  ),
  dashboardBody(
    tabItems(
      # First tab content
      tabItem(tabName = "KPI",
              fluidRow(
                checkboxGroupInput(inputId = "seizoen", label = "Seizoen", choices = list("Alle Maanden", 1,2,"mrt","apr", "mei", "jun", "jul", "aug", "sep", "okt", "nov", "dec"), value = TRUE),
                selectInput(inputId = "locatie",
                                   label = h3("Locatie"),
                                   choices = c("Speerpunt","Museum","Strandweg", "Zwarte pad", "Prins Clauslaan", "Prinsessegracht", "Fluwelen Burgwal", "Kranestraat", "Heulstraat", "Kneuterdijk", "Hoge Nieuwstraat", "Kijkduin", "De Uithof", "Zwembad het Hofbad", "Kyocera stadion", "Stadhuis"),
                                   selected = 1
                ),
                plotOutput("ggplot", height = 600, width= 600)
              )
      ),

      # Second tab content
      tabItem(tabName = "widgets",
              h2("Widgets tab 1")
      )
    )
  ))

server <- function(input, output) {

  output$ggplot <- renderPlot({

    print(
      Merge_Charge_Point %>%
        select(Charge_Point_ID, Location, kWh, Month_StartConnection) %>%
        filter(Location  == input$locatie & month(Month_StartConnection) %in% c(input$seizoen)) %>%
        group_by(Month_StartConnection) %>%
        summarise(aantal_sessies = n()) %>%
        filter(Month_StartConnection < as.Date(format(as.Date(strptime(Sys.Date(),"%Y-%m-%d",tz="")) ,format = "%y-%m-1")))%>%
        ggplot(aes(Month_StartConnection, aantal_sessies, group = 1))+
        geom_line()+scale_x_date(labels = date_format("%y-%m")) +
        geom_smooth(method = "lm", se = FALSE)
    ) 
  })
} 

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:0)

首先,您应该发布一些示例数据,如果没有它,实际上无法检查您的代码,如果它实际上正在工作....

根据您展示的内容,我认为问题在于:

...%in% c(input$seizoen))...

你应该只使用:

...%in% input$seizoen)...

没有c(...) !!

[示例代码] 看看它,我还没有看到您的数据,这就是为什么我必须创建一些示例数据来向您显示我的答案是正确的(如果它只是关于dplyr过滤的checkboxGroupInput值)

## app.R ##
library(shinydashboard)

x <- c("Alle Maanden", 1,2,"mrt","apr", "mei", "jun", "jul", "aug", "sep", "okt", "nov", "dec")
y <- c(1:13)

data <- data.frame(x,y)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Test", tabName = "KPI", icon = icon("dashboard")),
      menuItem("Widgets", tabName = "widgets", icon = icon("th"))
    )
  ),
  dashboardBody(
    tabItems(
      # First tab content
      tabItem(tabName = "KPI",
              fluidRow(
                checkboxGroupInput(inputId = "seizoen", label = "Seizoen", choices = list("Alle Maanden", 1,2,"mrt","apr", "mei", "jun", "jul", "aug", "sep", "okt", "nov", "dec")),
                selectInput(inputId = "locatie",
                            label = h3("Locatie"),
                            choices = c("Speerpunt","Museum","Strandweg", "Zwarte pad", "Prins Clauslaan", "Prinsessegracht", "Fluwelen Burgwal", "Kranestraat", "Heulstraat", "Kneuterdijk", "Hoge Nieuwstraat", "Kijkduin", "De Uithof", "Zwembad het Hofbad", "Kyocera stadion", "Stadhuis"),
                            selected = 1
                ),
                plotOutput("ggplot", height = 600, width= 600)
              )
      ),

      # Second tab content
      tabItem(tabName = "widgets",
              h2("Widgets tab 1")
      )
    )
  ))

server <- function(input, output) {

  output$ggplot <- renderPlot({
    print(input$seizoen)
    data <- data %>%
        filter(x %in% input$seizoen) 
    print(data)

    g <- ggplot(data,aes(x=x,y=y))+ geom_point()
    g

  })
} 

shinyApp(ui, server)