dimanche 17 mai 2015

If: the condition has length > 1 and only the first element will be used

I'm trying to make a shiny app, with a reactive ui showing a barplot (from ggplot) comparing the selection of the user. The graph should show a comparison of the bundles selected for each zipcode.

The first error is the "the condition has length > 1 and only the first element will be used" which doesn't allow the main comparison that I want to have in the graph. Then, I don't now if inside the ggplot() and the geom_bar() statements are ok in order to obtain the right graph.

I couldn't find this answer in previous posts.. Thank you for your help.

The dataset1 is like that:

    ZIPCODE_2       MAIN_SET_TYPE
1       30      Bundle ID 102 (DTV & HS & DP)
2       54      Double Play (HS & DP)
3       83      Bundle ID 102 (DTV & HS & DP)
4       89      Bundle ID 102 (DTV & HS & DP)
5       86      Bundle ID 102 (DTV & HS & DP)
6       84      Bundle ID 145 (DTV & HS & DP)
7       80      Bundle ID 145 (DTV & HS & DP)
8       32      Double Play (HS & DP)
9       80      Bundle ID 102 (DTV & HS & DP)
10      86      Bundle ID 145 (DTV & HS & DP)
library(shiny)
shinyUI(fluidPage(
                  titlePanel("Customer Development"),
                  sidebarLayout( 
                    sidebarPanel(
                      
                      uiOutput("ui"),
                      dateInput("date", 
                                label = h3("Snapshot Date"), 
                                value = "01-01-2003",format = "dd-mm-yyyy"),
                      tags$p("Input type:"),
                      verbatimTextOutput("input_focus")
                      ),
                    
                  mainPanel(
                      plotOutput("Plot"),
                      h4("Interpretation"),
                      p("This graphic compares the number of bundles activated at the Snapshot date differentiated according to different regions.")
                    )
                  )
         )
)

library(shiny)
library(ggvis)
library(ggplot2)
library(plyr)
#rm(list = ls())

#setwd("~/R")
#dataset1 <- read.csv("minidata.csv",  header=TRUE, sep = ";", na="")

shinyServer(function(input, output) {
  
  
  output$ui <- renderUI({
    
           checkboxGroupInput("focus", 
                              label=h4("Choose your focus"), 
                              choices= c( "Bundle 1"= "'Bundle_1'", 
                                          "Bundle 2"= "'Bundle_2'",
                                          "Double P"= "'Double_Play'"),
                              select="'Bundle_1'"
                              )
  })
    
  
  data <- reactive({
    
    
    if(input$focus=="'Bundle_1'"){
      datafilter<- dataset %>% 
        filter(dataset$MAIN_SET_TYPE %in% "Bundle ID 102 (DTV & HS & DP)")      
    }
    
    if(input$focus=="'Bundle_2'"){  
      datafilter<- dataset %>% 
        filter(dataset$MAIN_SET_TYPE %in% "Bundle ID 145 (DTV & HS & DP)") 
    }
    
    if(input$focus=="'Double_Play'"){
        datafilter<- dataset %>% 
          filter(dataset$MAIN_SET_TYPE %in% "Double Play (HS & DP)")   
    }
    
  })
  
  output$input_focus <- renderText({
    input$focus
  })
  

  output$Plot <- renderPlot({
    label<- c("Bundle 1", "Bundle 2", "Double Play")
    ggplot(data=dataset1) + geom_bar(data=data(), aes_string(input$focus, fill=input$focus)) +
      facet_grid(. ~ ZIPCODE_2)    
  })
  
})

Aucun commentaire:

Enregistrer un commentaire