Skip to content

Commit

Permalink
Move iiasafy functions to output.R
Browse files Browse the repository at this point in the history
  • Loading branch information
rplzzz committed Sep 21, 2017
1 parent 3b84351 commit e970aec
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 58 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]", role = c("aut", "cre")),
person("Xavier", "Gutierrez", email = "[email protected]", role = c("aut"))
Expand Down
54 changes: 0 additions & 54 deletions R/mcl.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
56 changes: 56 additions & 0 deletions R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion man/iiasa_sortcols.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/iiasafy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/proc_var_iiasa.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions tests/testthat/test_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -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'))

})

0 comments on commit e970aec

Please sign in to comment.