My RShiny app has a selectInput (ID="selectvariable") with choices = "Variable1", "Variable2" and a selectInput (ID= "selectduration") with choices = "daily", "weekly", "monthly"
My goal is to create a ggplot with dates on the x axis and the input of "selectvariable" on the y axis. If the input of "selectduration" == "daily" it should plot the selected variable as it appears in the data frame.
If the input of "selectduration" == "weekly" it should plot THE WEEKLY AVERAGE for each week across the data frame for the selected variable.
If the input of "selectduration" == "monthly" it should plot THE MONTHLY AVERAGE for each month across the data frame for the selected variable.
It is a fairly simple app, however I am stuck on how to create these "weekly" and "monthly" averages and plot them. I do not think that creating additional columns of weekly/monthly averages is an appropriate choice because in reality there are many other variables. I want this to be as automated as possible.
I would love to hear what everyone has to say! Thank you!
Thank you for the suggestion! I have studied this article (it actually helped me answer an unrelated question so that was nice!) however, the question I have in this post remains. Perhaps I was unclear but I am wondering how you can create time-based averages across the data set. I have ran into apply.weekly() but this has been unsuccessful for me so far.
Yes, I think this is very close! However, does produce an error for me currently. Would you mind briefly reviewing this aspect of my applicable code? I have included it below.
'''
output$plot1 <- renderPlot({
df_filtered <- filter(df, dates >= format(input$daterange[1]) & dates <= format(input$daterange[2])) %>%
filter (client == input$clientInput)
#as you can see here, there is also a date range filter and a client filter in this data set (I did not include those in my original post because I did not want to overcomplicate it
if (input$DurationInput == "Daily") {
df_filtered1 <- df_filtered
}
if(input$DurationInput =="Weekly"){
df_filtered1<- df_filtered %>%
as_tsibble(index = dates) %>%
index_by(year_week = ~ yearweek(.)) %>%
summarise(across(where(is.integer), mean, rm.na = TRUE))
}
#Here I will also include a monthly average ( if(input$DurationInput == "monthly") )
#and a 'season' average (an average from August to August each year)
ggplot(df_filtered1, aes(dates, .data[[input$VariableInput]])) +
geom_point(size=3)+
geom_line(color = "Red")+
theme_bw()+
theme(axis.line = element_line(colour = "black"))
})
'''
The error I am currently getting is "Warning: Error in FUN: object 'dates' not found" (I am unsure if there are additional errors behind this one.
you can see from andresrcs example that the dates field gets dropped in favour of the year_week variable that gets made by summarising it. You need to account for that.
Oh right! this makes complete sense. However, that seems to create a difficulty when attempting to dynamically plot these values. Is there an easy way around this difficulty?