Skip to content

Commit fbd4a14

Browse files
authored
Merge pull request #514 from R-Lum/issue_480_p1
Add the replace_metadata<-() method for the Risoe.BINfileData class
2 parents d83f86c + dd67bcd commit fbd4a14

9 files changed

+166
-2
lines changed

DESCRIPTION

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Collate:
9393
'RLum.Data.Image-class.R'
9494
'RLum.Data.Spectrum-class.R'
9595
'RLum.Results-class.R'
96+
'metadata.R'
9697
'set_Risoe.BINfileData.R'
9798
'get_Risoe.BINfileData.R'
9899
'Risoe.BINfileData-class.R'

NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ S3method(summary,RLum.Data.Curve)
7171
S3method(summary,RLum.Data.Image)
7272
S3method(summary,RLum.Results)
7373
S3method(unlist,RLum.Analysis)
74+
export("replace_metadata<-")
7475
export(Analyse_SAR.OSLdata)
7576
export(CW2pHMi)
7677
export(CW2pLM)
@@ -224,6 +225,7 @@ exportClasses(RLum.Data.Image)
224225
exportClasses(RLum.Data.Spectrum)
225226
exportClasses(RLum.Results)
226227
exportClasses(Risoe.BINfileData)
228+
exportMethods("replace_metadata<-")
227229
exportMethods(bin_RLum.Data)
228230
exportMethods(get_RLum)
229231
exportMethods(get_Risoe.BINfileData)

