Блестящий реактивный график ggvis

Я хотел бы сделать Shiny app, который строит графики Cumstom по параметрам, которые я выбираю с помощью пакета ggvis.

Если я выберу Все бренды, я бы хотел получить этот график:

Все бренды

Но когда я выбираю только одну конкретную марку, сюжет должен выглядеть так:

введите конкретный бренд

Я пробовал разные способы, но ни один из них не дал результатов, которых я ожидал.

Подскажите, пожалуйста, как решить эту проблему?

Также я включаю воспроизводимый пример:

library(shiny)
library(shinydashboard)
library(plyr)
library(ggvis)


# Header -----------------------------------------------------------

header <- dashboardHeader(title= "DashBoard")


# Sidebar --------------------------------------------------------------

sm <- sidebarMenu(

  menuItem(
    text="GGVIS",
    tabName="GGVIS",
    icon=icon("eye")
  )  

)

sidebar <- dashboardSidebar(sm)

# Body --------------------------------------------------

body <- dashboardBody(

  # Layout  --------------------------------------------  

  tabItems(


    tabItem(
      tabName="GGVIS",
      fluidPage(

        fluidRow(

          title = "Inputs", status = "warning", width = 2, solidHeader = TRUE, collapsible = TRUE,



          uiOutput("Category"),
          uiOutput("Brand"),
          uiOutput("Values"),
          ggvisOutput("p")






        )
      )
    )
  )
)

# Setup Shiny app UI components -------------------------------------------

ui <- dashboardPage(header, sidebar, body)

# Setup Shiny app back-end components -------------------------------------

server <- function(input, output) {

  set.seed(1992)
  n=101

   Letter <- sample(c("a", "b", "c"), n, replace = TRUE, prob = NULL)
   Category <- sample(c("Car", "Bus", "Bike"), n, replace = TRUE, prob = NULL)
   Brand <- sample("Brand", n, replace = TRUE, prob = NULL)
   Brand <- paste0(Brand, sample(1:14, n, replace = TRUE, prob = NULL))
   USD <- abs(rnorm(n))*100

   df <- data.frame(Letter, Category, Brand, USD)



  # Inputs --------------------------------------

   output$Category <- renderUI({
    selectInput("Category", "Choose category:", 
                choices = c("Car","Bus", "Bike" ))
  })


  output$Brand <- renderUI({

df2 <- df[df$Category %in% input$Category,]

  selectInput("Brand", 
                "Brand:", 
                c("All", unique(as.character(df2$Brand)))) 
  })


  # ----------------------------------------------------------------------------- 


data2 <-  reactive({


  df <- df[df$Category %in% input$Category,]
  df <- df[df$Brand %in% input$Brand,] # if I comment this line, I get All brands graph
  df <- droplevels(df)  

  df <- ddply(df, c("Letter", "Category", "Brand"), summarise, "USD" = sum(USD))

})   


data2%>% group_by(Brand) %>%
  ggvis(x = ~factor(Letter, levels = c("a", "b", "c")), y = ~USD, fill =    ~Brand, fillOpacity := 1) %>%
  layer_bars() %>%
  add_axis("x", title = "Letter") %>% bind_shiny("p")







  # -----------------------------------------------------------------------------


}

# Render Shiny app --------------------------------------------------------

shinyApp(ui, server)

person AK47    schedule 20.11.2015    source источник
comment
Вы наносите на график только «Автомобиль», а «Марка 9» имеет значения только в «а», почему вы хотите видеть 3 столбца?   -  person Batanichek    schedule 20.11.2015
comment
Таким образом легче сравнивать разные бренды, потому что «буквы» всегда находятся в одном и том же положении.   -  person AK47    schedule 23.11.2015


Ответы (1)


Пытаться

1) не менять df на реактивный

    data2 <-  reactive({

    df3=df
      df3 <- df3[df3$Category %in% input$Category,]
      df3 <- df3[df3$Brand %in% input$Brand,] # if I comment this line, I get All brands graph
      df3 <- droplevels(df3)  

      df3<- ddply(df3, c("Letter", "Category", "Brand"), summarise, "USD" = sum(USD))

})  

2) добавить оператор if

    if(!"All" %in% input$Brand){
    df3 <- df3[df3$Brand %in% input$Brand,] # if I comment this line, I get All brands graph
    }
person Batanichek    schedule 20.11.2015