Skip to content

Commit

Permalink
reordered arguments in "[" and "[<-" methods to make it compatible wi…
Browse files Browse the repository at this point in the history
…th most recent R develop
  • Loading branch information
tscheypidi committed Jun 25, 2023
1 parent de266d0 commit 655348c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '13476573'
ValidationKey: '119151300'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
- id: mixed-line-ending

- repo: https://github.com/lorenzwalthert/precommit
rev: v0.3.2.9007
rev: v0.3.2.9013
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.9.1
date-released: '2023-05-26'
version: 6.10.0
date-released: '2023-06-25'
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.9.1
Date: 2023-05-26
Version: 6.10.0
Date: 2023-06-25
Authors@R: c(
person("Jan Philipp", "Dietrich", , "[email protected]", role = c("aut", "cre")),
person("Benjamin Leon", "Bodirsky", , "[email protected]", role = "aut"),
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down
69 changes: 32 additions & 37 deletions R/magpie-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@

setClass("magpie", contains = "array", prototype = array(0, c(0, 0, 0)))

.mselectDF <- function(x, df) { #nolint
.mselectDF <- function(x, df) { # nolint
if (is.null(names(dimnames(x)))) stop("Dimnames must have names in order to use mselect!")

dims <- dimCode(names(df), x)
Expand All @@ -139,9 +139,9 @@ setClass("magpie", contains = "array", prototype = array(0, c(0, 0, 0)))
" Mixtures across main dimensions are not allowed!")

sdims <- as.integer(substring(dims, 3))
maxdim <- nchar(gsub("[^\\.]", "", names(dimnames(x))[maindim])) + 1 #nolint
maxdim <- nchar(gsub("[^\\.]", "", names(dimnames(x))[maindim])) + 1 # nolint
if (nrow(df) == 1) df[1, ] <- escapeRegex(df[1, ])
if (nrow(df) > 1) df <- data.frame(sapply(df, escapeRegex)) #nolint
if (nrow(df) > 1) df <- data.frame(sapply(df, escapeRegex)) # nolint
dmissing <- which(!(1:maxdim %in% sdims))
sdims <- c(sdims, dmissing)
for (d in dmissing) df <- cbind(df, "[^\\.]*")
Expand Down Expand Up @@ -191,7 +191,7 @@ setClass("magpie", contains = "array", prototype = array(0, c(0, 0, 0)))
return(x)
}

.dimextract <- function(x, i, dim, pmatch = FALSE, invert = FALSE) { #nolint
.dimextract <- function(x, i, dim, pmatch = FALSE, invert = FALSE) { # nolint

if (is.magpie(i) && is.logical(i)) {
# check whether input is a 1D magpie object
Expand Down Expand Up @@ -270,9 +270,12 @@ setClass("magpie", contains = "array", prototype = array(0, c(0, 0, 0)))
}

#' @exportMethod [
setMethod("[", #nolint
setMethod("[", # nolint
signature(x = "magpie"),
function(x, i, j, k, drop = FALSE, pmatch = FALSE, invert = FALSE, dim = NULL) {
function(x, i, j, k, ..., pmatch = FALSE, invert = FALSE, dim = NULL, drop = FALSE) {
if (length(list(...)) > 0) {
stop("unknown argument(s) supplied!")
}
if (!is.null(dim)) {
if (!is.element(dim, 1:3)) stop("Invalid dim selection (allowed: 1, 2 or 3)")
if (!missing(j) || (!missing(k) && !is.null(k) && k != FALSE)) {
Expand Down Expand Up @@ -302,14 +305,7 @@ setMethod("[", #nolint
j <- .dimextract(x, j, 2, pmatch = pmatch, invert = invert)
}
if (!missing(k)) k <- .dimextract(x, k, 3, pmatch = pmatch, invert = invert)

if (!missing(i) && missing(j) && !missing(k) && .isFALSE(k) &&
.isFALSE(drop) && .isFALSE(pmatch) && .isFALSE(invert)) {
# there is a weird case in which k is actually missing but is getting the
# value of the next argument in line (drop)
# this one is catched via .isFALSE(k)
# in addition non-default settings for drop, pmatch and invert indicate that object should still be
# returned as magpie object (and not as numeric as the following line will do)
if (length(sys.call(-1)) == 3) {
return(x@.Data[i])
}
if (!missing(k) && .isFALSE(k)) {
Expand All @@ -324,17 +320,17 @@ setMethod("[", #nolint
)

#' @exportMethod [<-
setMethod("[<-", #nolint
setMethod("[<-", # nolint
signature(x = "magpie"),
function(x, i, j, k, value, pmatch = FALSE, dim = NULL) {
function(x, i, j, k, ..., pmatch = FALSE, dim = NULL, value) {
if (length(list(...)) > 0) {
stop("unknown argument(s) supplied!")
}
if (!is.null(dim)) {
if (!is.element(dim, 1:3)) stop("Invalid dim selection (allowed: 1, 2 or 3)")
if (!missing(j) || (!missing(k) && !missing(value))) {
stop("Only single dimension selection allowed when dim is set!")
}
if (missing(value) && !missing(k)) {
value <- k
}
if (dim == 1) {
x[i, , , pmatch = pmatch] <- value
} else if (dim == 2) {
Expand Down Expand Up @@ -365,26 +361,25 @@ setMethod("[<-", #nolint
if (is.factor(k)) k <- as.character(k)
if (is.character(k) || is.list(k)) k <- .dimextract(x, k, 3, pmatch = pmatch)
}
if (missing(value)) {
x@.Data[i] <- k
return(x)
} else {
if (is.magpie(value)) {
if (missing(i)) ii <- seq_len(dim(x)[1]) else ii <- i
if (missing(j)) jj <- seq_len(dim(x)[2]) else jj <- j
if (missing(k)) kk <- seq_len(dim(x)[3]) else kk <- k
value <- magpie_expand(value, x[ii, jj, kk])
} else if (length(value) != length(x@.Data[i, j, k]) & length(value) != 1) {
# dangerous writing of value as order might be wrong!
stop("Replacement does not work! Different replacement length!")
} else if (length(value) != 1 && !is.null(getOption("magclass.verbosity")) &&
getOption("magclass.verbosity") > 1) {
message("NOTE ([<-): Dangerous replacement! As replacement value is not",
" an MAgPIE object name checking is deactivated!\n")
}
x@.Data[i, j, k] <- value
if (length(sys.call(which = -1)) == 4) {
x@.Data[i] <- value
return(x)
}
if (is.magpie(value)) {
if (missing(i)) ii <- seq_len(dim(x)[1]) else ii <- i
if (missing(j)) jj <- seq_len(dim(x)[2]) else jj <- j
if (missing(k)) kk <- seq_len(dim(x)[3]) else kk <- k
value <- magpie_expand(value, x[ii, jj, kk])
} else if (length(value) != length(x@.Data[i, j, k]) & length(value) != 1) {
# dangerous writing of value as order might be wrong!
stop("Replacement does not work! Different replacement length!")
} else if (length(value) != 1 && !is.null(getOption("magclass.verbosity")) &&
getOption("magclass.verbosity") > 1) {
message("NOTE ([<-): Dangerous replacement! As replacement value is not",
" an MAgPIE object name checking is deactivated!\n")
}
x@.Data[i, j, k] <- value
return(x)
}
)

Expand Down
6 changes: 3 additions & 3 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.9.1**
R package **magclass**, version **6.10.0**

[![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,7 +56,7 @@ 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.9.1, <https://github.com/pik-piam/magclass>.
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 (URL: https://doi.org/10.5281/zenodo.1158580), R package version 6.10.0, <URL: https://github.com/pik-piam/magclass>.

A BibTeX entry for LaTeX users is

Expand All @@ -65,7 +65,7 @@ A BibTeX entry for LaTeX users is
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.9.1},
note = {R package version 6.10.0},
doi = {10.5281/zenodo.1158580},
url = {https://github.com/pik-piam/magclass},
}
Expand Down
9 changes: 6 additions & 3 deletions tests/testthat/test-magpie.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ test_that("boolean subsetting works", {
})

test_that("error detection works", {
expect_error(p[, , , ], "argument is missing, with no default")
expect_error(p[, , , blub = 42], "unknown argument\\(s\\) supplied")
expect_error(p[, , , blub = 42] <- 42, "unknown argument\\(s\\) supplied")
expect_error(p[, , "A3"], "out of bounds")
expect_error(p[, , list("A3")], "out of bounds")
expect_error(p[, , list(scenario = "A3")], "out of bounds")
expect_error(p[, , list(blub = "A2")], "subdimension does not exist")

names(dimnames(p)) <- NULL
expect_error(p[, , list(scenario = "A2")], "subdimension does not exist \\(missing set names\\)") #nolint
expect_error(p[, , list(scenario = "A2")], "subdimension does not exist \\(missing set names\\)") # nolint

dimnames(p)[[3]] <- NULL
expect_error(p[, , "A2"], "Missing element names")
Expand Down Expand Up @@ -152,11 +155,11 @@ test_that("data.frame subsetting works", {
expect_identical(getItems(a[df[3:2][w, ]], 3), getItems(a, 3)[w])

# Unknown dimensions to be added in output!
df$blub <- paste0("bl", 1:dim(df)[1])
df$blub <- paste0("bl", seq_len(dim(df)[1]))
expect_identical(getItems(a[df[w, ]], 3), paste(getItems(a, 3), df$blub, sep = ".")[w])

df2 <- df
df2$ble <- paste0("ble", 1:dim(df2)[1])
df2$ble <- paste0("ble", seq_len(dim(df2)[1]))
expect_identical(getItems(a[df2[w, ]], 3), paste(getItems(a, 3), df2$blub, df2$ble, sep = ".")[w])

# subselections work
Expand Down

0 comments on commit 655348c

Please sign in to comment.