如何使总结动态地响应闪亮的用户输入?

时间:2015-03-24 10:44:36

标签: shiny tidyr

我正在构建一个闪亮的应用程序,在显示图表之前需要进行一些数据操作 当我在汇总功能中对 tidyr package 中的参数进行硬编码时,我的应用效果很好。但是,当我使用被动()功能将其设置为动态时,它不起作用。

下面是我的例子,你知道如何解决这个问题吗?

非常感谢您的帮助和最好的问候!

此作品

  selectedAAA <- reactive({
                 dplot <- mydata %>% 
                    filter(V1 %in% input$aaa, V2 %in% input$bbb) %>% 
                    group_by(date, V1, V2)
                 dplot <-  dplot %>% 
                   summarise(LT = mean(var, na.rm = TRUE))  %>% # here 'var' is the name of one variable in mydata dataset. Works if hardcoded
                   spread(Material_Type, LT)
                 rownames(dplot) <- dplot$date 
                dplot <- select(dplot, -c(V1, date))
                dplot[is.na(dplot)] <- 0
                dplot <- as.data.frame(dplot)
                })

这不起作用

   # reactive feature to make the graph interactive, based on user's input (selectInput whose id is 'LT')

   selectedLT <- reactive({
                  switch(input$LT,
                  "Label 1" = var1,
                  "Label 2" = var2)
                  })


  selectedAAA <- reactive({
                 dplot <- mydata %>% 
                    filter(V1 %in% input$aaa, V2 %in% input$bbb) %>% 
                    group_by(date, V1, V2)
                 dplot <-  dplot %>% 
                   summarise(LT = mean(selectedLT(), na.rm = TRUE))  %>% # here selectedLT() is the user's selected variable in mydata dataset. Does not work
                   spread(Material_Type, LT)
                 rownames(dplot) <- dplot$date 
                dplot <- select(dplot, -c(V1, date))
                dplot[is.na(dplot)] <- 0
                dplot <- as.data.frame(dplot)
                })

我有以下错误消息:

 Warning in mean.default(selectedLT(), na.rm = TRUE) :
   argument is not numeric or logical: returning NA

1 个答案:

答案 0 :(得分:1)

首先,我认为summarise实际上来自dplyr。其次,我认为你可以尝试传递字符串而不是实际的变量。请尝试使用summarise_函数进行以下操作。

  selectedLT <- reactive({
                  switch(input$LT,
                  "Label 1" = "var1",
                  "Label 2" = "var2")
                  })


  selectedAAA <- reactive({
                 dplot <- mydata %>% 
                    filter(V1 %in% input$aaa, V2 %in% input$bbb) %>% 
                    group_by(date, V1, V2)
                 dplot <-  dplot %>% 
                   summarise_(LT = mean(selectedLT(), na.rm = TRUE))  %>% # here selectedLT() is the user's selected variable in mydata dataset. Does not work
                   spread(Material_Type, LT)
                 rownames(dplot) <- dplot$date 
                dplot <- select(dplot, -c(V1, date))
                dplot[is.na(dplot)] <- 0
                dplot <- as.data.frame(dplot)
                })