From 0d13895cd18e684da1375c4a4ee6ca8d9f6bd4ec Mon Sep 17 00:00:00 2001 From: elimillera Date: Fri, 9 Jun 2023 03:09:50 +0000 Subject: [PATCH 1/2] #149 - Coerse metadata order to numeric --- NEWS.md | 1 + R/format.R | 11 +++++------ R/label.R | 11 +++++------ R/length.R | 11 +++++------ R/order.R | 12 ++++++------ R/type.R | 11 +++++------ tests/testthat/test-order.R | 13 +++++++++++++ 7 files changed, 40 insertions(+), 30 deletions(-) diff --git a/NEWS.md b/NEWS.md index f42c3fe0..48030221 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,7 @@ * Fixed bug where character_types were case sensitive. They are now case insensitive. * Updated `xportr_type` to make type coercion more explicit. * Added function `xportr_metadata()` to explicitly set metadata at the start of a pipeline (#44) +* Metadata order columns are now coersed to numeric by default in `xportr_order` to prevent character sorting (#149) ## Documentation diff --git a/R/format.R b/R/format.R index 9c8fd527..bfc6bd40 100644 --- a/R/format.R +++ b/R/format.R @@ -28,12 +28,11 @@ #' ) #' #' adsl <- xportr_format(adsl, metadata) -xportr_format <- function( - .df, - metadata = NULL, - domain = NULL, - verbose = getOption("xportr.length_verbose", "none"), - metacore = deprecated()) { +xportr_format <- function(.df, + metadata = NULL, + domain = NULL, + verbose = getOption("xportr.length_verbose", "none"), + metacore = deprecated()) { if (!missing(metacore)) { lifecycle::deprecate_warn( when = "0.3.0", diff --git a/R/label.R b/R/label.R index 738a5d50..e627c85c 100644 --- a/R/label.R +++ b/R/label.R @@ -30,12 +30,11 @@ #' ) #' #' adsl <- xportr_label(adsl, metadata) -xportr_label <- function( - .df, - metadata = NULL, - domain = NULL, - verbose = getOption("xportr.length_verbose", "none"), - metacore = deprecated()) { +xportr_label <- function(.df, + metadata = NULL, + domain = NULL, + verbose = getOption("xportr.length_verbose", "none"), + metacore = deprecated()) { if (!missing(metacore)) { lifecycle::deprecate_warn( when = "0.3.0", diff --git a/R/length.R b/R/length.R index dd6842dd..46f6b26c 100644 --- a/R/length.R +++ b/R/length.R @@ -28,12 +28,11 @@ #' ) #' #' adsl <- xportr_length(adsl, metadata) -xportr_length <- function( - .df, - metadata = NULL, - domain = NULL, - verbose = getOption("xportr.length_verbose", "none"), - metacore = deprecated()) { +xportr_length <- function(.df, + metadata = NULL, + domain = NULL, + verbose = getOption("xportr.length_verbose", "none"), + metacore = deprecated()) { if (!missing(metacore)) { lifecycle::deprecate_warn( when = "0.3.0", diff --git a/R/order.R b/R/order.R index a49a560f..7ecce6d8 100644 --- a/R/order.R +++ b/R/order.R @@ -25,12 +25,11 @@ #' ) #' #' adsl <- xportr_order(adsl, metadata) -xportr_order <- function( - .df, - metadata = NULL, - domain = NULL, - verbose = getOption("xportr.length_verbose", "none"), - metacore = deprecated()) { +xportr_order <- function(.df, + metadata = NULL, + domain = NULL, + verbose = getOption("xportr.length_verbose", "none"), + metacore = deprecated()) { if (!missing(metacore)) { lifecycle::deprecate_warn( when = "0.3.0", @@ -69,6 +68,7 @@ xportr_order <- function( # Grabs vars from Spec and inputted dataset vars_in_spec_ds <- metadata[, c(variable_name, order_name)] %>% + mutate(!!sym(order_name) := as.numeric(!!sym(order_name))) %>% arrange(!!sym(order_name)) %>% extract2(variable_name) diff --git a/R/type.R b/R/type.R index bac6bb9b..9c28a06b 100644 --- a/R/type.R +++ b/R/type.R @@ -32,12 +32,11 @@ #' ) #' #' df2 <- xportr_type(.df, metadata, "test") -xportr_type <- function( - .df, - metadata = NULL, - domain = NULL, - verbose = getOption("xportr.length_verbose", "none"), - metacore = deprecated()) { +xportr_type <- function(.df, + metadata = NULL, + domain = NULL, + verbose = getOption("xportr.length_verbose", "none"), + metacore = deprecated()) { if (!missing(metacore)) { lifecycle::deprecate_warn( when = "0.3.0", diff --git a/tests/testthat/test-order.R b/tests/testthat/test-order.R index 11cde5f7..6f087841 100644 --- a/tests/testthat/test-order.R +++ b/tests/testthat/test-order.R @@ -137,3 +137,16 @@ test_that("xportr_order: Variable ordering messaging is correct", { ) ) }) + +test_that("xportr_order: Metadata order columns are coersed to numeric", { + df <- data.frame(c = 1:5, a = "a", d = 5:1, b = LETTERS[1:5]) + df_meta <- data.frame( + dataset = "df", + variable = letters[1:4], + order = c("1", "2", "11", "90") + ) + + ordered_df <- xportr_order(df, df_meta) + + expect_equal(names(ordered_df), df_meta$variable) +}) From e412b82e9a038478003b38f37981281b9e55b191 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Sun, 11 Jun 2023 14:52:33 -0400 Subject: [PATCH 2/2] Update NEWS.md --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 48030221..d94262d3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,7 +8,7 @@ * Fixed bug where character_types were case sensitive. They are now case insensitive. * Updated `xportr_type` to make type coercion more explicit. * Added function `xportr_metadata()` to explicitly set metadata at the start of a pipeline (#44) -* Metadata order columns are now coersed to numeric by default in `xportr_order` to prevent character sorting (#149) +* Metadata order columns are now coerced to numeric by default in `xportr_order()` to prevent character sorting (#149) ## Documentation