2016-12-12 11 views
0

checkboxGroupInputの使用方法について質問があります。データフレームから何ヶ月もの日付をフィルタリングできるフィルタを作成したいと考えています。 dplyrのフィルタ関数では、例としてc(1,2,3,4)<を使用する必要があります。これをチェックボックスのGroupInputで生成して、選択した月をggplotに表示できるようにします。checkboxGroupInput dplyrフィルタリングの値

それは次のコードで動作し:

filter(Location == input$locatie & month(Month_StartConnection) %in% c(1,2,3,4)) 

C(1,2,4,5)checkboxGroupInput入力で動的でなければなりません。それは実際のベースに....

を働いている場合、あなたのコードをチェックするために、実際には不可能である

おかげ

## 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) 
+0

それはだから、 'あなたの代わりに、' == 'で、%'で '%を使用する必要がchecboxGroupInput':例えば、'フィルタ(%入力の$ locatieで場所%) '。これは 'checkboxGroupInput'が値のベクトルを返します – SymbolixAU

+0

あなたの入力をありがとうが、私たちはこのコードの答えを探しています: 月(Month_StartConnection)%in%c(入力$ seizoen) –

+0

例えば、 'choices = c(" Alle Maanden "、1,2、" mrt "、...)' – SymbolixAU

答えて

0

あなたはそれなしで、いくつかのサンプルデータをポストする必要があり、すべての最初に

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

はあなたがのみを使用する必要があります:あなたは、私は問題がであると思い示したものを

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

なしc(...) !!

[サンプルコード] それを見てください、私はそれだけのためにcheckboxGroupInput値についてのであれば(私の答えが正しいことを示すためにいくつかのサンプルデータを作成しなければならなかった理由thatsの、あなたのデータを見ていませんdplyrフィルタリング)

## 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) 
+0

あなたの応答に感謝します。 [画像のdashboad](http://i64.tinypic.com/5uolxs.png) 私は次のエラーが表示されます:私はあなたのコードを試しても動作しません。美しさは長さ1またはデータ(1)と同じでなければなりません:x、y、グループ –

+0

は編集を見てください!!エラー:Merge_Charge_Point%>から始まるコードでsmthgが間違っているようです% select (Charge_Point_ID、Location、kWh、Month_StartConnection)%>% 'しかし、データがないと私には言い難い –

+0

Malvina、ありがとうございます。 –

関連する問題