GitLab at IIASA

loaddata.R 1.33 KiB
Newer Older
Xinxin Yang's avatar
Xinxin Yang committed
#' Load capri data and filter subset with conditions
#'
#' @param filename Name of gdx file.
#' @param selregion Charactoer vector of regions, default = "all.
#' @param seldim5 Selection of the elements in the fifth dimension of the CAPRI data cube. By default it is the empty element.
#' @param selcols Selection of columns in the CAPRI data cube.
#' @param selrows Selection of rows in the CAPRI data cube.
#' @param simyear Simulation year to be loaded/filtered.
#' @param scenarioname Name of the scenario.
#' @return selected data frame.
#'
#' @export

#' @examples
capri_data <- function(filename, selregion = "all", seldim5 = "CUR", selcols, selrows, simyear = "2030", scenarioname = "baseline"){
  # load gdx
  library(caprir)
  mySelcapri <- capri_filter(filename, selregion, seldim5, selcols, selrows, simyear, scenarioname)
  new_prod_list <- product_list %>% add_row(code =  c("GRAI", "GRAE"), label = c("Gras and grazings intensive","Gras and grazings extensive"))

  mySelcapri <- mySelcapri %>%
    pivot_wider(names_from = cols, values_from = value, values_fill = 0) %>%
    pivot_longer(-c(region:scenario), names_to = "cols", values_to = "value") %>%
    select(-scenario) %>%
    left_join(new_prod_list, by =c("cols" ="code")) %>%
    distinct(region, rows, cols, value, label, .keep_all = TRUE) # duplicated???

  return(mySelcapri)

}