From e970aeca1a4a7c6c3ccf3c46355259341e4e0251 Mon Sep 17 00:00:00 2001 From: Robert Link Date: Thu, 21 Sep 2017 08:50:22 -0400 Subject: [PATCH] Move iiasafy functions to output.R --- DESCRIPTION | 2 +- R/mcl.R | 54 ---------------------------------- R/output.R | 56 ++++++++++++++++++++++++++++++++++++ man/iiasa_sortcols.Rd | 2 +- man/iiasafy.Rd | 2 +- man/proc_var_iiasa.Rd | 2 +- tests/testthat/test_output.R | 34 ++++++++++++++++++++++ 7 files changed, 94 insertions(+), 58 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 4dfcc61..deb6f4a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: iamrpt Title: Convert GCAM results to the format required by various IAM experiment databases -Version: 0.1.0-9000 +Version: 0.2.0 Authors@R: c( person("Robert", "Link", email = "robert.link@pnnl.gov", role = c("aut", "cre")), person("Xavier", "Gutierrez", email = "xavier.gutierrez@pnnl.gov", role = c("aut")) diff --git a/R/mcl.R b/R/mcl.R index 2a51ccf..9d8e58a 100644 --- a/R/mcl.R +++ b/R/mcl.R @@ -375,57 +375,3 @@ validate1 <- function(ctl, ctlname, expectcols, rqdcols) { } } -#' Convert a list of tables to a single table in IIASA format -#' -#' The result of this transformation will be a single table with the following -#' columns: -#' -#' \itemize{ -#' \item{Model} -#' \item{Scenario} -#' \item{Region} -#' \item{Variable (taken from the output name of the input)} -#' \item{Unit} -#' \item{NNNN - one for each year} -#' } -#' -#' @param datalist List of data frames, one for each variable. -#' @keywords internal -iiasafy <- function(datalist) -{ - varlist <- lapply(datalist, proc_var_iiasa) - - varlist <- lapply(names(varlist), # Add variable name (need access to names(varlist) for this.) - function(var) { - dplyr::mutate(varlist[[var]], Variable=var) - }) %>% - dplyr::bind_rows() # Combine into a single table -} - - -#' Select the columns needed for the IIASA format -#' -#' Starting with data in long format, keep only the columns needed to form the -#' IIASA format, namely, scenario, region, year, value, and Units. Then rename -#' variables according to the IIASA conventions, and spread to wide format. We don't -#' add the model or variable names at this point, however. -#' @keywords internal -proc_var_iiasa <- function(df) -{ - scenario <- region <- variable <- year <- value <- Units <- NULL # silence - # check notes - df <- df %>% - dplyr::select(scenario, region, year, value, Units) %>% - dplyr::rename(Scenario=scenario, Region=region, Unit=Units) %>% - tidyr::spread(year, value) -} - -#' Put columns in canonical order for IIASA data format -#' -#' @param df Data frame -#' @keywords internal -iiasa_sortcols <- function(df) -{ - cols <- unique(c('Model', 'Scenario', 'Region', 'Variable', 'Unit', names(df))) - dplyr::select(df, dplyr::one_of(cols)) -} diff --git a/R/output.R b/R/output.R index 54407b4..194b1e5 100644 --- a/R/output.R +++ b/R/output.R @@ -124,3 +124,59 @@ nameparse <- function(name) c(stringr::str_c(splt[1:(len-1)], collapse='.'), splt[len]) } } + + +#' Convert a list of tables to a single table in IIASA format +#' +#' The result of this transformation will be a single table with the following +#' columns: +#' +#' \itemize{ +#' \item{Model} +#' \item{Scenario} +#' \item{Region} +#' \item{Variable (taken from the output name of the input)} +#' \item{Unit} +#' \item{NNNN - one for each year} +#' } +#' +#' @param datalist List of data frames, one for each variable. +#' @keywords internal +iiasafy <- function(datalist) +{ + varlist <- lapply(datalist, proc_var_iiasa) + + varlist <- lapply(names(varlist), # Add variable name (need access to names(varlist) for this.) + function(var) { + dplyr::mutate(varlist[[var]], Variable=var) + }) %>% + dplyr::bind_rows() # Combine into a single table +} + + +#' Select the columns needed for the IIASA format +#' +#' Starting with data in long format, keep only the columns needed to form the +#' IIASA format, namely, scenario, region, year, value, and Units. Then rename +#' variables according to the IIASA conventions, and spread to wide format. We don't +#' add the model or variable names at this point, however. +#' @keywords internal +proc_var_iiasa <- function(df) +{ + scenario <- region <- year <- value <- Units <- NULL # silence + # check notes + df <- df %>% + dplyr::select(scenario, region, year, value, Units) %>% + dplyr::rename(Scenario=scenario, Region=region, Unit=Units) %>% + tidyr::spread(year, value) +} + +#' Put columns in canonical order for IIASA data format +#' +#' @param df Data frame +#' @keywords internal +iiasa_sortcols <- function(df) +{ + cols <- unique(c('Model', 'Scenario', 'Region', 'Variable', 'Unit', names(df))) + dplyr::select(df, dplyr::one_of(cols)) +} diff --git a/man/iiasa_sortcols.Rd b/man/iiasa_sortcols.Rd index 58cce57..de9b992 100644 --- a/man/iiasa_sortcols.Rd +++ b/man/iiasa_sortcols.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/mcl.R +% Please edit documentation in R/output.R \name{iiasa_sortcols} \alias{iiasa_sortcols} \title{Put columns in canonical order for IIASA data format} diff --git a/man/iiasafy.Rd b/man/iiasafy.Rd index 0c809ac..8adc482 100644 --- a/man/iiasafy.Rd +++ b/man/iiasafy.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/mcl.R +% Please edit documentation in R/output.R \name{iiasafy} \alias{iiasafy} \title{Convert a list of tables to a single table in IIASA format} diff --git a/man/proc_var_iiasa.Rd b/man/proc_var_iiasa.Rd index 2bb487d..07f0202 100644 --- a/man/proc_var_iiasa.Rd +++ b/man/proc_var_iiasa.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/mcl.R +% Please edit documentation in R/output.R \name{proc_var_iiasa} \alias{proc_var_iiasa} \title{Select the columns needed for the IIASA format} diff --git a/tests/testthat/test_output.R b/tests/testthat/test_output.R index 132f1b8..335f2d6 100644 --- a/tests/testthat/test_output.R +++ b/tests/testthat/test_output.R @@ -189,3 +189,37 @@ test_that('output_csv works for single tab mode.', 'billion m^2,Reference,Africa_Northern,resid,resid,resid_building,2075,5.3585') }) + + +test_that('Single table can be converted to iiasa format.', { + pop <- dplyr::filter(popq, year >= 2005, year <= 2020) + popiia <- proc_var_iiasa(pop) %>% + dplyr:::mutate(Variable='Population', Model='GCAM') %>% + iiasa_sortcols() + + expect_equal(nrow(popiia), length(unique(pop$region))) + expect_identical(names(popiia), c('Model', 'Scenario', 'Region', 'Variable', + 'Unit', as.character(seq(2005,2020,5)))) + expect_equal(popiia[['2010']], dplyr::filter(pop, year==2010)[['value']]) +}) + +test_that('List of tables can be converted to iiasa format.', { + pop <- dplyr::filter(popq, year >= 2005, year <= 2020) + flrspc <- + dplyr::filter(flrspcq, year >= 2005, year <= 2020) %>% + aggregate('sum', 'scenario, region') + + allvar <- list(Population=pop, Floorspace=flrspc) + + iitbl <- iiasafy(allvar) %>% + dplyr::mutate(Model='GCAM') %>% + iiasa_sortcols() + + expect_true(is.data.frame(iitbl)) + expect_equal(nrow(iitbl), 2*length(unique(pop$region))) + expect_identical(names(iitbl), c('Model', 'Scenario', 'Region', 'Variable', + 'Unit', as.character(seq(2005,2020,5)))) + + expect_identical(unique(iitbl$Variable), c('Population', 'Floorspace')) + +})