NEWS.Rmd

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ and `RLum.Analysis-class` objects and lists of such objects.
2727
* `merge_RLum.Data.Spectrum()`: This new function allows to merge two or
2828
more `RLum.Data.Spectrum` objects in different ways (#368, fixed in #419).
2929

30+
* `replace_metadata()`: This function allows to manipulate the metadata of
31+
a `Risoe.BINfileData` object (#480, fixed in #514).
32+
3033
* `view()`: Provides a shortcut to the `utils::View()` spreadsheet-like data
3134
viewer tailored to the objects in the package (#489, fixed in #490).
3235

NEWS.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<!-- NEWS.md was auto-generated by NEWS.Rmd. Please DO NOT edit by hand!-->
77

8-
# Changes in version 0.9.26.9000-82 (2024-12-04)
8+
# Changes in version 0.9.26.9000-82 (2024-12-05)
99

1010
## New functions
1111

@@ -28,6 +28,9 @@
2828
more `RLum.Data.Spectrum` objects in different ways (#368, fixed in
2929
\#419).
3030

31+
- `replace_metadata()`: This function allows to manipulate the metadata
32+
of a `Risoe.BINfileData` object (#480, fixed in \#514).
33+
3134
- `view()`: Provides a shortcut to the `utils::View()` spreadsheet-like
3235
data viewer tailored to the objects in the package (#489, fixed in
3336
\#490).

R/Risoe.BINfileData-class.R

+63-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' @include get_Risoe.BINfileData.R set_Risoe.BINfileData.R view.R
1+
#' @include get_Risoe.BINfileData.R set_Risoe.BINfileData.R metadata.R view.R
22
NULL
33

44
#' Class `"Risoe.BINfileData"`
@@ -445,6 +445,68 @@ setMethod("get_Risoe.BINfileData",
445445
}
446446
)
447447

448+
# replace_metadata() --------------------------------------------------------
449+
#' @describeIn Risoe.BINfileData
450+
#' Replaces metadata of [Risoe.BINfileData-class] objects
451+
#'
452+
#' @param object an object of class [Risoe.BINfileData-class]
453+
#'
454+
#' @param info_element [character] (**required**) name of the metadata field
455+
#' to replace
456+
#'
457+
#' @param subset [expression] (*optional*) logical expression to limit the
458+
#' substitution only to the selected subset of elements
459+
#'
460+
#' @param value (**required**) The value assigned to the selected elements
461+
#' of the metadata field.
462+
#'
463+
#' @keywords internal
464+
#'
465+
#' @md
466+
#' @export
467+
setMethod("replace_metadata<-",
468+
signature= "Risoe.BINfileData",
469+
definition = function(object, info_element, subset = NULL, value) {
470+
.set_function_name("replace_metadata")
471+
on.exit(.unset_function_name(), add = TRUE)
472+
473+
## Integrity checks ---------------------------------------------
474+
475+
.validate_class(info_element, "character")
476+
valid.names <- colnames(object@METADATA)
477+
if (!info_element %in% valid.names) {
478+
.throw_error("'info_element' not recognised")
479+
}
480+
481+
## select relevant rows
482+
sel <- TRUE
483+
if (!is.null(substitute(subset))) {
484+
sel <- tryCatch(eval(
485+
expr = substitute(subset),
486+
envir = object@METADATA,
487+
enclos = parent.frame()
488+
), error = function(e) {
489+
.throw_error("Invalid 'subset' expression, valid terms are: ",
490+
.collapse(valid.names, quote = FALSE))
491+
})
492+
if (!is.logical(sel)) {
493+
.throw_error("'subset' should contain a logical expression")
494+
}
495+
if (all(is.na(sel))) {
496+
sel <- FALSE
497+
}
498+
if (!any(sel)) {
499+
.throw_message("'subset' expression produced an ",
500+
"empty selection, nothing done")
501+
return(object)
502+
}
503+
}
504+
505+
object@METADATA[sel, info_element] <- value
506+
assign(x = deparse(substitute(object))[1], object)
507+
})
508+
509+
448510
# view () -----------------------------------------------------------------------
449511
#'@describeIn Risoe.BINfileData
450512
#'View method for [Risoe.BINfileData-class] objects

R/metadata.R

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#' @title Safe replacement of object metadata
2+
#'
3+
#' @description
4+
#' Generic function for replacement of object metadata.
5+
#'
6+
#' @param object (**required**) object to manipulate
7+
#'
8+
#' @param ... further arguments passed to the function
9+
#'
10+
#' @param value the value assigned
11+
#'
12+
#' @author
13+
#' Marco Colombo, Institute of Geography, Heidelberg University (Germany)
14+
#'
15+
#' @keywords utilities
16+
#'
17+
#' @md
18+
#' @export
19+
setGeneric("replace_metadata<-",
20+
function (object, ..., value) standardGeneric("replace_metadata<-"))

man/Risoe.BINfileData-class.Rd

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/replace_metadata-set.Rd

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_metadata.R

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## load data
2+
bin.v8 <- system.file("extdata/BINfile_V8.binx", package = "Luminescence")
3+
risoe <- read_BIN2R(bin.v8, verbose = FALSE)
4+
5+
test_that("input validation", {
6+
testthat::skip_on_cran()
7+
8+
expect_error(replace_metadata(risoe, "error") <- 1,
9+
"'info_element' not recognised")
10+
expect_error(replace_metadata(risoe, "SEL", subset = error == 99) <- 0,
11+
"Invalid 'subset' expression, valid terms are")
12+
expect_error(replace_metadata(risoe, "SEL", subset = ID + 99) <- 0,
13+
"'subset' should contain a logical expression")
14+
expect_message(replace_metadata(risoe, "SEL", subset = ID == 99) <- 0,
15+
"'subset' expression produced an empty selection, nothing done")
16+
expect_message(replace_metadata(risoe, "SEL", subset = ID == NA) <- 0,
17+
"'subset' expression produced an empty selection, nothing done")
18+
})
19+
20+
test_that("check functionality", {
21+
testthat::skip_on_cran()
22+
23+
## Risoe.BINfileData
24+
res <- risoe
25+
replace_metadata(res, "SEL") <- FALSE
26+
expect_equal(res@METADATA$SEL,
27+
rep(FALSE, nrow(res@METADATA)))
28+
replace_metadata(res, "LTYPE", subset = SET == 2 & POSITION == 1) <- "OSL"
29+
expect_equal(res@METADATA$LTYPE,
30+
c("OSL", "TL"))
31+
32+
## the original object is unchanged
33+
expect_equal(risoe@METADATA$SEL,
34+
rep(TRUE, nrow(res@METADATA)))
35+
expect_equal(risoe@METADATA$LTYPE,
36+
rep("TL", nrow(res@METADATA)))
37+
})

0 commit comments

Comments
 (0)