Skip to content
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

Bug: xportr_metadata replaces metadata with default arguments #224

Open
averissimo opened this issue Jan 23, 2024 · 0 comments
Open

Bug: xportr_metadata replaces metadata with default arguments #224

averissimo opened this issue Jan 23, 2024 · 0 comments
Labels
bug Something isn't working programming

Comments

@averissimo
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working programming
Projects
None yet
Development

No branches or pull requests

1 participant