From d08071da4d5f691a3817597a51ae57a16dc27e04 Mon Sep 17 00:00:00 2001 From: Jan Dietrich Date: Mon, 9 Dec 2019 13:32:08 +0100 Subject: [PATCH] -getSets add now dimCode information to output and can now be manipulated via adressing a specific subdimension --- DESCRIPTION | 4 ++-- R/getSets.R | 13 ++++++++++--- man/getSets.Rd | 4 ++-- tests/testthat/test-getSets.R | 6 ++++-- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index ffac493a..2ba8b4c7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: magclass Type: Package Title: Data Class and Tools for Handling Spatial-Temporal Data -Version: 5.6.0 +Version: 5.7.0 Date: 2019-12-09 Authors@R: c(person("Jan Philipp", "Dietrich", email = "dietrich@pik-potsdam.de", role = c("aut","cre")), person("Benjamin Leon", "Bodirsky", email = "bodirsky@pik-potsdam.de", role = "aut"), @@ -48,4 +48,4 @@ LazyData: true Encoding: UTF-8 RoxygenNote: 7.0.2 VignetteBuilder: knitr -ValidationKey: 10213840 +ValidationKey: 10396230 diff --git a/R/getSets.R b/R/getSets.R index 7351a1b8..014d9c26 100644 --- a/R/getSets.R +++ b/R/getSets.R @@ -12,7 +12,7 @@ #' names of the object with. #' @return Sets of the MAgPIE-object. If no information about contained sets is #' available NULL -#' @author Markus Bonsch +#' @author Markus Bonsch, Jan Philipp Dietrich #' @seealso \code{\link{getRegions}}, #' \code{\link{getNames}},\code{\link{getYears}}, \code{\link{getCPR}}, #' \code{\link{read.magpie}}, \code{\link{write.magpie}}, @@ -23,7 +23,7 @@ #' getSets(a) <- c("reg","cell","t","bla","blub") #' getSets(a) #' -#' getSets(a)[4] <- "BLA" +#' getSets(a)["d3.1"] <- "BLA" #' getSets(a,fulldim=FALSE) #' getSets(a) #' @@ -39,7 +39,14 @@ getSets <- function(x,fulldim=TRUE,sep=".") { return(x) } ) - out <- as.vector(unlist(tmp)) + addDimCode <- function(x) { + names(x) <- 1:length(x) + return(x) + } + tmp <- lapply(tmp,addDimCode) + names(tmp) <- paste0("d",1:length(tmp)) + + out <- unlist(tmp) } return(out) } diff --git a/man/getSets.Rd b/man/getSets.Rd index 779626e8..e374ca0f 100644 --- a/man/getSets.Rd +++ b/man/getSets.Rd @@ -38,7 +38,7 @@ Extracts sets of a MAgPIE-object if available getSets(a) <- c("reg","cell","t","bla","blub") getSets(a) - getSets(a)[4] <- "BLA" + getSets(a)["d3.1"] <- "BLA" getSets(a,fulldim=FALSE) getSets(a) @@ -50,5 +50,5 @@ Extracts sets of a MAgPIE-object if available \code{"\linkS4class{magpie}"} } \author{ -Markus Bonsch +Markus Bonsch, Jan Philipp Dietrich } diff --git a/tests/testthat/test-getSets.R b/tests/testthat/test-getSets.R index 9e56f5a0..d55f4c76 100644 --- a/tests/testthat/test-getSets.R +++ b/tests/testthat/test-getSets.R @@ -2,7 +2,9 @@ context("Set Manipulation Test") test_that("sets names can be overwritten", { x <- new.magpie("GLO.REG","y1995","BLA.BLUB",sets=c("glo.reg","year","glo.data")) - expect_identical(getSets(x),c("glo","reg","year","glo","data")) + expect_identical(getSets(x),c("d1.1"="glo","d1.2"="reg","d2.1"="year","d3.1"="glo","d3.2"="data")) getSets(x)[4] <- "bla" - expect_identical(getSets(x),c("glo","reg","year","bla","data")) + expect_identical(getSets(x),c("d1.1"="glo","d1.2"="reg","d2.1"="year","d3.1"="bla","d3.2"="data")) + getSets(x)["d1.2"] <- "region" + expect_identical(getSets(x),c("d1.1"="glo","d1.2"="region","d2.1"="year","d3.1"="bla","d3.2"="data")) })