Reputation: 1070
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!
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(V1, 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(V1, 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
Upvotes: 0
Views: 490
Reputation: 19950
Firstly, I believe summarise
is actually from dplyr
. Second, I think you could try passing strings instead of the actual variable. Try the following with the summarise_
function instead.
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)
})
Upvotes: 1