Bug: `xportr_metadata` replaces metadata with default arguments
averissimo opened this issue · 0 comments
averissimo commented
What happened?
xportr_metadata
will always replace the metadata
attribute even if the default parameters are given (as it's NULL
)
See example.
Possible solution (replicate domain
setting)
diff --git a/R/metadata.R b/R/metadata.R
index df206ef..baa0905 100644
--- a/R/metadata.R
+++ b/R/metadata.R
@@ -44,12 +44,15 @@ xportr_metadata <- function(.df, metadata = NULL, domain = NULL) {
if (is.null(metadata) && is.null(domain)) {
stop("Must provide either metadata or domain argument")
}
- ## Common section to detect domain from argument or attribute
+ ## Common section to detect default arguments
domain <- get_domain(.df, domain)
if (!is.null(domain)) attr(.df, "_xportr.df_arg_") <- domain
+ metadata <- metadata %||% attr(.df, "_xportr.df_metadata_")
+ if (!is.null(metadata)) attr(.df, "_xportr.df_arg_") <- metadata
+
## End of common section
- structure(.df, `_xportr.df_metadata_` = metadata)
+ .df
}
Session Information
No response
Reproducible Example
library(xportr)
metadata <- data.frame(
dataset = "test",
variable = c("Subj", "Param", "Val", "NotUsed"),
type = c("numeric", "character", "numeric", "character"),
format = NA,
order = c(1, 3, 4, 2)
)
adlb <- data.frame(
Subj = as.character(123, 456, 789),
Different = c("a", "b", "c"),
Val = c("1", "2", "3"),
Param = c("param1", "param2", "param3")
)
adlb_with_metadata <- adlb |>
xportr_metadata(metadata) |>
xportr_metadata(domain = "test")
attr(adlb_with_metadata, "_xportr.df_metadata_")
#> NULL
Created on 2024-01-23 with reprex v2.0.2