-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #224 from tscheypidi/master
Improved metadata handling in readSource + metadata helper function
- Loading branch information
Showing
20 changed files
with
305 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
ValidationKey: '62859660' | ||
ValidationKey: '63066150' | ||
AcceptedWarnings: | ||
- 'Warning: package ''.*'' was built under R version' | ||
- 'Warning: namespace ''.*'' is not available and has been replaced' | ||
AcceptedNotes: unable to verify current time | ||
AutocreateReadme: yes | ||
allowLinterWarnings: no | ||
enforceVersionUpdate: no | ||
skipCoverage: no |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Version: 3.6.4 | ||
Date: 2023-08-23 08:10:10 UTC | ||
SHA: d549de159878c4be432d706e3d676ab67de4c98b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
Type: Package | ||
Package: madrat | ||
Title: May All Data be Reproducible and Transparent (MADRaT) * | ||
Version: 3.14.0 | ||
Date: 2024-10-23 | ||
Version: 3.15.0 | ||
Date: 2024-10-25 | ||
Authors@R: c( | ||
person("Jan Philipp", "Dietrich", , "[email protected]", role = c("aut", "cre"), | ||
comment = c(affiliation = "Potsdam Institute for Climate Impact Research", ORCID = "0000-0002-4309-6431")), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#' cleanComment | ||
#' | ||
#' Helper function to clean a comment from additional metadata information | ||
#' | ||
#' @md | ||
#' @param x magclass object the comment should be read from | ||
#' @param remove Vector of categories to be removed | ||
#' @author Jan Philipp Dietrich | ||
#' @examples | ||
#' x <- maxample("animal") | ||
#' getComment(x) <- c("unit: bla", "comment: hallo", "blub: ble") | ||
#' madrat:::cleanComment(x) | ||
|
||
|
||
cleanComment <- function(x, remove = c("unit", "description", "comment", "origin", "creation date", "note")) { | ||
# remove old descriptors | ||
x <- getComment(x) | ||
out <- grep(paste0("^ *(", paste(remove, collapse = "|"), "):"), x, value = TRUE, invert = TRUE) | ||
if (length(out) == 0) return(NULL) | ||
return(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#' getFromComment | ||
#' | ||
#' Helper function extract a metadata comment | ||
#' | ||
#' @md | ||
#' @param x object the metadata should be extracted from | ||
#' @param name name of the metadata to be extracted (e.g. unit) | ||
#' @author Jan Philipp Dietrich | ||
#' @examples | ||
#' x <- as.magpie(1) | ||
#' getComment(x) <- c(" description: example description", " unit: kg") | ||
#' getFromComment(x, "unit") | ||
#' getFromComment(x, "description") | ||
#' @export | ||
|
||
getFromComment <- function(x, name) { | ||
comment <- attr(x, "comment") | ||
key <- paste0("^ ", name, ": ") | ||
entry <- grep(key, comment) | ||
if (length(entry) == 0) return(NULL) | ||
if (length(entry) > 1) stop("duplicate metadata entries found!") | ||
return(sub(key, "", comment[entry])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#' prepComment | ||
#' | ||
#' Helper function to properly format a metadata comment entry | ||
#' | ||
#' @md | ||
#' @param x content to be added as metadata comment | ||
#' @param name Name of the metadata entry | ||
#' @param warning Either NULL (no warning) or a warning text that should be | ||
#' returned if x is NULL | ||
#' @author Jan Philipp Dietrich | ||
#' @examples | ||
#' madrat:::prepComment("example comment", "example") | ||
|
||
prepComment <- function(x, name, warning = NULL) { | ||
if (!is.null(x)) { | ||
x[1] <- paste0(" ", name, ": ", x[1]) | ||
if (length(x) > 1) { | ||
x[2:length(x)] <- paste0(paste(rep(" ", 3 + nchar(name)), collapse = ""), x[2:length(x)]) | ||
} | ||
} else { | ||
if (!is.null(warning)) { | ||
vcat(0, warning) | ||
x <- paste0(" ", name, ": not provided") | ||
} | ||
} | ||
return(x) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#' prepExtendedComment | ||
#' | ||
#' Helper function condense metadata information into | ||
#' an extended comment entry | ||
#' | ||
#' @md | ||
#' @param x list containing the metadata to be condensed | ||
#' @param type output type, e.g. "TauTotal" | ||
#' @param warn boolean indicating whether warnings should be triggered | ||
#' if entries are missing, or not. | ||
#' @param n the number of functions to go back for the extraction of the call | ||
#' information | ||
#' @author Jan Philipp Dietrich | ||
#' @examples | ||
#' test <- function(a = 1) { | ||
#' return(madrat:::prepExtendedComment(list(unit = "m", description = "example", package = "blub"))) | ||
#' } | ||
#' test(a = 42) | ||
#' | ||
prepExtendedComment <- function(x, type = "#undefined", warn = TRUE, n = 1) { | ||
|
||
# extract function call information for the parent call defined by n | ||
cl <- sys.call(-n) | ||
f <- get(as.character(cl[[1]]), mode = "function", sys.frame(-n - 1)) | ||
cl <- match.call(definition = f, call = cl) | ||
|
||
if (isTRUE(warn)) { | ||
unitWarning <- paste0('Missing unit information for data set "', type, '"!') | ||
descriptionWarning <- paste0('Missing description for data set "', type, | ||
'"! Please add a description in the corresponding calc function!') | ||
} else { | ||
unitWarning <- NULL | ||
descriptionWarning <- NULL | ||
} | ||
|
||
unit <- prepComment(x$unit, "unit", unitWarning) | ||
description <- prepComment(x$description, "description", descriptionWarning) | ||
comment <- prepComment(cleanComment(x$x), "comment") | ||
origin <- prepComment(paste0(gsub("\\s{2,}", " ", paste(deparse(cl), collapse = "")), | ||
" (madrat ", unname(getNamespaceVersion("madrat")), " | ", x$package, ")"), | ||
"origin") | ||
date <- prepComment(date(), "creation date") | ||
note <- prepComment(x$note, "note") | ||
|
||
extendedComment <- c(description, | ||
unit, | ||
note, | ||
comment, | ||
origin, | ||
date) | ||
return(extendedComment) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.