Skip to content

Commit

Permalink
Merge pull request #179 from orichters/master
Browse files Browse the repository at this point in the history
add unitjoin to undo unitsplit
  • Loading branch information
orichters authored Feb 3, 2025
2 parents f1466de + ac8b659 commit 0d865c0
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 26 deletions.
4 changes: 3 additions & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '123025056'
ValidationKey: '124192984'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand All @@ -7,3 +7,5 @@ AcceptedNotes: unable to verify current time
AutocreateReadme: yes
allowLinterWarnings: no
enforceVersionUpdate: no
AutocreateCITATION: yes
skipCoverage: no
25 changes: 16 additions & 9 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ jobs:
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
any::lucode2
any::covr
any::madrat
any::magclass
any::citation
any::gms
any::goxygen
any::GDPuc
lucode2
covr
madrat
magclass
citation
gms
goxygen
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
Expand All @@ -44,6 +44,13 @@ jobs:
[ -f requirements.txt ] && python -m pip install --upgrade pip wheel || true
[ -f requirements.txt ] && pip install -r requirements.txt || true
- name: Run pre-commit checks
shell: bash
run: |
python -m pip install pre-commit
python -m pip freeze --local
pre-commit run --show-diff-on-failure --color=always --all-files
- name: Verify validation key
shell: Rscript {0}
run: lucode2:::validkey(stopIfInvalid = TRUE)
Expand All @@ -63,6 +70,6 @@ jobs:
shell: Rscript {0}
run: |
nonDummyTests <- setdiff(list.files("./tests/testthat/"), c("test-dummy.R", "_snaps"))
if(length(nonDummyTests) > 0) covr::codecov(quiet = FALSE)
if(length(nonDummyTests) > 0 && !lucode2:::loadBuildLibraryConfig()[["skipCoverage"]]) covr::codecov(quiet = FALSE)
env:
NOT_CRAN: "true"
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exclude: '^tests/testthat/_snaps/.*$'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: 2c9f875913ee60ca25ce70243dc24d5b6415598c # frozen: v4.6.0
rev: cef0300fd0fc4d2a87a85fa2093c6b283ea36f4b # frozen: v5.0.0
hooks:
- id: check-case-conflict
- id: check-json
Expand All @@ -15,7 +15,7 @@ repos:
- id: mixed-line-ending

