r - Shiny 的 ggvis react 图

标签 r plot shiny ggvis

我想制作Shiny应用程序,它可以使用ggvis包根据我选择的参数绘制cumstom图。

如果我选择所有品牌,我想得到这个图:

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)

最佳答案

尝试

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
    }

关于r - Shiny 的 ggvis react 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33827316/

相关文章:

R函数胶水与粘贴

r - 添加新的回归线,但保留 R 中先前运行的回归线

python - pylab 和 pyplot 有什么区别?

r - 无功值 R Shiny 的多个输入

r - 如何在Shiny中自动“调整大小” ggplot?

r - 如何基于两列合并多个数据框?

r - 堆叠条形图显示 R 中的反向标签

matrix - 快速可视化一个大的二进制矩阵?

r - 在 R DT 数据表的 lengthMenu(页面长度菜单)中设置值的名称

R : applying function on list of similar dataframes