How to make summarise (tidyr) react dynamically to shiny user's input?

21 views
Skip to first unread message

cho7tom

unread,
Mar 24, 2015, 6:55:07 AM3/24/15
to manip...@googlegroups.com
Dear all,

I am building a shiny app which requires some data manipulation prior to display a chart.
My app works nice when I 'hard code' the parameter in the summarise function, from tidyr package. However it does not work when I make it dynamic, using the reactive() feature.

below is my example, do you know how to solve this?

Many thanks in advance for your help and best regards!

Thomas

This works

  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(Apigroup_Cd, date))
                dplot[is.na(dplot)] <- 0
                dplot <- as.data.frame(dplot)
                })

This does not work

   # 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(Apigroup_Cd, date))
                dplot[is.na(dplot)] <- 0
                dplot <- as.data.frame(dplot)
                })

I have the following error message:

 Warning in mean.default(selectedLT(), na.rm = TRUE) :
   argument is not numeric or logical: returning NA
Reply all
Reply to author
Forward
0 new messages