Skip to content

Commit

Permalink
Merge branch 'master' of github.com:pik-piam/magclass
Browse files Browse the repository at this point in the history
  • Loading branch information
pfuehrlich-pik committed Jan 10, 2024
2 parents 2ab6ab3 + f4828b7 commit 19fc7d0
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '119866320'
ValidationKey: '120779572'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
11 changes: 7 additions & 4 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
gamstransfer=?ignore
any::lucode2
any::covr
any::madrat
Expand All @@ -31,9 +32,9 @@ jobs:
any::gms
any::goxygen
any::GDPuc
# explicitly installing all piam packages which are also on
# CRAN (madrat, magclass, citation, gms, goxygen, GDPuc) to
# get newest version instad of outdated binary version from CRAN
# 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:
Expand All @@ -56,6 +57,8 @@ jobs:
- name: Test coverage
shell: Rscript {0}
run: covr::codecov(quiet = FALSE)
run: |
nonDummyTests <- setdiff(list.files("./tests/testthat/"), c("test-dummy.R", "_snaps"))
if(length(nonDummyTests) > 0) covr::codecov(quiet = FALSE)
env:
NOT_CRAN: "true"
4 changes: 2 additions & 2 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: v4.4.0
rev: v4.5.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: v0.3.2.9019
rev: v0.3.2.9025
hooks:
- id: parsable-R
- id: deps-in-desc
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: 'magclass: Data Class and Tools for Handling Spatial-Temporal Data'
version: 6.12.0
date-released: '2023-08-17'
version: 6.12.1
date-released: '2024-01-10'
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.12.0
Date: 2023-08-17
Version: 6.12.1
Date: 2024-01-10
Authors@R: c(
person("Jan Philipp", "Dietrich", , "[email protected]", role = c("aut", "cre")),
person("Benjamin Leon", "Bodirsky", , "[email protected]", role = "aut"),
Expand Down
3 changes: 0 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,8 @@ importFrom(methods,setGeneric)
importFrom(methods,setMethod)
importFrom(methods,signature)
importFrom(stats,as.formula)
importFrom(utils,capture.output)
importFrom(utils,head)
importFrom(utils,read.csv)
importFrom(utils,read.table)
importFrom(utils,tail)
importFrom(utils,toBibtex)
importFrom(utils,type.convert)
importFrom(utils,write.table)
20 changes: 13 additions & 7 deletions R/mbind.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

mbind <- function(...) { #nolint
inputs <- list(...)
if (length(inputs) == 1 & is.list(inputs[[1]])) inputs <- inputs[[1]]
if (length(inputs) == 1 && is.list(inputs[[1]])) inputs <- inputs[[1]]
# Remove NULL elements from list
for (i in rev(seq_along(inputs))) {
if (is.null(inputs[[i]])) {
Expand All @@ -43,6 +43,9 @@ mbind <- function(...) { #nolint
if (0 == length(inputs))
return(NULL)

# store total number of elements to ensure that they remain unchanged
nElems <- sum(vapply(inputs, length, integer(1)))

cells <- NULL
elems <- NULL
years <- NULL
Expand All @@ -63,19 +66,19 @@ mbind <- function(...) { #nolint
years <- c(years, getYears(inputs[[i]]))
elems <- c(elems, getNames(inputs[[i]]))
cells <- c(cells, getCells(inputs[[i]]))
if (!diffspat & ncells(inputs[[1]]) > 1) inputs[[i]] <- inputs[[i]][getCells(inputs[[1]]), , ]
if (!difftemp & nyears(inputs[[1]]) > 1) inputs[[i]] <- inputs[[i]][, getYears(inputs[[1]]), ]
if (!diffdata & ndata(inputs[[1]]) > 1) inputs[[i]] <- inputs[[i]][, , getNames(inputs[[1]])]
if (!diffspat && ncells(inputs[[1]]) > 1) inputs[[i]] <- inputs[[i]][getCells(inputs[[1]]), , ]
if (!difftemp && nyears(inputs[[1]]) > 1) inputs[[i]] <- inputs[[i]][, getYears(inputs[[1]]), ]
if (!diffdata && ndata(inputs[[1]]) > 1) inputs[[i]] <- inputs[[i]][, , getNames(inputs[[1]])]
}

if (!(length(grep(".", cells, fixed = TRUE)) %in% c(0, length(cells)))) {
stop("Mixture of regional (no cell numbers) and cellular (with cell numbers)",
" data objects! Cannot handle this case!")
}

if (diffspat & difftemp) stop("Cannot handle objects! Spatial as well as temporal dimensions differ!")
if (difftemp & diffdata) stop("Cannot handle objects! Data as well as temporal dimensions differ!")
if (diffdata & diffspat) stop("Cannot handle objects! Data as well as spatial dimensions differ!")
if (diffspat && difftemp) stop("Cannot handle objects! Spatial as well as temporal dimensions differ!")
if (difftemp && diffdata) stop("Cannot handle objects! Data as well as temporal dimensions differ!")
if (diffdata && diffspat) stop("Cannot handle objects! Data as well as spatial dimensions differ!")
if (difftemp) {
if (length(years) != length(unique(years))) stop("Some years occur more than once!",
" Cannot handle this case!")
Expand All @@ -98,5 +101,8 @@ mbind <- function(...) { #nolint
}
names(dimnames(output)) <- names(dimnames(inputs[[1]]))

if (length(output) != nElems) {
stop("Invalid object (number of values changed during mbind). Does the data contain duplicates?")
}
return(output)
}
11 changes: 11 additions & 0 deletions R/write.report.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ write.report <- function(x, file = NULL, model = NULL, scenario = NULL, unit = N
}
} else {
if (!is.magpie(x)) stop("Input is not a MAgPIE object!")

if (all(is.na(x)) && isTRUE(skipempty)) {
msg <- "magclass object contains only NAs, returning empty data table."
if (!is.null(file)) {
msg <- paste0(msg, " No file was written.")
}
message(msg)
return(data.table::data.table())
}

x <- prepareData(x, model = model, scenario = scenario, unit = unit, skipempty = skipempty,
ndigit = ndigit, extracols = extracols)
if (is.null(file)) return(x)
Expand Down Expand Up @@ -82,6 +92,7 @@ write.report <- function(x, file = NULL, model = NULL, scenario = NULL, unit = N

prepareData <- function(x, model = NULL, scenario = NULL, unit = NULL, skipempty = FALSE,
ndigit = 4, extracols = NULL) {

sep <- "."
# clean data
x <- round(clean_magpie(x, what = "sets"), digits = ndigit)
Expand Down
10 changes: 5 additions & 5 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.12.0**
R package **magclass**, version **6.12.1**

[![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,17 @@ 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 (2023). _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.12.0, <https://github.com/pik-piam/magclass>.
Dietrich J, Bodirsky B, Bonsch M, Humpenoeder F, Bi S, Karstens K, Leip D (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.12.1, <https://github.com/pik-piam/magclass>.

A BibTeX entry for LaTeX users is

```latex
@Manual{,
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},
year = {2023},
note = {R package version 6.12.0},
doi = {10.5281/zenodo.1158580},
year = {2024},
note = {R package version 6.12.1},
url = {https://github.com/pik-piam/magclass},
doi = {10.5281/zenodo.1158580},
}
```
57 changes: 6 additions & 51 deletions man/read.magpie.Rd

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

3 changes: 2 additions & 1 deletion tests/testthat/test-mbind.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

a <- maxample("animal")
p <- maxample("pop")
attr(p, "Metadata") <- NULL
attr(p, "Metadata") <- NULL # nolint: object_name_linter

test_that("mbind works", {
expect_identical(mbind(p, p), p[, , c(1:2, 1:2)])
Expand All @@ -19,6 +19,7 @@ test_that("mbind works", {
expect_error(mbind(p[1:3, 2:4, ], p), "Cannot handle")
expect_error(mbind(p[, 1:3, 1], p), "Cannot handle")
expect_error(mbind(p[1:3, , 1], p), "Cannot handle")
expect_error(suppressWarnings(mbind(p[, , c(1, 1)])), "Invalid object")

for (i in 1:3) {
p0 <- p[1, dim = i]
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-readwritereport.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,13 @@ test_that("read/write report works with braces", {
df <- read.csv(f, sep = ";", stringsAsFactors = FALSE)
expect_identical(df$Unit, "Mt CO2/yr")
})

test_that("write report does not crash with only NAs", {
f <- tempfile()
foo <- new.magpie("DEU", c(2015, 2020),
"Emissions|CO2|Energy|Demand|Transportation (w/ bunkers) (Mt CO2/yr)",
fill = NA)
expect_identical(write.report(foo, f), data.table::data.table())
expect_message(write.report(foo, f),
"magclass object contains only NAs, returning empty data table. No file was written.")
})

0 comments on commit 19fc7d0

Please sign in to comment.