R Shiny ggvis对动态输入没有反应

时间:2016-06-10 12:04:03

标签: r dynamic shiny ggvis

我尝试用ggvis barcharts构建一个闪亮的仪表板。这个想法是显示一年中特定月份的十大成本的条形图。用户可以选择年份和月份。月份的选择取决于所选年份,因此如果选择2016年,则只能选择迄今为止的月份。

不知怎的,我无法将动态输入字段的值传递给条形图。虽然只要两个输入字段都是静态的,它就可以工作(在服务器脚本的第一行中用输入$ monthtest交换输入$ month1会显示它应该是什么样子)。我已经尝试了从isolate()输入月到使用observe()而不是reactive()的所有想法。

返回的错误是:警告:eval中的错误:长度不正确(0),期望:12;即某种方式,动态字段中的值不会传递给反应环境

非常感谢提前!

这是一个" minimal"工作示例足够接近我的实际仪表板:

#Minimal example Shiny dynamic field
#ggvis Barchart depending on static and dynamic dropdown

library("zoo")
library("shiny")
library("ggvis")
library("magrittr")
library("dplyr")
library("lubridate")

#data-------------
#in the actual code months are in German
month.loc <-  c("January","February","March","April","Mai", "June",
            "July","August","September","October","November", "December")

#generate data
Databc1M<-data.frame("date"=seq(as.Date("2015-01-01"), 
as.Date("2016-03-01"), by="months"), "cost1"=runif(15,min=0,max=100),
     "cost2"=runif(15,min=0,max=100), "cost3"=runif(15,min=0,max=100), 
     "cost4"=runif(15,min=0,max=100),
     "cost5"=runif(15,min=0,max=100), "cost6"=runif(15,min=0,max=100),
     "cost7"=runif(15,min=0,max=100),
     "cost8"=runif(15,min=0,max=100), "cost9"=runif(15,min=0,max=100),
     "cost10"=runif(15,min=0,max=100))

#ui-------------
ui <- fluidPage(
 ggvisOutput(plot_id = "Barcha_Abw"),
  selectInput(inputId = "Year1", label = h3("Choose a year"),
           choices = c(2015,2016), selected=2015
),
#Rendering plot works with static input field
selectInput(inputId = "monthtest", label = h3("Choose a month"),
           choices = month.loc[1:3], selected=month.loc[1]
),
uiOutput("bc1month")

)

#server-------------
server <- function(input, output, session) {
 output$bc1month <- renderUI({
 #filter available months in a given year
 outmonths<- Databc1M %>% dplyr::filter(format(Databc1M$date,"%Y")== 
 as.character(input$Year1)) %>% 
   .[[1]] %>% month() %>% month.loc[.]

 #dynamic dropdown; latest month selected
 selectInput(inputId = 'month1', label = h3("Choose a month
  (dynamic drop down)"), choices = outmonths, 
  #choose latest month in dataset
  selected= Databc1M[NROW(Databc1M),"date"] %>% month() %>%
  month.loc[.]) 
 })

#structure data and render barchart
TopAbw1 <- reactive({

 Dataaux <- Databc1M %>%
  dplyr::filter(format(Databc1M$date,"%Y")==input$Year1)

 #!!!!!! not working: match(input$month1,...); input$monthtest works
 Dataaux<-
 dplyr::filter(Dataaux,month(Dataaux$date)==match(input$month1,
 month.loc)) #input$month1
 Dataaux <- gather(Dataaux,key="cost_id",value="Abweichung")
 Dataaux <- Dataaux[-1,]

 Dataaux <- Dataaux[order(Dataaux$Abweichung,decreasing=TRUE),] %>%
  head(10)

 Dataaux$cost_id <- factor(Dataaux$cost_id, levels = Dataaux$cost_id)

 #render barchart 
 Dataaux %>% 
  ggvis(y=~abs(Abweichung),x=~cost_id) %>%
   layer_text(text:=~round(abs(Abweichung),2)) %>%
    layer_bars( stack =FALSE) %>%
     add_axis("x", title = "", properties = axis_props(labels =
      list(angle= 45, align = "left", fontSize = 12))) %>%
       add_axis("y", title = "Mio. EUR") 
})

TopAbw1 %>% bind_shiny("Barcha_Abw")

}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

我有一个德语版的问题。出于文档目的(如果有人对答案感到好奇:

#Minimal example Shiny dynamic field
#ggis Barchart soll nach Nutzereingabe in 2 Dropdowns neu gerendert werden
#Ein Dropdown ist dynamisch

library("zoo")
library("shiny")
library("ggvis")
library("magrittr")
library("dplyr")
library("lubridate")

#data-------------
month.loc <-  c("Januar","Februar","März","April","Mai", "Juni",
                "Juli","August","September","Oktober","November", "Dezember")

currdate <- as.Date("2015-01-01",format="%Y-%m-%d")

Databc1M<-data.frame("date"=seq(as.Date("2015-01-01"), as.Date("2016-03-01"), by="months"), "cost1"=runif(15,min=0,max=100),
                     "cost2"=runif(15,min=0,max=100), "cost3"=runif(15,min=0,max=100), "cost4"=runif(15,min=0,max=100),
                     "cost5"=runif(15,min=0,max=100), "cost6"=runif(15,min=0,max=100), "cost7"=runif(15,min=0,max=100),
                     "cost8"=runif(15,min=0,max=100), "cost9"=runif(15,min=0,max=100), "cost10"=runif(15,min=0,max=100))




#ui-------------
ui <- fluidPage(
  ggvisOutput(plot_id = "Barcha_Abw"),
  selectInput(inputId = "Year1", label = h3("Wählen Sie ein Jahr"),
              choices = c(2015,2016), selected=2015
  ),
  #Test mit fixem dropdown funktioniert der Plot
  uiOutput("bc1month"),
  plotOutput("TopAbw1")

)
#server-------------
server <- function(input, output, session) {

  output$bc1month <- renderUI({

    outmonths<- Databc1M %>% dplyr::filter(format(Databc1M$date,"%Y")== input$Year1) %>% 
      .[[1]] %>% month() %>% month.loc[.]

    #input$Year1
    selectInput(inputId = 'month1', label = h3("Wählen Sie einen Monat (dynamisch)"), choices = outmonths) 

  })
  TopAbw1 <- reactive({

    Dataaux <- Databc1M[which(year(Databc1M$date) %in% input$Year1),]
    Dataaux <- Dataaux[which(months(Dataaux$date) %in% input$month1),]
    Dataaux <- gather(Dataaux,key="cost_id",value="Abweichung")
    Dataaux <- Dataaux[-1,]

    Dataaux <- Dataaux[order(Dataaux$Abweichung,decreasing=TRUE),] %>%
      head(10)

    Dataaux$cost_id <- factor(Dataaux$cost_id, levels = Dataaux$cost_id)

    Dataaux %>% 
      ggvis(y=~abs(Abweichung),x=~cost_id) #%>%
      #layer_text(text:=~round(abs(Abweichung),2)) %>%
      #layer_bars( stack =FALSE) %>%
      #add_axis("x", title = "", properties = axis_props(labels = list(angle = 45, align = "left", fontSize = 12))) %>%
      #add_axis("y", title = "Mio. EUR") 

  })
  TopAbw1 %>% bind_shiny("Barcha_Abw")

}
shinyApp(ui = ui, server = server)