Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use piamPlotComparison for comparing scenarios #19

Merged
merged 7 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '219142'
ValidationKey: '239196'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'reporttransport: Reporting package for edgeTransport'
version: 0.0.11
date-released: '2024-07-18'
version: 0.0.12
date-released: '2024-07-29'
abstract: This package contains edgeTransport-specific routines to report model results.
The main functionality is to generate transport reporting variables in MIF format
from a given edgeTransport model run folder or REMIND input data.
Expand Down
13 changes: 5 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: reporttransport
Title: Reporting package for edgeTransport
Version: 0.0.11
Date: 2024-07-18
Version: 0.0.12
Date: 2024-07-29
Authors@R:
person("Johanna", "Hoppe", , "[email protected]", role = c("aut", "cre"))
Description: This package contains edgeTransport-specific routines to
Expand All @@ -17,17 +17,14 @@ Imports:
data.table,
gdx,
gdxrrw,
piamPlotComparison,
quitte,
remind2,
rlang,
rmarkdown,
rmndt,
utils,
yaml,
ymlthis
utils
Suggests:
knitr,
sf,
testthat
Encoding: UTF-8
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
7 changes: 0 additions & 7 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,8 @@ importFrom(quitte,aggregate_map)
importFrom(quitte,as.quitte)
importFrom(quitte,write.mif)
importFrom(remind2,toolRegionSubsets)
importFrom(rlang,parse_expr)
importFrom(rmarkdown,render)
importFrom(rmndt,approx_dt)
importFrom(rmndt,disaggregate_dt)
importFrom(rmndt,magpie2dt)
importFrom(utils,packageVersion)
importFrom(utils,write.csv)
importFrom(yaml,yaml.load)
importFrom(ymlthis,as_yml)
importFrom(ymlthis,use_rmarkdown)
importFrom(ymlthis,yml_params_code)
importFrom(ymlthis,yml_replace)
164 changes: 18 additions & 146 deletions R/transportCompareScenarios.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#' Render CompareScenarios EDGE Transport
#' Render CompareScenarios for EDGE Transport
#'
#' Renders the *.Rmd-files associated to CompareScenarios EDGE TRansport. In the Rmds,
#' scenario- and historical .mif-files are loaded. Then plots are created from
#' this data. The result may be rendered to PDF or HTML. Alternatively one can
#' choose Rmd as output format and obtain a copy of the *.Rmd-files.
#' A wrapper for piamPlotComparison::compareScenarios
#'
#' @param mifScen \code{character(n)}, optionally named. Paths to scenario mifs.
#' If the vector has names, those are used to refer to the scenarios in the
Expand All @@ -15,149 +12,24 @@
#' and intermediary files are created.
#' @param outputFormat \code{character(1)}, not case-sensitive. \code{"html"},
#' \code{"pdf"}, or \code{"rmd"}.
#' @param ... YAML parameters, see below.
#' @importFrom yaml yaml.load
#' @importFrom rlang parse_expr
#' @importFrom ymlthis yml_params_code yml_replace as_yml use_rmarkdown
#' @importFrom rmarkdown render
#' @return The value returned by \code{\link[rmarkdown:render]{render()}}.
#' @section YAML Parameters:
#' \describe{
#' \item{\code{yearsScen}}{
#' \code{numeric(n)}.
#' Default: \code{c(seq(2005, 2060, 5), seq(2070, 2100, 10))}.
#' Years to show for scenario data.}
#' \item{\code{yearsHist}}{
#' \code{numeric(n)}.
#' Default: \code{c(seq(1990, 2020, 1), seq(2025, 2100, 5))}.
#' Years to show for historical data.}
#' \item{\code{yearsBarPlot}}{
#' \code{numeric(n)}.
#' Default: \code{c(2010, 2030, 2050, 2100)}.
#' Years to show in bar plots of scenario data.}
#' \item{\code{reg}}{
#' \code{NULL} or \code{character(n)}.
#' Default: \code{NULL}.
#' Regions to show. \code{NULL} means all.}
#' \item{\code{modelsHistExclude}}{
#' \code{character(n) or NULL}.
#' Default: \code{c()}.
#' Models in historical data to exclude.}
#' \item{\code{sections}}{
#' \code{character(n)}.
#' Default: \code{"all"}.
#' Names of sections to include. A subset of
#' \code{c("01_energy_demand", "02_energy_services", "03_stock_and_sales", "04_costs_and_shareweight_trends")}
#' or \code{"all"} for all available sections.}
#' \item{\code{userSectionPath}}{
#' \code{NULL} or \code{character(n)}.
#' Default: \code{NULL}.
#' Path to a *.Rmd-file that may be included as additional section.}
#' \item{\code{mainReg}}{
#' \code{character(1)}.
#' Default: \code{"World"}.
#' A region for which larger plots are shown.}
#' \item{\code{figWidth, figHeight}}{
#' \code{numeric(1)}.
#' Default: \code{15} and \code{10}, respectively.
#' Size of plots in inches.}
#' \item{\code{warning}}{
#' \code{logical(1)}.
#' Default: \code{TRUE}.
#' Show warnings in output?}
#' }
#' @author Christof Schoetz, Johanna Hoppe
#' @examples
#' \dontrun{
#' compareScenarios2(
#' mifScen = c("path/to/Base.mif", "path/to/NDC.mif"),
#' mifHist = "path/to/historical.mif",
#' outputFile = "CompareScenarios2Example1",
#' userSectionPath = "path/to/myPlots.Rmd")
#' compareScenarios2(
#' mifScen = c(ScenarioName1 = "path/to/scen1.mif", ScenarioName2 = "path/to/scen2.mif"),
#' mifHist = "path/to/historical.mif",
#' outputFile = "CompareScenarios2Example2",
#' figWidth = 18, figHeight = 10)
#' }
#' @export
transportCompareScenarios <- function(
mifScen, mifHist,
outputDir = getwd(),
outputFile = "CompareScenarios",
outputFormat = "PDF",
...
) {
yamlParams <- c(
list(
mifScen = normalizePath(mifScen, mustWork = TRUE),
mifScenNames = names(mifScen),
mifHist = normalizePath(mifHist, mustWork = TRUE)),
list(...))
mifScen, mifHist,
outputDir = getwd(),
outputFile = "CompareScenarios",
outputFormat = "PDF") {

# convert relative to absolute paths
if ("userSectionPath" %in% names(yamlParams)) {
yamlParams$userSectionPath <- normalizePath(yamlParams$userSectionPath,
mustWork = TRUE)
}

outputFormat <- tolower(outputFormat)
if (outputFormat == "pdf") outputFormat <- "pdf_document"
if (outputFormat == "html") outputFormat <- "html_document"
if (identical(tolower(outputFormat), "rmd")) {
return(.compareScenarios2Rmd(yamlParams, outputDir, outputFile))
}
# copy the template directory from the package to the outputDir because rmarkdown writes to the folder
# containing the template.
templateInOutputDir <- file.path(outputDir, "CompareScenarios", "csEDGET_main.Rmd")
file.copy(system.file("compareScenarios", package = "reporttransport"),
outputDir, recursive = TRUE)
render(
templateInOutputDir,
intermediates_dir = outputDir,
output_dir = outputDir,
output_file = outputFile,
output_format = outputFormat,
params = yamlParams,
envir = new.env())
unlink(file.path(outputDir, "CompareScenarios"), recursive = TRUE)
}

