-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added tools for cache persistent status messages #177
Changes from 4 commits
de66d68
f9ccb69
97d1b23
96ef848
0bdfdc8
fef5347
718e548
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.5.0 | ||
Date: 2023-07-21 | ||
Version: 3.6.0 | ||
Date: 2023-07-24 | ||
Authors@R: c( | ||
person("Jan Philipp", "Dietrich", , "[email protected]", role = c("aut", "cre")), | ||
person("Lavinia", "Baumstark", , "[email protected]", role = "aut"), | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,31 @@ | ||||||
#' Tool: getMadratMessage | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes me think it is a function with the "tool" prefix, but it is not. So maybe delete the "Tool: " here?
Suggested change
|
||||||
#' | ||||||
#' Read a madrat message from the madrat environment. The madrat environment | ||||||
#' behaves similar like global options, except that 1) messages will also | ||||||
#' be stored in cache files and restored when a cache file is being loaded and | ||||||
#' 2) messages are always stored in lists with messages split by function calls | ||||||
#' where the message was triggered. | ||||||
#' | ||||||
#' @param name The category in which the message should be stored | ||||||
#' @param fname function name. If specified only messages belonging to the | ||||||
#' functions history will be returned (this includes entries from the function | ||||||
#' itself, but also entries from functions which were called by this function). | ||||||
#' @author Jan Philipp Dietrich | ||||||
#' @seealso \code{\link{getMadratMessage}} | ||||||
#' @examples | ||||||
#' putMadratMessage("test", "This is a toast", fname = "readTau") | ||||||
#' getMadratMessage("test", fname = "calcTauTotal") | ||||||
#' @export | ||||||
|
||||||
getMadratMessage <- function(name = NULL, fname = NULL) { | ||||||
x <- getOption("madratMessage") | ||||||
if (!is.null(x) && !is.null(fname)) { | ||||||
deps <- suppressWarnings(getDependencies(sub("^.*:::", "", fname), | ||||||
pfuehrlich-pik marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
packages = getConfig("packages"), self = TRUE)$func) | ||||||
for (n in names(x)) { | ||||||
x[[n]] <- x[[n]][sub("^.*:::", "", names(x[[n]])) %in% deps] | ||||||
} | ||||||
} | ||||||
if (!is.null(name)) x <- x[[name]] | ||||||
return(x) | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#' Tool: putMadratMessage | ||
#' | ||
#' Store a madrat message in the madrat environment. The madrat environment | ||
#' behaves similar like global options, except that 1) messages will also | ||
#' be stored in cache files and restored when a cache file is being loaded and | ||
#' 2) messages are always stored in lists with messages split by function calls | ||
#' where the message was triggered. | ||
#' | ||
#' @param name The category in which the message should be stored | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What categories do you mean here? The example puts "test" here, but I'm still not sure how to use this. It is an arbitrary string chosen by the user? |
||
#' @param value The message that should be recorded as character. Alternatively, | ||
#' if \code{name} is not set, it is also possible to provide a complete list | ||
#' of the structure value[[name]][[fname]] where name and fname correspond | ||
pfuehrlich-pik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' to the category name and function name entries. | ||
#' @param fname function name the entry belongs to or the frame number from which | ||
#' the function name should be derived from (e.g. -1 to recieve function name | ||
#' from parent function). | ||
#' @param add boolean deciding whether the value should be added to a existing | ||
#' value (TRUE) or overwrite it (FALSE) | ||
#' @author Jan Philipp Dietrich | ||
#' @seealso \code{\link{putMadratMessage}} | ||
#' @examples | ||
#' putMadratMessage("test", "This is a toast", fname = "readTau") | ||
#' getMadratMessage("test", fname = "calcTauTotal") | ||
#' @export | ||
|
||
putMadratMessage <- function(name, value, fname = -1, add = FALSE) { | ||
if ((missing(name) || is.null(name)) && is.list(value) && !is.null(names(value))) { | ||
for (n in names(value)) { | ||
for (f in names(value[[n]])) { | ||
putMadratMessage(name = n, value = value[[n]][[f]], fname = f, add = add) | ||
} | ||
} | ||
} else { | ||
if (is.numeric(fname)) fname <- as.character(sys.call(fname))[1] | ||
madratMessage <- getOption("madratMessage") | ||
madratMessage[[name]][[fname]] <- if (add) c(madratMessage[[name]][[fname]], value) else value | ||
options(madratMessage = madratMessage) # nolint: undesirable_function_linter | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#' Tool: resetMadratMessages | ||
#' | ||
#' Delete stored madrat messages from the madrat environment. The madrat environment | ||
#' behaves similar like global options, except that 1) messages will also | ||
#' be stored in cache files and restored when a cache file is being loaded and | ||
#' 2) messages are always stored in lists with messages split by function calls | ||
#' where the message was triggered. | ||
pfuehrlich-pik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' | ||
#' @param name The category for which the messages should be reset (if not set | ||
#' messages in all categories will be reset) | ||
pfuehrlich-pik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' @param fname function name for which the entries should be reset (if not specified | ||
#' messages for all function names will be reset) | ||
#' @author Jan Philipp Dietrich | ||
#' @seealso \code{\link{putMadratMessage}}, \code{\link{getMadratMessage}} | ||
#' @examples | ||
#' putMadratMessage("test", "This is a toast", fname = "readTau") | ||
#' getMadratMessage("test", fname = "calcTauTotal") | ||
#' resetMadratMessages("test") | ||
#' @export | ||
|
||
resetMadratMessages <- function(name, fname) { | ||
if (missing(name)) name <- NULL | ||
if (missing(fname)) fname <- NULL | ||
pfuehrlich-pik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
madratMessage <- getOption("madratMessage") | ||
|
||
if (is.null(name) && is.null(fname)) { | ||
madratMessage <- NULL | ||
} else if (is.null(fname)) { | ||
madratMessage[[name]] <- NULL | ||
} else if (is.null(name)) { | ||
for (n in names(madratMessage)) { | ||
madratMessage[[n]][[fname]] <- NULL | ||
} | ||
} else { | ||
madratMessage[[name]][[fname]] <- NULL | ||
} | ||
if (length(madratMessage) == 0) madratMessage <- NULL | ||
options(madratMessage = madratMessage) # nolint: undesirable_function_linter | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
test_that("getMadratMessage and putMadratMessage work", { | ||
localConfig(globalenv = FALSE, .verbose = FALSE) | ||
expect_silent(resetMadratMessages()) | ||
expect_silent(putMadratMessage("test", "This is a test", fname = "example")) | ||
expect_silent(putMadratMessage("test", "This is a toast", fname = "readTau")) | ||
expect_identical(getOption("madratMessage"), list(test = list(example = "This is a test", | ||
readTau = "This is a toast"))) | ||
expect_identical(getMadratMessage("test", fname = "calcTauTotal"), list(readTau = "This is a toast")) | ||
expect_silent(resetMadratMessages()) | ||
expect_null(getMadratMessage()) | ||
test <- function() { | ||
putMadratMessage("level", "level 1") | ||
.tmp <- function() { | ||
putMadratMessage("level", "level 2") | ||
putMadratMessage("level", "level 1 again", fname = -2, add = TRUE) | ||
} | ||
.tmp() | ||
} | ||
test() | ||
expect_identical(getMadratMessage(), list(level = list(test = c("level 1", "level 1 again"), .tmp = "level 2"))) | ||
expect_silent(resetMadratMessages(fname = ".tmp")) | ||
expect_identical(getMadratMessage(), list(level = list(test = c("level 1", "level 1 again")))) | ||
expect_silent(resetMadratMessages("level")) | ||
expect_null(getMadratMessage()) | ||
}) | ||
|
||
test_that("get/putMadratMessage work in combination with caching", { | ||
localConfig(globalenv = TRUE, .verbose = FALSE) | ||
resetMadratMessages() | ||
|
||
calcMessage1 <- function() { | ||
putMadratMessage("example", "message 1") | ||
return(list(x = as.magpie(1), description = "-", unit = "-")) | ||
} | ||
calcMessage2 <- function() { | ||
putMadratMessage("example", "message 2") | ||
return(list(x = as.magpie(2), description = "-", unit = "-")) | ||
} | ||
calcMessage3 <- function() { | ||
x <- calcOutput("Message1", aggregate = FALSE) | ||
putMadratMessage("example", "message 3") | ||
return(list(x = as.magpie(3), description = "-", unit = "-")) | ||
} | ||
globalassign("calcMessage1", "calcMessage2", "calcMessage3") | ||
|
||
tmp <- calcOutput("Message2", aggregate = FALSE) | ||
expect_identical(getMadratMessage(), list(example = list(calcMessage2 = "message 2"))) | ||
tmp <- calcOutput("Message3", aggregate = FALSE) | ||
expect_identical(getMadratMessage(), list(example = list(calcMessage2 = "message 2", calcMessage1 = "message 1", | ||
calcMessage3 = "message 3"))) | ||
options(madratMessage = NULL) | ||
expect_null(getMadratMessage()) | ||
tmp <- calcOutput("Message3", aggregate = FALSE) | ||
expect_identical(getMadratMessage(), list(example = list(calcMessage1 = "message 1", | ||
calcMessage3 = "message 3"))) | ||
resetMadratMessages() | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have expected that the message is also printed here, is there a reason why not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, with this procedure you can store various types of information. For some you might want to have them reported at some point for others you might not. As it is unclear what should happen with this information I would not print it here.