# dataPrep() --------------------------------------------------------------
#' Filter Query Data to Specified Region and Forcing Target
#'
#' This helper function is used in the barchart() generic before calling the specific barchart methods.
#'
#' @param list.q Comparison Figures Object
#' @param query string, query must be in list.q
#' @param reg string, one of 32 GCAM regions or Global. Defaults to Global
#' @param frc string, one of forcing targets in list.q[[query]]
dataPrep <- function(list.q, query, reg, frc) {
# get query data, subset by region & forcing
dataset <- list.q[[query]] %>%
filter(region == reg) %>%
filter(forcing == frc)
# define ttle, pass to lapply so can be modified by forcing
ttle <- paste0(query, " (", reg, ") (", frc, ")")
return(list(dataset, ttle))
}
# Master.Minus.Development() ----------------------------------------------
#' Calculate Master Minus Development, Include as additional entry under "Branch"
#'
#' This calculation essentially introduces a third key under the "Branch" column. The three
#' keys are "Master", the name of the development branched, and "Master.Minus.Develoment". Note
#' that 'branch' is an attribute added to project data via a mapping file in the transformation
#' functions (see GCAMFigsBase()).
#'
#' Spread is used to separate the "Master" and "Development" values into separate columns,
#' which makes calculating the difference possible.
#'
#' Gather collapses the specified columns into a set of keys stored under branch
#'
#' @param df data.frame, query data
#' @param cols unquoted column names
#' @param query string, name of query
#' @importFrom tidyr spread gather
Master.Minus.Development <- function(df, query) {
# set of unique branches in data.
branches <- unique(df$branch)
# development branch
# for some reason needs to be converted to symbol in order to be unquoted on RHS in mutate() call below
development <- branches[branches != "Master"] %>% as.character() %>% rlang::sym()
# we want to use this as the name of the column we are calculating. this data becomes its own row
# in the final barcharts image
difference <- paste0("Master.Minus.", development)
df2 <- df %>%
spread(branch, value) %>%
mutate(!!difference := Master - !!(development)) %>%
gather(branch, value, Master, !!development, !!difference)
df2
}
# End ---------------------------------------------------------------------
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.