diff --git a/NEWS.md b/NEWS.md index f42c3fe0..d94262d3 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 coerced 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) +})