From cbf9eb51cacf47429353962de8359c77de10b623 Mon Sep 17 00:00:00 2001 From: orichters Date: Tue, 19 Sep 2023 12:43:31 +0200 Subject: [PATCH 1/3] let as.quitte support lists --- .buildlibrary | 2 +- .github/workflows/check.yaml | 92 ++++++++---------------------------- .pre-commit-config.yaml | 2 +- CITATION.cff | 4 +- DESCRIPTION | 4 +- Makefile | 5 +- NAMESPACE | 1 + R/as.quitte.R | 12 ++++- README.md | 6 +-- man/prepQuitteForScatter.Rd | 1 + 10 files changed, 46 insertions(+), 83 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index a2ba2ec..0ed3adb 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '610998170' +ValidationKey: '612505180' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index fcd7136..7d564a1 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -1,5 +1,3 @@ -# Run CI for R using https://eddelbuettel.github.io/r-ci/ - name: check on: @@ -8,11 +6,6 @@ on: pull_request: branches: [main, master] -env: - USE_BSPM: "true" - _R_CHECK_FORCE_SUGGESTS_: "false" - NO_BINARY_INSTALL_R_PACKAGES: 'c("madrat", "magclass", "citation", "gms", "goxygen", "GDPuc", "roxygen2")' - jobs: check: runs-on: ubuntu-latest @@ -20,80 +13,37 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Bootstrap - run: | - sudo chown runner -R . - sudo locale-gen en_US.UTF-8 - sudo add-apt-repository -y ppa:ubuntugis/ppa - curl -OLs https://eddelbuettel.github.io/r-ci/run.sh - chmod 0755 run.sh - ./run.sh bootstrap - rm -f bspm_*.tar.gz - - - name: Enable r-universe repo, modify bspm integration - run: | - # install packages from https://pik-piam.r-universe.dev and CRAN - echo ' - options(repos = c(universe = "https://pik-piam.r-universe.dev", - CRAN = "https://cloud.r-project.org")) - ' >> .Rprofile - cat .Rprofile - # modify bspm integration to never install binary builds of PIK CRAN packages - sudo sed -i '/bspm::enable()/d' /etc/R/Rprofile.site - # need double % because of printf, %s is replaced with "$NO_BINARY_INSTALL_R_PACKAGES" (see "env:" above) - printf ' - local({ - expr <- quote({ - if (!is.null(repos)) { - noBinaryInstallRPackages <- %s - pkgs <- c(bspm::install_sys(pkgs[!pkgs %%in%% noBinaryInstallRPackages]), - pkgs[pkgs %%in%% noBinaryInstallRPackages]) - } - type <- "source" - }) - trace(utils::install.packages, expr, print = FALSE) - }) - ' "$NO_BINARY_INSTALL_R_PACKAGES" | sudo tee --append /etc/R/Rprofile.site >/dev/null - cat /etc/R/Rprofile.site - - - name: Set up Pandoc - uses: r-lib/actions/setup-pandoc@v2 + - uses: r-lib/actions/setup-pandoc@v2 - - name: Set up Python 3.9 - uses: actions/setup-python@v4 + - uses: r-lib/actions/setup-r@v2 with: - python-version: 3.9 + use-public-rspm: true + extra-repositories: "https://rse.pik-potsdam.de/r/packages" - - name: Cache R libraries - if: ${{ !env.ACT }} # skip when running locally via nektos/act - uses: pat-s/always-upload-cache@v3 + - uses: r-lib/actions/setup-r-dependencies@v2 with: - path: /usr/local/lib/R/ - key: 3-${{ runner.os }}-usr-local-lib-R-${{ hashFiles('DESCRIPTION') }} - restore-keys: | - 3-${{ runner.os }}-usr-local-lib-R- - - - name: Restore R library permissions - run: | - sudo chmod 2777 /usr/local/lib/R /usr/local/lib/R/site-library - - - name: Install dependencies - run: | - ./run.sh install_aptget libhdf5-dev libharfbuzz-dev libfribidi-dev - ./run.sh install_all - ./run.sh install_r_binary covr rstudioapi - ./run.sh install_r lucode2 + extra-packages: | + any::lucode2 + any::covr + any::madrat + any::magclass + any::citation + any::gms + any::goxygen + any::GDPuc + # piam packages also available on CRAN (madrat, magclass, citation, + # gms, goxygen, GDPuc) will usually have an outdated binary version + # available; by using extra-packages we get the newest version + + - uses: actions/setup-python@v4 + with: + python-version: 3.9 - name: Install python dependencies if applicable run: | [ -f requirements.txt ] && python -m pip install --upgrade pip wheel || true [ -f requirements.txt ] && pip install -r requirements.txt || true - - name: Remove bspm integration # to get rid of error when running install.packages - run: | - sudo sed -i '/ trace(utils::install.packages, expr, print = FALSE)/d' /etc/R/Rprofile.site - cat /etc/R/Rprofile.site - - name: Verify validation key shell: Rscript {0} run: lucode2:::validkey(stopIfInvalid = TRUE) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9c3b069..5d2e4ca 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,7 +15,7 @@ repos: - id: mixed-line-ending - repo: https://github.com/lorenzwalthert/precommit - rev: v0.3.2.9013 + rev: v0.3.2.9019 hooks: - id: parsable-R - id: deps-in-desc diff --git a/CITATION.cff b/CITATION.cff index f3ea957..d1a0d8a 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -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: 'quitte: Bits and pieces of code to use with quitte-style data frames' -version: 0.3121.0 -date-released: '2023-08-08' +version: 0.3122.0 +date-released: '2023-09-19' abstract: A collection of functions for easily dealing with quitte-style data frames, doing multi-model comparisons and plots. authors: diff --git a/DESCRIPTION b/DESCRIPTION index 5d4a330..9829ceb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: quitte Title: Bits and pieces of code to use with quitte-style data frames -Version: 0.3121.0 -Date: 2023-08-08 +Version: 0.3122.0 +Date: 2023-09-19 Authors@R: c( person("Michaja", "Pehl", , "pehl@pik-potsdam.de", role = c("aut", "cre")), person("Nico", "Bauer", , "nicolasb@pik-potsdam.de", role = "aut"), diff --git a/Makefile b/Makefile index c1e0c68..38e857f 100644 --- a/Makefile +++ b/Makefile @@ -11,8 +11,9 @@ HELP_PARSING = 'm <- readLines("Makefile");\ help: ## Show this help. @Rscript -e $(HELP_PARSING) -build: ## Build the package using lucode2::buildLibrary(). - Rscript -e 'lucode2::buildLibrary()' +build: ## Build the package using lucode2::buildLibrary(). You can pass the + ## updateType with 'make build u=3' + Rscript -e 'lucode2::buildLibrary(updateType = "$(u)")' check: ## Build documentation and vignettes, run testthat tests, ## and check if code etiquette is followed using lucode2::check(). diff --git a/NAMESPACE b/NAMESPACE index bc96f61..b0e41f6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ S3method(as.quitte,character) S3method(as.quitte,data.frame) +S3method(as.quitte,list) S3method(as.quitte,magpie) S3method(as.quitte,quitte) export(ISOyear) diff --git a/R/as.quitte.R b/R/as.quitte.R index 16e43a9..65d48fa 100644 --- a/R/as.quitte.R +++ b/R/as.quitte.R @@ -228,7 +228,17 @@ as.quitte.magpie <- function(x, periodClass = "integer", addNA = FALSE, na.rm = class(x) <- c("quitte", class(x)) return(x) - } +} + +#' @method as.quitte list +#' @export +as.quitte.list <- function(x, periodClass = "integer", addNA = FALSE, na.rm = FALSE) { # nolint + out <- NULL + for (xi in x) { + out <- rbind(out, as.quitte(xi, periodClass = periodClass, addNA = addNA, na.rm = na.rm)) + } + return(out) +} qaddNA <- function(x) { for (col in colnames(x)) { diff --git a/README.md b/README.md index 34f1271..109b2d7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Bits and pieces of code to use with quitte-style data frames -R package **quitte**, version **0.3121.0** +R package **quitte**, version **0.3122.0** [![CRAN status](https://www.r-pkg.org/badges/version/quitte)](https://cran.r-project.org/package=quitte) [![R build status](https://github.com/pik-piam/quitte/workflows/check/badge.svg)](https://github.com/pik-piam/quitte/actions) [![codecov](https://codecov.io/gh/pik-piam/quitte/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/quitte) [![r-universe](https://pik-piam.r-universe.dev/badges/quitte)](https://pik-piam.r-universe.dev/builds) @@ -47,7 +47,7 @@ In case of questions / problems please contact Michaja Pehl . +Pehl M, Bauer N, Hilaire J, Levesque A, Luderer G, Schultes A, Dietrich J, Richters O (2023). _quitte: Bits and pieces of code to use with quitte-style data frames_. R package version 0.3122.0, . A BibTeX entry for LaTeX users is @@ -56,7 +56,7 @@ A BibTeX entry for LaTeX users is title = {quitte: Bits and pieces of code to use with quitte-style data frames}, author = {Michaja Pehl and Nico Bauer and Jérôme Hilaire and Antoine Levesque and Gunnar Luderer and Anselm Schultes and Jan Philipp Dietrich and Oliver Richters}, year = {2023}, - note = {R package version 0.3121.0}, + note = {R package version 0.3122.0}, url = {https://github.com/pik-piam/quitte}, } ``` diff --git a/man/prepQuitteForScatter.Rd b/man/prepQuitteForScatter.Rd index 6e97150..77ee071 100644 --- a/man/prepQuitteForScatter.Rd +++ b/man/prepQuitteForScatter.Rd @@ -27,6 +27,7 @@ needs to be replaced by None. \dontrun{ quitteOut <- prepQuitteForScatter(quitteIn, c('Emissions|CO2', 'Price|Carbon')) } + } \author{ Nico Bauer, Anselm Schultes, Jerome Hilaire From 137912a6f83335c111f849c0ec82761719539550 Mon Sep 17 00:00:00 2001 From: orichters Date: Tue, 19 Sep 2023 15:15:23 +0200 Subject: [PATCH 2/3] add as.quitte.list tests --- tests/testthat/test-as.quitte.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/testthat/test-as.quitte.R b/tests/testthat/test-as.quitte.R index b425d35..ed78450 100644 --- a/tests/testthat/test-as.quitte.R +++ b/tests/testthat/test-as.quitte.R @@ -74,3 +74,15 @@ test_that( as.quitte(semicolonfile)) } ) + +test_that( + 'as.quitte understands lists()', + { + expect_identical(quitte_example_data, as.quitte(list(list(quitte_example_data)))) + miffile <- tempfile(fileext = ".mif") + writeLines(c("Model,Scenario,Region,Variable,Unit,2005,2010", + "REMIND,Base,World,FE,EJ/yr,12,14"), + con = miffile, sep = "\n") + expect_identical(as.quitte(miffile), as.quitte(list(miffile))) + } +) From 53fa753404e719d6eb405b817db9abad2ab2c790 Mon Sep 17 00:00:00 2001 From: orichters Date: Tue, 19 Sep 2023 15:16:37 +0200 Subject: [PATCH 3/3] add further man files created by buildLibrary --- man/df.as.list.Rd | 35 +++++++++++++++++++++++++++++++++ man/strtrimcommon.Rd | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 man/df.as.list.Rd create mode 100644 man/strtrimcommon.Rd diff --git a/man/df.as.list.Rd b/man/df.as.list.Rd new file mode 100644 index 0000000..3c76317 --- /dev/null +++ b/man/df.as.list.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/df.as.list.R +\name{df.as.list} +\alias{df.as.list} +\title{Data Frame as List} +\usage{ +df.as.list(df, names = 1, x = 2) +} +\arguments{ +\item{df}{A data frame.} + +\item{names}{Index used for naming list items. Integer or character, must +work with \code{df[[names]]}. Defaults to the first data frame column.} + +\item{x}{Index used for list items. Integer or character, must work with +\code{df[[x]]}. Defaults to the second data frame column.} +} +\value{ +A list. +} +\description{ +Data Frame as List +} +\examples{ +(df <- data.frame( + modules = c('power', 'macro', 'welfare', 'PE_FE_parameters', + 'initialCap', 'aerosols'), + `*` = c('IntC', 'singleSectorGr', 'utilitarian', 'iea2014', 'on', + 'exoGAINS'), + check.names = FALSE +)) + +df.as.list(df, 'modules', '*') + +} diff --git a/man/strtrimcommon.Rd b/man/strtrimcommon.Rd new file mode 100644 index 0000000..269c4ef --- /dev/null +++ b/man/strtrimcommon.Rd @@ -0,0 +1,46 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/strtrimcommon.R +\name{strtrimcommon} +\alias{strtrimcommon} +\title{Trim common portions from both sides of a vector of strings} +\usage{ +strtrimcommon(x, split = "", USE.NAMES = FALSE, return.all = FALSE) +} +\arguments{ +\item{x}{A vector of strings} + +\item{split}{A \code{\link{character}} to use for splitting. If \code{split} is empty (i.e. +\code{split = ''}), \code{x} is split into single characters. Otherwise, \code{x} is +split on \code{split} boundaries.} + +\item{USE.NAMES}{logical; if \code{TRUE} use \code{x} as \code{\link{names}} for the result.} + +\item{return.all}{logical; if \code{FALSE} (the default), returns only the striped +strings. If \code{TRUE}, returns a list with elements \code{left}, \code{strings}, +\code{right}, containing left and right common portions, and the trimmed +strings, respectively.} +} +\value{ +A (named) vector of strings, or a list of string vectors (see +parameter \code{return.all} for details). +} +\description{ +Trim common portions from both sides of a vector of strings +} +\examples{ +x <- c('/tmp/remind2_test-convGDX2MIF_fulldata.gdx', + '/tmp/remind2_test-Ariadne_fulldata.gdx', + '/tmp/remind2_test-NAVIGATE_fulldata.gdx', + '/tmp/remind2_test-NGFS_fulldata_oneRegi.gdx', + '/tmp/remind2_test-SHAPE_fulldata.gdx') + +strtrimcommon(x, USE.NAMES = TRUE) + +x <- c('Some|name|with|common|text|elements', + 'Some|name|without|extra|text|elements') + +strtrimcommon(x, split = '|', return.all = TRUE) +} +\author{ +Michaja Pehl +}