# Copies the CompareScenarios2-Rmds to the specified location and modifies
# their YAML header according to \code{yamlParams}.
.compareScenarios2Rmd <- function(yamlParams, outputDir, outputFile) {
pathMain <- system.file("CompareScenarios/csEDGET_main.Rmd", package = "reporttransport")
linesMain <- readLines(pathMain)
delimiters <- grep("^(---|\\.\\.\\.)\\s*$", linesMain)
headerMain <- linesMain[(delimiters[1]):(delimiters[2])]
yml <- yaml.load(
headerMain,
handlers = list(r = function(x) yml_params_code(!!parse_expr(x))))
baseYaml <- as_yml(yml)
newYamlParams <- baseYaml$params
newYamlParams[names(yamlParams)] <- yamlParams
if (!is.null(names(yamlParams$mifScen))) {
newYamlParams$mifScenNames <- names(yamlParams$mifScen)
}
newYaml <- yml_replace(
baseYaml,
params = newYamlParams,
date = format(Sys.Date()))
pathDir <- file.path(outputDir, paste0(outputFile, "_Rmd"))
if (!dir.exists(pathDir)) dir.create(pathDir)
dirFiles <- dir(
system.file("CompareScenarios", package = "reporttransport"),
full.names = TRUE)
rmdDirFiles <- grep(
dirFiles,
pattern = "csEDGET_main\\.Rmd$",
invert = TRUE, value = TRUE)
file.copy(rmdDirFiles, pathDir)
use_rmarkdown(
newYaml,
path = file.path(pathDir, "cs2_main.Rmd"),
template = system.file(
"CompareScenarios/csEDGET_main.Rmd",
package = "reporttransport"),
include_yaml = FALSE)
piamPlotComparison::compareScenarios(
projectLibrary = "reporttransport",
mifScen = mifScen,
mifHist = mifHist,
outputFormat = outputFormat,
outputFile = outputFile,
sections = "all",
docTitle = "Edge Transport Compare Scenarios",
outputDir = outputDir,
reg = c("OAS", "MEA", "SSA", "LAM", "REF", "CAZ", "CHA", "IND", "JPN", "USA", "NEU", "EUR", "World"),
yearsHist = c(seq(2010, 2020, 1), seq(2025, 2100, 5))
)
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Reporting package for edgeTransport

R package **reporttransport**, version **0.0.11**
R package **reporttransport**, version **0.0.12**

[![CRAN status](https://www.r-pkg.org/badges/version/reporttransport)](https://cran.r-project.org/package=reporttransport) [![R build status](https://github.com/pik-piam/reporttransport/workflows/check/badge.svg)](https://github.com/pik-piam/reporttransport/actions) [![codecov](https://codecov.io/gh/pik-piam/reporttransport/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/reporttransport) [![r-universe](https://pik-piam.r-universe.dev/badges/reporttransport)](https://pik-piam.r-universe.dev/builds)

Expand Down Expand Up @@ -41,7 +41,7 @@ In case of questions / problems please contact Johanna Hoppe <johanna.hoppe@pik-

To cite package **reporttransport** in publications use:

Hoppe J (2024). _reporttransport: Reporting package for edgeTransport_. R package version 0.0.11, <https://github.com/pik-piam/reporttransport>.
Hoppe J (2024). _reporttransport: Reporting package for edgeTransport_. R package version 0.0.12, <https://github.com/pik-piam/reporttransport>.

A BibTeX entry for LaTeX users is

Expand All @@ -50,7 +50,7 @@ A BibTeX entry for LaTeX users is
title = {reporttransport: Reporting package for edgeTransport},
author = {Johanna Hoppe},
year = {2024},
note = {R package version 0.0.11},
note = {R package version 0.0.12},
url = {https://github.com/pik-piam/reporttransport},
}
```
Loading