#' This function calculates the Year to date sum for Figure 1 in the report
#' calculates rolling sum depending on the year to date.
#' e.g. year_to_date = "Jun" calculates the cumulative sum Mar to June (2 obsevations as quarterly data).
#' @param data raw data returned from the API call
#' @param year_to_date Last month in quarter: March, June, September, December
#' @return list containing data frame, dates and length of time series
#' @examples
#' \dontrun{year_to_date_sum (data, "Mar")}
year_to_date_sum <- function(data, year_to_date) {
month2num <- list("Mar"= 1, "Jun" = 2, "Sep" = 3 , "Dec" = 4)
roll_apply_param <- month2num[[year_to_date]]
N <- dim(data$df)[1]
df = zoo::rollapply(data$df, roll_apply_param, sum, fill = NA, by.column=TRUE, align="right")
quarter_dates <- format_dates(data)
df_new <- cbind.data.frame(data$dates, quarter_dates, df)
return(list(df_new,N))
}
# Testing function
#temp <- year_to_date_sum(df_struct, "Dec")
#plot_data <- as.data.frame(temp[[1]])
#dates_temp <- temp[[2]]
# Now should be able to filter by year ending quarter
#dplyr::filter(temp[[1]], grepl('Mar', temp[[1]]$quarter_dates))
#data <- year_to_date_sum(test_data, "Dec")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.