2015-11-20 3 views
2

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

Если я выбираю Все марки, я хотел бы получить этот сюжет:

All brands

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

enter Specific brand

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

Не могли бы вы дать идеи, как решить эту проблему?

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

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

Вы сюжетными только «Car» и Brand9 имеют только значения в 'а', почему вы хотите увидеть 3 бара? – Batanichek

+0

Таким образом, легче сравнивать разные бренды, потому что «буквы» всегда находятся в одном и том же виде – AK47

ответ

2

Попробуйте

1) не изменяется ДФОМ в реактивный

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(!"All" %in% input$Brand){ 
    df3 <- df3[df3$Brand %in% input$Brand,] # if I comment this line, I get All brands graph 
    } 
Смежные вопросы