- repo: https://github.com/lorenzwalthert/precommit
rev: 7910e0323d7213f34275a7a562b9ef0fde8ce1b9 # frozen: v0.4.2
rev: 3b70240796cdccbe1474b0176560281aaded97e6 # frozen: v0.4.3.9003
hooks:
- id: parsable-R
- id: deps-in-desc
Expand All @@ -25,4 +25,4 @@ repos:
- id: readme-rmd-rendered
- id: use-tidy-description
ci:
autoupdate_schedule: quarterly
autoupdate_schedule: weekly
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: 'magclass: Data Class and Tools for Handling Spatial-Temporal Data'
version: 6.17.1
date-released: '2024-08-01'
version: 6.17.2
date-released: '2025-02-03'
abstract: Data class for increased interoperability working with spatial-temporal
data together with corresponding functions and methods (conversions, basic calculations
and basic data manipulation). The class distinguishes between spatial, temporal
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: magclass
Title: Data Class and Tools for Handling Spatial-Temporal Data
Version: 6.17.1
Date: 2024-08-01
Version: 6.17.2
Date: 2025-02-03
Authors@R: c(
person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de",
comment = c(affiliation = "Potsdam Institute for Climate Impact Research", ORCID = "0000-0002-4309-6431"), role = c("aut", "cre")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export(setYears)
export(spatRasterToDataset)
export(suppressSpecificWarnings)
export(time_interpolate)
export(unitjoin)
export(unitsplit)
export(unwrap)
export(where)
Expand Down
37 changes: 37 additions & 0 deletions R/unitjoin.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#' joins a data.frame or vector of strings with variable and unit separated into
#' a data.frame with variable and unit joined as 'variable (unit)'.
#' Use magclass::unitsplit to split them again
#'
#' @param x data.frame or vector of strings
#' @param col column name. Default: variable
#' @param unit vector of strings. If NULL, col 'unit' in x is used
#' @return data.frame or vector of strings, dependent on x
#'
#' @export
unitjoin <- function(x, unit = NULL, col = "variable") {
# check whether is a data.frame
if (is.null(ncol(x))) {
if (length(x) == 0 || is.null(unit)) {
# return empty variable vector or if no unit specified
return(x)
} else {
# paste unit to it, keeping factor type
if (is.factor(x)) {
return(as.factor(paste0(x, " (", unit, ")")))
} else {
return(paste0(x, " (", unit, ")"))
}
}
} else { # is data.frame
# no unit is specified -> take from unit column
stopifnot(col %in% colnames(x))
if (is.null(unit)) {
x[col] <- unitjoin(x[[col]], x[["unit"]])
x["unit"] <- NULL
} else {
# use what is specified
x[col] <- unitjoin(x[[col]], unit)
}
return(x)
}
}
2 changes: 1 addition & 1 deletion R/unitsplit.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ unitsplit <- function(x, col = "variable") {
varName <- sub(pattern, "\\1", x[[col]], perl = TRUE)
unit <- sub(pattern, "\\3", x[[col]], perl = TRUE)
unit[grep(pattern, x[[col]], invert = TRUE, perl = TRUE)] <- "N/A"
tmp <- data.frame(varName, unit, stringsAsFactors = FALSE)
tmp <- data.frame(varName, unit, stringsAsFactors = is.factor(x[[col]]))
names(tmp) <- c(names(x[col]), "unit")
x <- cbind(tmp, x[setdiff(names(x), names(x[col]))])
return(x)
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Data Class and Tools for Handling Spatial-Temporal Data

R package **magclass**, version **6.17.1**
R package **magclass**, version **6.17.2**

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

Expand Down Expand Up @@ -56,17 +56,18 @@ In case of questions / problems please contact Jan Philipp Dietrich <dietrich@pi

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

Dietrich J, Bodirsky B, Bonsch M, Humpenoeder F, Bi S, Karstens K, Leip D, Sauer P (2024). _magclass: Data Class and Tools for Handling Spatial-Temporal Data_. doi:10.5281/zenodo.1158580 <https://doi.org/10.5281/zenodo.1158580>, R package version 6.17.1, <https://github.com/pik-piam/magclass>.
Dietrich J, Bodirsky B, Bonsch M, Humpenoeder F, Bi S, Karstens K, Leip D, Sauer P, Baumstark L, Bertram C, Giannousakis A, Klein D, Neher I, Pehl M, Schultes A, Stevanovic M, Wang X, Beier F, Pflüger M, Richters O (2025). "magclass: Data Class and Tools for Handling Spatial-Temporal Data." doi:10.5281/zenodo.1158580 <https://doi.org/10.5281/zenodo.1158580>, Version: 6.17.2, <https://github.com/pik-piam/magclass>.

A BibTeX entry for LaTeX users is

```latex
@Manual{,
@Misc{,
title = {magclass: Data Class and Tools for Handling Spatial-Temporal Data},
author = {Jan Philipp Dietrich and Benjamin Leon Bodirsky and Markus Bonsch and Florian Humpenoeder and Stephen Bi and Kristine Karstens and Debbora Leip and Pascal Sauer},
year = {2024},
note = {R package version 6.17.1},
url = {https://github.com/pik-piam/magclass},
author = {Jan Philipp Dietrich and Benjamin Leon Bodirsky and Markus Bonsch and Florian Humpenoeder and Stephen Bi and Kristine Karstens and Debbora Leip and Pascal Sauer and Lavinia Baumstark and Christoph Bertram and Anastasis Giannousakis and David Klein and Ina Neher and Michaja Pehl and Anselm Schultes and Miodrag Stevanovic and Xiaoxi Wang and Felicitas Beier and Mika Pflüger and Oliver Richters},
doi = {10.5281/zenodo.1158580},
date = {2025-02-03},
year = {2025},
url = {https://github.com/pik-piam/magclass},
note = {Version: 6.17.2},
}
```
25 changes: 25 additions & 0 deletions man/unitjoin.Rd

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

8 changes: 7 additions & 1 deletion tests/testthat/test-unitsplit.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_that("unitsplit on vectors and strings works", {
test_that("unitsplit and unitjoin on vectors and strings works", {
teststring <- "Emi|CO2|+|Energy (Mt CO2/yr)"
expected <- data.frame(
variable = "Emi|CO2|+|Energy",
Expand All @@ -7,6 +7,7 @@ test_that("unitsplit on vectors and strings works", {
expect_identical(unitsplit(teststring), expected)
expect_identical(unitsplit(teststring, 1), expected)
expect_identical(unitsplit(teststring, "variable"), expected)
expect_equal(unitjoin(expected)$variable, teststring)
testvector <- c("Emi|CO2|+|Energy (Mt CO2/yr)", "Emi|CO|Land Use (Mt CO/yr)")
expected <- data.frame(
variable = c("Emi|CO2|+|Energy", "Emi|CO|Land Use"),
Expand All @@ -15,6 +16,9 @@ test_that("unitsplit on vectors and strings works", {
expect_identical(unitsplit(testvector), expected)
expect_identical(unitsplit(testvector, 1), expected)
expect_identical(unitsplit(testvector, "variable"), expected)
expect_equal(unitjoin(expected)$variable, testvector)
expect_identical(unitjoin(as.factor("V"), as.factor("U")), as.factor("V (U)"))
expect_identical(unitjoin("V", "U"), "V (U)")
})

test_that("simple unitsplit works", {
Expand All @@ -41,6 +45,8 @@ test_that("simple unitsplit works", {
)
expect_identical(unitsplit(df, 4), expected)
expect_identical(unitsplit(df, "Data"), expected)
expect_equal(unitjoin(expected, col = "Data")[colnames(df)], df)
expect_error(unitjoin(expected, col = "doesnotexist"))
})

test_that("unitsplit works with braces", {
Expand Down

0 comments on commit 0d865c0

Please sign in to comment.