From a6696574cff9d8ed043adca3569fb09b8fd7e4ad Mon Sep 17 00:00:00 2001 From: Celine Date: Tue, 19 Dec 2023 06:54:40 -0500 Subject: [PATCH 01/37] Add a function for max length --- R/utils-xportr.R | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/R/utils-xportr.R b/R/utils-xportr.R index feb31195..38034cc4 100644 --- a/R/utils-xportr.R +++ b/R/utils-xportr.R @@ -387,3 +387,35 @@ check_multiple_var_specs <- function(metadata, ) } } + +#' Calculate the maximum length of variables +#' +#' Function to calculate the maximum length of variables in a given dataframe +#' +#' @inheritParams xportr_length +#' +#' @return Returns a dataframe with variables and their maximum length +#' +#' @export + +variable_max_length <- function(.df) { + max_nchar <- .df %>% + summarize(across(where(is.character), ~ max(0L, nchar(., type = "bytes"), na.rm = TRUE))) + + + xport_max_length <- data.frame() + col <- 0 + for (var in names(.df)) { + col <- col + 1 + + xport_max_length[col, xportr.variable_name] <- var + + if (is.character(.df[[var]])) { + xport_max_length[col, xportr.length] <- max_nchar[var] + } else { + xport_max_length[col, xportr.length] <- 8 + } + } + + return(xport_max_length) +} From ea1201acb9d0935c330a57d4985edf675d049910 Mon Sep 17 00:00:00 2001 From: Celine Date: Tue, 19 Dec 2023 07:25:08 -0500 Subject: [PATCH 02/37] Update to use getOption for df name --- R/utils-xportr.R | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/R/utils-xportr.R b/R/utils-xportr.R index 38034cc4..168a2a48 100644 --- a/R/utils-xportr.R +++ b/R/utils-xportr.R @@ -399,6 +399,10 @@ check_multiple_var_specs <- function(metadata, #' @export variable_max_length <- function(.df) { + + variable_length <- getOption("xportr.length") + variable_name <- getOption("xportr.variable_name") + max_nchar <- .df %>% summarize(across(where(is.character), ~ max(0L, nchar(., type = "bytes"), na.rm = TRUE))) @@ -408,12 +412,12 @@ variable_max_length <- function(.df) { for (var in names(.df)) { col <- col + 1 - xport_max_length[col, xportr.variable_name] <- var + xport_max_length[col, variable_name] <- var if (is.character(.df[[var]])) { - xport_max_length[col, xportr.length] <- max_nchar[var] + xport_max_length[col, variable_length] <- max_nchar[var] } else { - xport_max_length[col, xportr.length] <- 8 + xport_max_length[col, variable_length] <- 8 } } From df65c51a8d8d19a347ac44a389403981d03f08d4 Mon Sep 17 00:00:00 2001 From: Celine Date: Wed, 20 Dec 2023 09:25:06 -0500 Subject: [PATCH 03/37] Add message for data length --- R/length.R | 8 ++++++++ R/messages.R | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/R/length.R b/R/length.R index 17627268..47765973 100644 --- a/R/length.R +++ b/R/length.R @@ -121,6 +121,14 @@ xportr_length <- function(.df, } } + # Check if data length is shorter than metadata length + var_length_max <- variable_max_length(.df) + + length_msg <- left_join(var_length_max, metadata[, c(variable_name, variable_length)], by = variable_name) %>% + filter(length.x < length.y) + + max_length_msg(length_msg, verbose) + .df } diff --git a/R/messages.R b/R/messages.R index 6c4e21c0..6a2b417f 100644 --- a/R/messages.R +++ b/R/messages.R @@ -161,3 +161,30 @@ var_ord_msg <- function(reordered_vars, moved_vars, verbose) { cli_h2("All variables in dataset are ordered") } } + +#' Utility for data Lengths +#' +#' @param max_length Dataframe with data and metadata length +#' @param verbose Provides additional messaging for user +#' +#' @return Output to Console + +max_length_msg <- function(max_length, verbose) { + if (nrow(max_length) > 0) { + cli_h2("Variable length is shorter than the length specified in the metadata.") + + xportr_logger( + glue( + "Update length in metadata to trim the variables:" + ), + type = verbose + ) + + xportr_logger( + glue( + "{format(max_length[[1]], width = 8)} has a length of {format(as.character(max_length[[2]]), width = 3)} and a length of {format(as.character(max_length[[3]]), width = 3)} in metadata" + ), + type = verbose + ) + } +} From 6693b8e45f7424d74d3efdb686207e4caef696d6 Mon Sep 17 00:00:00 2001 From: Celine Date: Fri, 22 Dec 2023 03:45:05 -0500 Subject: [PATCH 04/37] Add argument to function --- R/length.R | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/R/length.R b/R/length.R index 47765973..2e35053d 100644 --- a/R/length.R +++ b/R/length.R @@ -11,6 +11,8 @@ #' @param domain Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset #' the metadata object. If none is passed, then name of the dataset passed as #' .df will be used. +#' @param length TO BE UPDATED!!! +#' *Permitted Values*: `"metadata"`, `"data"` #' @param verbose The action this function takes when an action is taken on the #' dataset or function validation finds an issue. See 'Messaging' section for #' details. Options are 'stop', 'warn', 'message', and 'none' @@ -66,6 +68,7 @@ xportr_length <- function(.df, metadata = NULL, domain = NULL, + length = "metadata", verbose = getOption("xportr.length_verbose", "none"), metacore = deprecated()) { if (!missing(metacore)) { @@ -96,13 +99,13 @@ xportr_length <- function(.df, metadata <- metadata$var_spec } - if (domain_name %in% names(metadata)) { - metadata <- metadata %>% - filter(!!sym(domain_name) == domain) - } else { - # Common check for multiple variables name - check_multiple_var_specs(metadata, variable_name) - } + # if (domain_name %in% names(metadata)) { + # metadata <- metadata %>% + # filter(!!sym(domain_name) == domain) + # } else { + # # Common check for multiple variables name + # check_multiple_var_specs(metadata, variable_name) + # } # Check any variables missed in metadata but present in input data --- @@ -110,24 +113,27 @@ xportr_length <- function(.df, length_log(miss_vars, verbose) - length <- metadata[[variable_length]] - names(length) <- metadata[[variable_name]] + length_metadata <- metadata[[variable_length]] + names(length_metadata) <- metadata[[variable_name]] for (i in names(.df)) { if (i %in% miss_vars) { attr(.df[[i]], "width") <- impute_length(.df[[i]]) } else { - attr(.df[[i]], "width") <- length[[i]] + attr(.df[[i]], "width") <- length_metadata[[i]] } } # Check if data length is shorter than metadata length - var_length_max <- variable_max_length(.df) + if (length == "data"){ + var_length_max <- variable_max_length(.df) - length_msg <- left_join(var_length_max, metadata[, c(variable_name, variable_length)], by = variable_name) %>% - filter(length.x < length.y) + length_msg <- left_join(var_length_max, metadata[, c(variable_name, variable_length)], by = variable_name) %>% + filter(length.x < length.y) + + max_length_msg(length_msg, verbose) + } - max_length_msg(length_msg, verbose) .df } From 878fa51107df5399893714b7412e8cd45d5cdd39 Mon Sep 17 00:00:00 2001 From: Celine Date: Wed, 3 Jan 2024 03:49:25 -0500 Subject: [PATCH 05/37] Add length from max data length to 'width' attribute --- R/length.R | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/R/length.R b/R/length.R index 2e35053d..7c3062ae 100644 --- a/R/length.R +++ b/R/length.R @@ -113,21 +113,30 @@ xportr_length <- function(.df, length_log(miss_vars, verbose) - length_metadata <- metadata[[variable_length]] - names(length_metadata) <- metadata[[variable_name]] - - for (i in names(.df)) { - if (i %in% miss_vars) { - attr(.df[[i]], "width") <- impute_length(.df[[i]]) - } else { - attr(.df[[i]], "width") <- length_metadata[[i]] + if (length == "metadata"){ + length_metadata <- metadata[[variable_length]] + names(length_metadata) <- metadata[[variable_name]] + + for (i in names(.df)) { + if (i %in% miss_vars) { + attr(.df[[i]], "width") <- impute_length(.df[[i]]) + } else { + attr(.df[[i]], "width") <- length_metadata[[i]] + } } } - # Check if data length is shorter than metadata length + # Assign length from data if (length == "data"){ var_length_max <- variable_max_length(.df) + length_data <- var_length_max[[variable_length]] + names(length_data) <- var_length_max[[variable_name]] + + for (i in names(.df)) { + attr(.df[[i]], "width") <- length_data[[i]] + } + length_msg <- left_join(var_length_max, metadata[, c(variable_name, variable_length)], by = variable_name) %>% filter(length.x < length.y) From edc977a14595528497936155f0903370ee2966e4 Mon Sep 17 00:00:00 2001 From: Celine Date: Wed, 3 Jan 2024 04:35:04 -0500 Subject: [PATCH 06/37] Update function description --- R/length.R | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/R/length.R b/R/length.R index 7c3062ae..7bd0d233 100644 --- a/R/length.R +++ b/R/length.R @@ -1,9 +1,9 @@ #' Assign SAS Length #' -#' Assigns SAS length from a metadata object to a given data frame. If a -#' length isn't present for a variable the length value is set to 200 for -#' character columns, and 8 for non-character columns. This value is stored in -#' the 'width' attribute of the column. +#' Assigns the SAS length to a specified data frame, either from a metadata object +#' or based on the calculated maximum data length. If a length isn't present for +#' a variable the length value is set to 200 for character columns, and 8 +#' for non-character columns. This value is stored in the 'width' attribute of the column. #' #' @param .df A data frame of CDISC standard. #' @param metadata A data frame containing variable level metadata. See @@ -11,7 +11,11 @@ #' @param domain Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset #' the metadata object. If none is passed, then name of the dataset passed as #' .df will be used. -#' @param length TO BE UPDATED!!! +#' @param length Choose the assigned length from either metadata or data. +#' +#' If `"metadata"` is specified, the assigned length is from the metadata length. +#' If `"data"` is specified, the assigned length is determined by the calculated maximum data length. +#' #' *Permitted Values*: `"metadata"`, `"data"` #' @param verbose The action this function takes when an action is taken on the #' dataset or function validation finds an issue. See 'Messaging' section for From 20f48533d8f36c260982b25ed35c225f29cbb176 Mon Sep 17 00:00:00 2001 From: Celine Date: Wed, 3 Jan 2024 08:58:06 -0500 Subject: [PATCH 07/37] add test for length argument --- tests/testthat/test-length.R | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/testthat/test-length.R b/tests/testthat/test-length.R index e749684d..dfe56cb4 100644 --- a/tests/testthat/test-length.R +++ b/tests/testthat/test-length.R @@ -224,3 +224,30 @@ test_that("xportr_length: Gets warning when metadata has multiple rows with same # Checks that message doesn't appear when xportr.domain_name is valid multiple_vars_in_spec_helper2(xportr_length) }) + +meta_example <- data.frame( + dataset = "df", + variable = c("USUBJID", "WEIGHT"), + length = c(10, 8) +) + +df <- data.frame( + USUBJID = c("1", "12", "123"), + WEIGHT = c(85, 45, 121 ) +) + +test_that("xportr_length: length assigned as expected from metadata or data", { + result <- df %>% + xportr_length(meta_example, length = "metadata") %>% + expect_attr_width(c(10,8)) + + result <- df %>% + xportr_length(meta_example, length = "data") %>% + expect_attr_width(c(3,8)) +}) + +test_that("xportr_length: Gets message when length in metadata longer than data length", { + result <- df %>% + xportr_length(meta_example, length = "data") %>% + expect_message() +}) From 8e01730c561e02e367cc832bbddae7b6918d411f Mon Sep 17 00:00:00 2001 From: Celine Date: Tue, 9 Jan 2024 09:02:19 -0500 Subject: [PATCH 08/37] Update nchar_gt_200 to take into account NA values --- R/utils-xportr.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils-xportr.R b/R/utils-xportr.R index 168a2a48..04c2f178 100644 --- a/R/utils-xportr.R +++ b/R/utils-xportr.R @@ -305,7 +305,7 @@ xpt_validate <- function(data) { # 4.0 max length of Character variables <= 200 bytes max_nchar <- data %>% - summarize(across(where(is.character), ~ max(nchar(., type = "bytes")))) + summarize(across(where(is.character), ~ max(0L, nchar(., type = "bytes"), na.rm = TRUE))) nchar_gt_200 <- max_nchar[which(max_nchar > 200)] if (length(nchar_gt_200) > 0) { err_cnd <- c( From 56f02d763ccdd6df581263d55ba47159b2669de7 Mon Sep 17 00:00:00 2001 From: Celine Date: Tue, 9 Jan 2024 09:19:58 -0500 Subject: [PATCH 09/37] Add test for variable length < 200 when the variable contains NAs --- tests/testthat/test-utils-xportr.R | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/testthat/test-utils-xportr.R b/tests/testthat/test-utils-xportr.R index 41f6adb8..f36393af 100644 --- a/tests/testthat/test-utils-xportr.R +++ b/tests/testthat/test-utils-xportr.R @@ -127,3 +127,11 @@ test_that("xpt_validate: Get error message when the length of a non-ASCII charac "Length of A must be 200 bytes or less." ) }) + +test_that("xpt_validate: Get error message when the length of a character variable is > 200 bytes and contains NAs", { + df <- data.frame(A = c(paste(rep("A", 201), collapse = ""), NA_character_)) + expect_equal( + xpt_validate(df), + "Length of A must be 200 bytes or less." + ) +}) From 4d3973e7e0b58d4dbbc14cdf8f9114e890ed0d6d Mon Sep 17 00:00:00 2001 From: Celine Date: Tue, 9 Jan 2024 09:22:06 -0500 Subject: [PATCH 10/37] Uncomment code --- R/length.R | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/R/length.R b/R/length.R index 7bd0d233..bbf08207 100644 --- a/R/length.R +++ b/R/length.R @@ -103,13 +103,13 @@ xportr_length <- function(.df, metadata <- metadata$var_spec } - # if (domain_name %in% names(metadata)) { - # metadata <- metadata %>% - # filter(!!sym(domain_name) == domain) - # } else { - # # Common check for multiple variables name - # check_multiple_var_specs(metadata, variable_name) - # } + if (domain_name %in% names(metadata)) { + metadata <- metadata %>% + filter(!!sym(domain_name) == domain) + } else { + # Common check for multiple variables name + check_multiple_var_specs(metadata, variable_name) + } # Check any variables missed in metadata but present in input data --- From 7fdcfc8d7f568cbbf05780a63cf5b53d175ee9fe Mon Sep 17 00:00:00 2001 From: Celine Date: Wed, 10 Jan 2024 03:33:57 -0500 Subject: [PATCH 11/37] add "domain =" in function for test --- tests/testthat/test-length.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-length.R b/tests/testthat/test-length.R index dfe56cb4..8a580227 100644 --- a/tests/testthat/test-length.R +++ b/tests/testthat/test-length.R @@ -238,16 +238,16 @@ df <- data.frame( test_that("xportr_length: length assigned as expected from metadata or data", { result <- df %>% - xportr_length(meta_example, length = "metadata") %>% + xportr_length(meta_example, domain = "df", length = "metadata") %>% expect_attr_width(c(10,8)) result <- df %>% - xportr_length(meta_example, length = "data") %>% + xportr_length(meta_example, domain = "df", length = "data") %>% expect_attr_width(c(3,8)) }) test_that("xportr_length: Gets message when length in metadata longer than data length", { result <- df %>% - xportr_length(meta_example, length = "data") %>% + xportr_length(meta_example, domain = "df", length = "data") %>% expect_message() }) From 43c0a2dce054238c34db688e763276f68da693fc Mon Sep 17 00:00:00 2001 From: Celine Date: Wed, 10 Jan 2024 03:48:57 -0500 Subject: [PATCH 12/37] Update style --- tests/testthat/test-length.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-length.R b/tests/testthat/test-length.R index 8a580227..68218903 100644 --- a/tests/testthat/test-length.R +++ b/tests/testthat/test-length.R @@ -233,17 +233,17 @@ meta_example <- data.frame( df <- data.frame( USUBJID = c("1", "12", "123"), - WEIGHT = c(85, 45, 121 ) + WEIGHT = c(85, 45, 121) ) test_that("xportr_length: length assigned as expected from metadata or data", { result <- df %>% xportr_length(meta_example, domain = "df", length = "metadata") %>% - expect_attr_width(c(10,8)) + expect_attr_width(c(10, 8)) result <- df %>% xportr_length(meta_example, domain = "df", length = "data") %>% - expect_attr_width(c(3,8)) + expect_attr_width(c(3, 8)) }) test_that("xportr_length: Gets message when length in metadata longer than data length", { From 2bd889d24e1fb593b09b14d38dbebe2187f9629f Mon Sep 17 00:00:00 2001 From: Celine Date: Wed, 10 Jan 2024 03:54:16 -0500 Subject: [PATCH 13/37] Update documentation --- NAMESPACE | 1 + man/max_length_msg.Rd | 19 +++++++++++++++++++ man/variable_max_length.Rd | 17 +++++++++++++++++ man/xportr_length.Rd | 16 ++++++++++++---- 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 man/max_length_msg.Rd create mode 100644 man/variable_max_length.Rd diff --git a/NAMESPACE b/NAMESPACE index d2f10378..c8cc4e25 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ export(length_log) export(type_log) export(var_names_log) export(var_ord_msg) +export(variable_max_length) export(xportr_df_label) export(xportr_format) export(xportr_label) diff --git a/man/max_length_msg.Rd b/man/max_length_msg.Rd new file mode 100644 index 00000000..85bd35a7 --- /dev/null +++ b/man/max_length_msg.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/messages.R +\name{max_length_msg} +\alias{max_length_msg} +\title{Utility for data Lengths} +\usage{ +max_length_msg(max_length, verbose) +} +\arguments{ +\item{max_length}{Dataframe with data and metadata length} + +\item{verbose}{Provides additional messaging for user} +} +\value{ +Output to Console +} +\description{ +Utility for data Lengths +} diff --git a/man/variable_max_length.Rd b/man/variable_max_length.Rd new file mode 100644 index 00000000..3c478d7c --- /dev/null +++ b/man/variable_max_length.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils-xportr.R +\name{variable_max_length} +\alias{variable_max_length} +\title{Calculate the maximum length of variables} +\usage{ +variable_max_length(.df) +} +\arguments{ +\item{.df}{A data frame of CDISC standard.} +} +\value{ +Returns a dataframe with variables and their maximum length +} +\description{ +Function to calculate the maximum length of variables in a given dataframe +} diff --git a/man/xportr_length.Rd b/man/xportr_length.Rd index 89fb5703..513f0496 100644 --- a/man/xportr_length.Rd +++ b/man/xportr_length.Rd @@ -8,6 +8,7 @@ xportr_length( .df, metadata = NULL, domain = NULL, + length = "metadata", verbose = getOption("xportr.length_verbose", "none"), metacore = deprecated() ) @@ -22,6 +23,13 @@ xportr_length( the metadata object. If none is passed, then name of the dataset passed as .df will be used.} +\item{length}{Choose the assigned length from either metadata or data. + +If \code{"metadata"} is specified, the assigned length is from the metadata length. +If \code{"data"} is specified, the assigned length is determined by the calculated maximum data length. + +\emph{Permitted Values}: \code{"metadata"}, \code{"data"}} + \item{verbose}{The action this function takes when an action is taken on the dataset or function validation finds an issue. See 'Messaging' section for details. Options are 'stop', 'warn', 'message', and 'none'} @@ -33,10 +41,10 @@ metadata now renamed with \code{metadata}} Data frame with \code{SASlength} attributes for each variable. } \description{ -Assigns SAS length from a metadata object to a given data frame. If a -length isn't present for a variable the length value is set to 200 for -character columns, and 8 for non-character columns. This value is stored in -the 'width' attribute of the column. +Assigns the SAS length to a specified data frame, either from a metadata object +or based on the calculated maximum data length. If a length isn't present for +a variable the length value is set to 200 for character columns, and 8 +for non-character columns. This value is stored in the 'width' attribute of the column. } \section{Messaging}{ \code{length_log} is the primary messaging tool for From 2cd4a6fadb6914c55d6da90c547bbd94d21e7062 Mon Sep 17 00:00:00 2001 From: Celine Date: Wed, 10 Jan 2024 04:12:08 -0500 Subject: [PATCH 14/37] Update style --- R/length.R | 2 +- R/messages.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/length.R b/R/length.R index bbf08207..b9699b41 100644 --- a/R/length.R +++ b/R/length.R @@ -131,7 +131,7 @@ xportr_length <- function(.df, } # Assign length from data - if (length == "data"){ + if (length == "data") { var_length_max <- variable_max_length(.df) length_data <- var_length_max[[variable_length]] diff --git a/R/messages.R b/R/messages.R index 6a2b417f..8dfce082 100644 --- a/R/messages.R +++ b/R/messages.R @@ -182,7 +182,7 @@ max_length_msg <- function(max_length, verbose) { xportr_logger( glue( - "{format(max_length[[1]], width = 8)} has a length of {format(as.character(max_length[[2]]), width = 3)} and a length of {format(as.character(max_length[[3]]), width = 3)} in metadata" + "{format(max_length[[1]], width = 8)} has a length of {format(as.character(max_length[[2]]), width = 3)} and a length of {format(as.character(max_length[[3]]), width = 3)} in metadata" ), type = verbose ) From e796810e8dd93898b499b7538deada44c5966d16 Mon Sep 17 00:00:00 2001 From: Celine Date: Wed, 10 Jan 2024 04:22:00 -0500 Subject: [PATCH 15/37] Update style --- R/length.R | 2 +- R/messages.R | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/R/length.R b/R/length.R index b9699b41..21436adc 100644 --- a/R/length.R +++ b/R/length.R @@ -117,7 +117,7 @@ xportr_length <- function(.df, length_log(miss_vars, verbose) - if (length == "metadata"){ + if (length == "metadata") { length_metadata <- metadata[[variable_length]] names(length_metadata) <- metadata[[variable_name]] diff --git a/R/messages.R b/R/messages.R index 8dfce082..e5fea57b 100644 --- a/R/messages.R +++ b/R/messages.R @@ -182,7 +182,8 @@ max_length_msg <- function(max_length, verbose) { xportr_logger( glue( - "{format(max_length[[1]], width = 8)} has a length of {format(as.character(max_length[[2]]), width = 3)} and a length of {format(as.character(max_length[[3]]), width = 3)} in metadata" + "{format(max_length[[1]], width = 8)} has a length of {format(as.character(max_length[[2]]), width = 3)}", + " and a length of {format(as.character(max_length[[3]]), width = 3)} in metadata" ), type = verbose ) From 2ec667f558f49a3f7062f7c88809bd79072f9fff Mon Sep 17 00:00:00 2001 From: Celine Date: Mon, 22 Jan 2024 08:10:56 -0500 Subject: [PATCH 16/37] update style --- R/utils-xportr.R | 1 - tests/testthat/test-length.R | 1 - 2 files changed, 2 deletions(-) diff --git a/R/utils-xportr.R b/R/utils-xportr.R index ce847993..09dfaa6c 100644 --- a/R/utils-xportr.R +++ b/R/utils-xportr.R @@ -396,7 +396,6 @@ check_multiple_var_specs <- function(metadata, #' @export variable_max_length <- function(.df) { - variable_length <- getOption("xportr.length") variable_name <- getOption("xportr.variable_name") diff --git a/tests/testthat/test-length.R b/tests/testthat/test-length.R index 3d108183..25ed989f 100644 --- a/tests/testthat/test-length.R +++ b/tests/testthat/test-length.R @@ -218,7 +218,6 @@ test_that("xportr_length: Gets message when length in metadata longer than data result <- df %>% xportr_length(meta_example, domain = "df", length = "data") %>% expect_message() - }) test_that("xportr_length: Works as expected with only one domain in metadata", { From 2d6b3bb6c9d0dcd49c03e5dd48dee838701ff3e3 Mon Sep 17 00:00:00 2001 From: Celine Piraux <69685640+cpiraux@users.noreply.github.com> Date: Fri, 26 Jan 2024 09:46:01 +0100 Subject: [PATCH 17/37] add match.args MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Veríssimo <211358+averissimo@users.noreply.github.com> --- R/length.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/length.R b/R/length.R index 313bacb0..2253618e 100644 --- a/R/length.R +++ b/R/length.R @@ -75,6 +75,7 @@ xportr_length <- function(.df, length = "metadata", verbose = getOption("xportr.length_verbose", "none"), metacore = deprecated()) { + length <- match.args(length) if (!missing(metacore)) { lifecycle::deprecate_stop( when = "0.3.1.9005", From 53c9cef7a8a6134cc0728750fce08c7bc903f8b1 Mon Sep 17 00:00:00 2001 From: Celine Piraux <69685640+cpiraux@users.noreply.github.com> Date: Fri, 26 Jan 2024 09:46:48 +0100 Subject: [PATCH 18/37] add assertion on parameter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Veríssimo <211358+averissimo@users.noreply.github.com> --- R/utils-xportr.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/utils-xportr.R b/R/utils-xportr.R index b5e93ece..2a062fda 100644 --- a/R/utils-xportr.R +++ b/R/utils-xportr.R @@ -384,6 +384,8 @@ check_multiple_var_specs <- function(metadata, #' @export variable_max_length <- function(.df) { + assert_data_frame(.df) + variable_length <- getOption("xportr.length") variable_name <- getOption("xportr.variable_name") From 86c604f1e2b10f09e9cdbbef140129c5c39afc44 Mon Sep 17 00:00:00 2001 From: Celine Piraux <69685640+cpiraux@users.noreply.github.com> Date: Fri, 26 Jan 2024 09:47:37 +0100 Subject: [PATCH 19/37] add assertion on parameters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Veríssimo <211358+averissimo@users.noreply.github.com> --- R/messages.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/messages.R b/R/messages.R index da86ba81..37b691a9 100644 --- a/R/messages.R +++ b/R/messages.R @@ -190,6 +190,9 @@ var_ord_msg <- function(reordered_vars, moved_vars, verbose) { #' @return Output to Console max_length_msg <- function(max_length, verbose) { + assert_data_frame(max_length) + assert_choice(verbose, choices = .internal_verbose_choices) + if (nrow(max_length) > 0) { cli_h2("Variable length is shorter than the length specified in the metadata.") From cd01549f34e6819b070d56cdbfc22dcf05000bed Mon Sep 17 00:00:00 2001 From: Celine Piraux <69685640+cpiraux@users.noreply.github.com> Date: Fri, 26 Jan 2024 09:47:58 +0100 Subject: [PATCH 20/37] remove blank line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: André Veríssimo <211358+averissimo@users.noreply.github.com> --- R/utils-xportr.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/utils-xportr.R b/R/utils-xportr.R index 2a062fda..f83c9eb3 100644 --- a/R/utils-xportr.R +++ b/R/utils-xportr.R @@ -459,4 +459,3 @@ assert_metadata <- function(metadata, #' Internal choices for verbose option #' @noRd .internal_verbose_choices <- c("none", "warn", "message", "stop") - From da6f80e415d6e48a520d25f1c75952c11e720b86 Mon Sep 17 00:00:00 2001 From: Celine Date: Fri, 26 Jan 2024 03:55:03 -0500 Subject: [PATCH 21/37] run devtools::document() --- man/metadata.Rd | 4 ++-- man/xportr_df_label.Rd | 4 ++-- man/xportr_format.Rd | 4 ++-- man/xportr_label.Rd | 4 ++-- man/xportr_length.Rd | 4 ++-- man/xportr_options.Rd | 2 +- man/xportr_order.Rd | 4 ++-- man/xportr_type.Rd | 4 ++-- man/xportr_write.Rd | 4 ++-- 9 files changed, 17 insertions(+), 17 deletions(-) diff --git a/man/metadata.Rd b/man/metadata.Rd index 658fe0a4..71d2b4cd 100644 --- a/man/metadata.Rd +++ b/man/metadata.Rd @@ -13,8 +13,8 @@ xportr_metadata(.df, metadata = NULL, domain = NULL) 'Metadata' section for details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be -called before hand to set the domain as an attribute of \code{.df}.} +the metadata object. If none is passed, then name of the dataset passed as +.df will be used.} } \value{ \code{.df} dataset with metadata and domain attributes set diff --git a/man/xportr_df_label.Rd b/man/xportr_df_label.Rd index 363c59c4..691de990 100644 --- a/man/xportr_df_label.Rd +++ b/man/xportr_df_label.Rd @@ -13,8 +13,8 @@ xportr_df_label(.df, metadata = NULL, domain = NULL, metacore = deprecated()) details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be -called before hand to set the domain as an attribute of \code{.df}.} +the metadata object. If none is passed, then name of the dataset passed as +.df will be used.} \item{metacore}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Previously used to pass metadata now renamed with \code{metadata}} diff --git a/man/xportr_format.Rd b/man/xportr_format.Rd index dd883554..c1655dbd 100644 --- a/man/xportr_format.Rd +++ b/man/xportr_format.Rd @@ -13,8 +13,8 @@ xportr_format(.df, metadata = NULL, domain = NULL, metacore = deprecated()) 'Metadata' section for details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be -called before hand to set the domain as an attribute of \code{.df}.} +the metadata object. If none is passed, then name of the dataset passed as +.df will be used.} \item{metacore}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Previously used to pass metadata now renamed with \code{metadata}} diff --git a/man/xportr_label.Rd b/man/xportr_label.Rd index 6af7ad9a..4cd7d18c 100644 --- a/man/xportr_label.Rd +++ b/man/xportr_label.Rd @@ -19,8 +19,8 @@ xportr_label( 'Metadata' section for details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be -called before hand to set the domain as an attribute of \code{.df}.} +the metadata object. If none is passed, then name of the dataset passed as +.df will be used.} \item{verbose}{The action this function takes when an action is taken on the dataset or function validation finds an issue. See 'Messaging' section for diff --git a/man/xportr_length.Rd b/man/xportr_length.Rd index 58360219..0e922374 100644 --- a/man/xportr_length.Rd +++ b/man/xportr_length.Rd @@ -20,8 +20,8 @@ xportr_length( 'Metadata' section for details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be -called before hand to set the domain as an attribute of \code{.df}.} +the metadata object. If none is passed, then name of the dataset passed as +.df will be used.} \item{length}{Choose the assigned length from either metadata or data. diff --git a/man/xportr_options.Rd b/man/xportr_options.Rd index 4194aa52..feb212cd 100644 --- a/man/xportr_options.Rd +++ b/man/xportr_options.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/options.R \name{xportr_options} \alias{xportr_options} -\title{Get or set Xportr options} +\title{Get or set xportr options} \usage{ xportr_options(...) } diff --git a/man/xportr_order.Rd b/man/xportr_order.Rd index 50fd7e73..7a796e37 100644 --- a/man/xportr_order.Rd +++ b/man/xportr_order.Rd @@ -19,8 +19,8 @@ xportr_order( 'Metadata' section for details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be -called before hand to set the domain as an attribute of \code{.df}.} +the metadata object. If none is passed, then name of the dataset passed as +.df will be used.} \item{verbose}{The action this function takes when an action is taken on the dataset or function validation finds an issue. See 'Messaging' section for diff --git a/man/xportr_type.Rd b/man/xportr_type.Rd index f8c17945..8886e6d3 100644 --- a/man/xportr_type.Rd +++ b/man/xportr_type.Rd @@ -19,8 +19,8 @@ xportr_type( 'Metadata' section for details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be -called before hand to set the domain as an attribute of \code{.df}.} +the metadata object. If none is passed, then name of the dataset passed as +.df will be used.} \item{verbose}{The action this function takes when an action is taken on the dataset or function validation finds an issue. See 'Messaging' section for diff --git a/man/xportr_write.Rd b/man/xportr_write.Rd index b85f1766..db739e9c 100644 --- a/man/xportr_write.Rd +++ b/man/xportr_write.Rd @@ -23,8 +23,8 @@ used as \code{xpt} name.} 'Metadata' section for details.} \item{domain}{Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset -the metadata object. If none is passed, then \code{\link[=xportr_metadata]{xportr_metadata()}} must be -called before hand to set the domain as an attribute of \code{.df}.} +the metadata object. If none is passed, then name of the dataset passed as +.df will be used.} \item{strict_checks}{If TRUE, xpt validation will report errors and not write out the dataset. If FALSE, xpt validation will report warnings and continue From 9c129cc7e955845b237427be39772d2250366686 Mon Sep 17 00:00:00 2001 From: Celine Date: Fri, 26 Jan 2024 04:27:11 -0500 Subject: [PATCH 22/37] Remove blank line --- R/messages.R | 2 +- R/utils-xportr.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/messages.R b/R/messages.R index 37b691a9..ebe43a69 100644 --- a/R/messages.R +++ b/R/messages.R @@ -192,7 +192,7 @@ var_ord_msg <- function(reordered_vars, moved_vars, verbose) { max_length_msg <- function(max_length, verbose) { assert_data_frame(max_length) assert_choice(verbose, choices = .internal_verbose_choices) - + if (nrow(max_length) > 0) { cli_h2("Variable length is shorter than the length specified in the metadata.") diff --git a/R/utils-xportr.R b/R/utils-xportr.R index f83c9eb3..a6b9077e 100644 --- a/R/utils-xportr.R +++ b/R/utils-xportr.R @@ -385,7 +385,7 @@ check_multiple_var_specs <- function(metadata, variable_max_length <- function(.df) { assert_data_frame(.df) - + variable_length <- getOption("xportr.length") variable_name <- getOption("xportr.variable_name") From 7bbfca89c9831b5ca8039354ba22a79ccbbc4581 Mon Sep 17 00:00:00 2001 From: bs832471 Date: Sun, 28 Jan 2024 22:36:53 +0000 Subject: [PATCH 23/37] fix: #91 typo in match.arg function --- R/length.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/length.R b/R/length.R index 2253618e..4301ff82 100644 --- a/R/length.R +++ b/R/length.R @@ -75,7 +75,7 @@ xportr_length <- function(.df, length = "metadata", verbose = getOption("xportr.length_verbose", "none"), metacore = deprecated()) { - length <- match.args(length) + length <- match.arg(length) if (!missing(metacore)) { lifecycle::deprecate_stop( when = "0.3.1.9005", From 9ac78b029e09198f0168aedf9112d617c59f538b Mon Sep 17 00:00:00 2001 From: bs832471 Date: Mon, 29 Jan 2024 18:52:46 +0000 Subject: [PATCH 24/37] docs: #91 including data option. updating vignettes --- R/length.R | 4 ++-- man/xportr_length.Rd | 2 +- vignettes/deepdive.Rmd | 10 +++++----- vignettes/xportr.Rmd | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/R/length.R b/R/length.R index 4ed30c7e..d7cf31a5 100644 --- a/R/length.R +++ b/R/length.R @@ -72,10 +72,10 @@ xportr_length <- function(.df, metadata = NULL, domain = NULL, - length = "metadata", + length = c("metadata", "data"), verbose = NULL, metacore = deprecated()) { - length <- match.arg(length) + # length <- match.arg(length) if (!missing(metacore)) { lifecycle::deprecate_stop( when = "0.3.1.9005", diff --git a/man/xportr_length.Rd b/man/xportr_length.Rd index dc052d2a..8a5f0d18 100644 --- a/man/xportr_length.Rd +++ b/man/xportr_length.Rd @@ -8,7 +8,7 @@ xportr_length( .df, metadata = NULL, domain = NULL, - length = "metadata", + length = c("metadata", "data"), verbose = NULL, metacore = deprecated() ) diff --git a/vignettes/deepdive.Rmd b/vignettes/deepdive.Rmd index fc9f601d..ae0c070a 100644 --- a/vignettes/deepdive.Rmd +++ b/vignettes/deepdive.Rmd @@ -179,7 +179,7 @@ Each of the core `{xportr}` functions requires several inputs: A valid dataframe ```{r, eval = FALSE} adsl %>% xportr_type(var_spec, "ADSL", "message") %>% - xportr_length(var_spec, "ADSL", "message") %>% + xportr_length(var_spec, "ADSL", "message", length = "metadata") %>% xportr_label(var_spec, "ADSL", "message") %>% xportr_order(var_spec, "ADSL", "message") %>% xportr_format(var_spec, "ADSL") %>% @@ -194,7 +194,7 @@ To help reduce these repetitive calls, we have created `xportr_metadata()`. A us adsl %>% xportr_metadata(var_spec, "ADSL") %>% xportr_type() %>% - xportr_length() %>% + xportr_length(length = "metadata") %>% xportr_label() %>% xportr_order() %>% xportr_format() %>% @@ -310,7 +310,7 @@ str(adsl) ``` ```{r, echo = TRUE} -adsl_length <- xportr_length(.df = adsl, metadata = var_spec, domain = "ADSL", verbose = "warn") +adsl_length <- xportr_length(.df = adsl, metadata = var_spec, domain = "ADSL", verbose = "warn", length = "metadata") ``` Using `xportr_length()` with `verbose = "warn"` we can apply the length column to all the columns in the dataset. The function detects that two variables, `TRTDUR` and `DCREASCD` are missing from the metadata file. Note that the variables have slight misspellings in the dataset and metadata, which is a great catch! However, lengths are still applied with TRTDUR being give a length of 8 and DCREASCD a length of 200. @@ -325,7 +325,7 @@ str(adsl_length) Just like we did for `xportr_type()`, setting `verbose = "stop"` immediately stops R from processing the lengths. Here the function detects the missing variables and will not apply any lengths to the dataset until corrective action is applied. ```{r, echo = TRUE, error = TRUE} -adsl_length <- xportr_length(.df = adsl, metadata = var_spec, domain = "ADSL", verbose = "stop") +adsl_length <- xportr_length(.df = adsl, metadata = var_spec, domain = "ADSL", verbose = "stop", length = "metadata") ``` @@ -426,7 +426,7 @@ It is also note worthy that you can set the dataset label using the `xportr_df_l adsl %>% xportr_metadata(var_spec, "ADSL") %>% xportr_type() %>% - xportr_length() %>% + xportr_length(length = "metadata") %>% xportr_label() %>% xportr_order() %>% xportr_format() %>% diff --git a/vignettes/xportr.Rmd b/vignettes/xportr.Rmd index 2e39f386..53331e9f 100644 --- a/vignettes/xportr.Rmd +++ b/vignettes/xportr.Rmd @@ -274,7 +274,7 @@ Finally, we arrive at exporting the R data frame object as a `xpt` file with `xp ```{r} adsl %>% xportr_type(var_spec, "ADSL", "message") %>% - xportr_length(var_spec, "ADSL", "message") %>% + xportr_length(var_spec, "ADSL", "message", length = "metadata") %>% xportr_label(var_spec, "ADSL", "message") %>% xportr_order(var_spec, "ADSL", "message") %>% xportr_format(var_spec, "ADSL") %>% From d02431ef7792768b8be2e0446f57500db373a6fd Mon Sep 17 00:00:00 2001 From: bs832471 Date: Mon, 29 Jan 2024 19:01:51 +0000 Subject: [PATCH 25/37] fix: #91 global bindings and arguments --- R/length.R | 4 ++-- R/xportr-package.R | 2 +- man/xportr_length.Rd | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/R/length.R b/R/length.R index d7cf31a5..0c3179bb 100644 --- a/R/length.R +++ b/R/length.R @@ -68,11 +68,11 @@ #' length = c(10, 8) #' ) #' -#' adsl <- xportr_length(adsl, metadata, domain = "adsl") +#' adsl <- xportr_length(adsl, metadata, domain = "adsl", length = "metadata") xportr_length <- function(.df, metadata = NULL, domain = NULL, - length = c("metadata", "data"), + length = "metadata", verbose = NULL, metacore = deprecated()) { # length <- match.arg(length) diff --git a/R/xportr-package.R b/R/xportr-package.R index 7ffeafcb..6168f506 100644 --- a/R/xportr-package.R +++ b/R/xportr-package.R @@ -117,7 +117,7 @@ globalVariables(c( "abbr_parsed", "abbr_stem", "adj_orig", "adj_parsed", "col_pos", "dict_varname", "lower_original_varname", "my_minlength", "num_st_ind", "original_varname", "renamed_n", "renamed_var", "use_bundle", "viable_start", "type.x", "type.y", - "variable" + "variable", "length.x", "lenght.y" )) # The following block is used by usethis to automatically manage diff --git a/man/xportr_length.Rd b/man/xportr_length.Rd index 8a5f0d18..c5fc1bd5 100644 --- a/man/xportr_length.Rd +++ b/man/xportr_length.Rd @@ -8,7 +8,7 @@ xportr_length( .df, metadata = NULL, domain = NULL, - length = c("metadata", "data"), + length = "metadata", verbose = NULL, metacore = deprecated() ) @@ -89,5 +89,5 @@ metadata <- data.frame( length = c(10, 8) ) -adsl <- xportr_length(adsl, metadata, domain = "adsl") +adsl <- xportr_length(adsl, metadata, domain = "adsl", length = "metadata") } From b8469998afe22f7aaa98e37b53f5e2a551c3e6c1 Mon Sep 17 00:00:00 2001 From: bs832471 Date: Mon, 29 Jan 2024 19:19:16 +0000 Subject: [PATCH 26/37] chore: #91 nolint commented code --- R/length.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/length.R b/R/length.R index 0c3179bb..fddcbf67 100644 --- a/R/length.R +++ b/R/length.R @@ -75,7 +75,7 @@ xportr_length <- function(.df, length = "metadata", verbose = NULL, metacore = deprecated()) { - # length <- match.arg(length) + # length <- match.arg(length) # nolint if (!missing(metacore)) { lifecycle::deprecate_stop( when = "0.3.1.9005", From 548821682e48fcb6c48b611123abd3f59a7ee9b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Tue, 30 Jan 2024 19:28:29 +0100 Subject: [PATCH 27/37] fix: use match.arg for xportr_lenght lenght --- R/length.R | 4 ++-- man/xportr_length.Rd | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R/length.R b/R/length.R index fddcbf67..de6fa9a8 100644 --- a/R/length.R +++ b/R/length.R @@ -72,10 +72,10 @@ xportr_length <- function(.df, metadata = NULL, domain = NULL, - length = "metadata", + length = c("metadata", "data"), verbose = NULL, metacore = deprecated()) { - # length <- match.arg(length) # nolint + length <- match.arg(length) if (!missing(metacore)) { lifecycle::deprecate_stop( when = "0.3.1.9005", diff --git a/man/xportr_length.Rd b/man/xportr_length.Rd index c5fc1bd5..5f944084 100644 --- a/man/xportr_length.Rd +++ b/man/xportr_length.Rd @@ -8,7 +8,7 @@ xportr_length( .df, metadata = NULL, domain = NULL, - length = "metadata", + length = c("metadata", "data"), verbose = NULL, metacore = deprecated() ) From 89f421ab33550edf7c5de46ff062dd7f4013a0bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Tue, 30 Jan 2024 19:46:50 +0100 Subject: [PATCH 28/37] fix: correct order of parameters on vignettes --- vignettes/deepdive.Rmd | 2 +- vignettes/xportr.Rmd | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/vignettes/deepdive.Rmd b/vignettes/deepdive.Rmd index ae0c070a..b245ad15 100644 --- a/vignettes/deepdive.Rmd +++ b/vignettes/deepdive.Rmd @@ -179,7 +179,7 @@ Each of the core `{xportr}` functions requires several inputs: A valid dataframe ```{r, eval = FALSE} adsl %>% xportr_type(var_spec, "ADSL", "message") %>% - xportr_length(var_spec, "ADSL", "message", length = "metadata") %>% + xportr_length(var_spec, "ADSL", verbose = "message") %>% xportr_label(var_spec, "ADSL", "message") %>% xportr_order(var_spec, "ADSL", "message") %>% xportr_format(var_spec, "ADSL") %>% diff --git a/vignettes/xportr.Rmd b/vignettes/xportr.Rmd index 53331e9f..7ea6eaa3 100644 --- a/vignettes/xportr.Rmd +++ b/vignettes/xportr.Rmd @@ -186,7 +186,7 @@ str(adsl) No lengths have been applied to the variables as seen in the printout - the lengths would be in the `attr()` part of each variables. Let's now use `xportr_length()` to apply our lengths from the specification file. ```{r} -adsl_length <- adsl %>% xportr_length(var_spec, domain = "ADSL", "message") +adsl_length <- adsl %>% xportr_length(var_spec, domain = "ADSL", verbose = "message") ``` @@ -274,7 +274,7 @@ Finally, we arrive at exporting the R data frame object as a `xpt` file with `xp ```{r} adsl %>% xportr_type(var_spec, "ADSL", "message") %>% - xportr_length(var_spec, "ADSL", "message", length = "metadata") %>% + xportr_length(var_spec, "ADSL", verbose = "message") %>% xportr_label(var_spec, "ADSL", "message") %>% xportr_order(var_spec, "ADSL", "message") %>% xportr_format(var_spec, "ADSL") %>% From d41682b8bb069f8f9601ff49d47013e9cd6b8421 Mon Sep 17 00:00:00 2001 From: Celine Date: Wed, 7 Feb 2024 14:31:15 -0500 Subject: [PATCH 29/37] change argument name length to length_source --- R/length.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/length.R b/R/length.R index 4b47831c..d43ed4f3 100644 --- a/R/length.R +++ b/R/length.R @@ -11,7 +11,7 @@ #' @param domain Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset #' the metadata object. If none is passed, then name of the dataset passed as #' .df will be used. -#' @param length Choose the assigned length from either metadata or data. +#' @param length_source Choose the assigned length from either metadata or data. #' #' If `"metadata"` is specified, the assigned length is from the metadata length. #' If `"data"` is specified, the assigned length is determined by the calculated maximum data length. @@ -72,10 +72,10 @@ xportr_length <- function(.df, metadata = NULL, domain = NULL, - length = c("metadata", "data"), + length_source = c("metadata", "data"), verbose = NULL, metacore = deprecated()) { - length <- match.arg(length) + length_source <- match.arg(length_source) if (!missing(metacore)) { lifecycle::deprecate_stop( when = "0.3.1.9005", @@ -123,7 +123,7 @@ xportr_length <- function(.df, length_log(miss_vars, verbose) - if (length == "metadata") { + if (length_source == "metadata") { length_metadata <- metadata[[variable_length]] names(length_metadata) <- metadata[[variable_name]] @@ -137,7 +137,7 @@ xportr_length <- function(.df, } # Assign length from data - if (length == "data") { + if (length_source == "data") { var_length_max <- variable_max_length(.df) length_data <- var_length_max[[variable_length]] From be567999e3caa9c232cf6db54571da3c05e20165 Mon Sep 17 00:00:00 2001 From: Celine Date: Wed, 7 Feb 2024 14:38:35 -0500 Subject: [PATCH 30/37] change order of argument --- R/length.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/R/length.R b/R/length.R index d43ed4f3..15acba9d 100644 --- a/R/length.R +++ b/R/length.R @@ -11,15 +11,15 @@ #' @param domain Appropriate CDSIC dataset name, e.g. ADAE, DM. Used to subset #' the metadata object. If none is passed, then name of the dataset passed as #' .df will be used. +#' @param verbose The action this function takes when an action is taken on the +#' dataset or function validation finds an issue. See 'Messaging' section for +#' details. Options are 'stop', 'warn', 'message', and 'none' #' @param length_source Choose the assigned length from either metadata or data. #' #' If `"metadata"` is specified, the assigned length is from the metadata length. #' If `"data"` is specified, the assigned length is determined by the calculated maximum data length. #' #' *Permitted Values*: `"metadata"`, `"data"` -#' @param verbose The action this function takes when an action is taken on the -#' dataset or function validation finds an issue. See 'Messaging' section for -#' details. Options are 'stop', 'warn', 'message', and 'none' #' @param metacore `r lifecycle::badge("deprecated")` Previously used to pass #' metadata now renamed with `metadata` #' @@ -72,8 +72,8 @@ xportr_length <- function(.df, metadata = NULL, domain = NULL, - length_source = c("metadata", "data"), verbose = NULL, + length_source = c("metadata", "data"), metacore = deprecated()) { length_source <- match.arg(length_source) if (!missing(metacore)) { From 13dd326989bb3baaf71226551e1f564d5e85faf3 Mon Sep 17 00:00:00 2001 From: Celine Date: Wed, 7 Feb 2024 14:59:28 -0500 Subject: [PATCH 31/37] Added description in NEWS.md --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 3a5891c0..dafe5599 100644 --- a/NEWS.md +++ b/NEWS.md @@ -21,6 +21,8 @@ * All core functions can be run together by using new function `xportr()` (#137) +*New argument in `xportr_length()` allows selection between the length from metadata, as previously done, or from the calculated maximum length per variable when `length_source` is set to “data” (#91) + ## Documentation ## Deprecation and Breaking Changes From d811cb7fe1b2a27f964bc3ba1e9f0c86c14ed2a8 Mon Sep 17 00:00:00 2001 From: Celine Date: Wed, 7 Feb 2024 15:01:46 -0500 Subject: [PATCH 32/37] Update documentation --- man/xportr_length.Rd | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/man/xportr_length.Rd b/man/xportr_length.Rd index 6a964405..b288f984 100644 --- a/man/xportr_length.Rd +++ b/man/xportr_length.Rd @@ -8,8 +8,8 @@ xportr_length( .df, metadata = NULL, domain = NULL, - length = c("metadata", "data"), verbose = NULL, + length_source = c("metadata", "data"), metacore = deprecated() ) } @@ -23,17 +23,17 @@ xportr_length( the metadata object. If none is passed, then name of the dataset passed as .df will be used.} -\item{length}{Choose the assigned length from either metadata or data. +\item{verbose}{The action this function takes when an action is taken on the +dataset or function validation finds an issue. See 'Messaging' section for +details. Options are 'stop', 'warn', 'message', and 'none'} + +\item{length_source}{Choose the assigned length from either metadata or data. If \code{"metadata"} is specified, the assigned length is from the metadata length. If \code{"data"} is specified, the assigned length is determined by the calculated maximum data length. \emph{Permitted Values}: \code{"metadata"}, \code{"data"}} -\item{verbose}{The action this function takes when an action is taken on the -dataset or function validation finds an issue. See 'Messaging' section for -details. Options are 'stop', 'warn', 'message', and 'none'} - \item{metacore}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Previously used to pass metadata now renamed with \code{metadata}} } From 7bf3770f84533db3225255226b60980972829900 Mon Sep 17 00:00:00 2001 From: Celine Date: Sun, 11 Feb 2024 02:41:44 -0500 Subject: [PATCH 33/37] Change argument name to source_length in test-length --- tests/testthat/test-length.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-length.R b/tests/testthat/test-length.R index 305a6e13..7235cb97 100644 --- a/tests/testthat/test-length.R +++ b/tests/testthat/test-length.R @@ -206,17 +206,17 @@ df <- data.frame( test_that("xportr_length: length assigned as expected from metadata or data", { result <- df %>% - xportr_length(meta_example, domain = "df", length = "metadata") %>% + xportr_length(meta_example, domain = "df", length_source = "metadata") %>% expect_attr_width(c(10, 8)) result <- df %>% - xportr_length(meta_example, domain = "df", length = "data") %>% + xportr_length(meta_example, domain = "df", length_source = "data") %>% expect_attr_width(c(3, 8)) }) test_that("xportr_length: Gets message when length in metadata longer than data length", { result <- df %>% - xportr_length(meta_example, domain = "df", length = "data") %>% + xportr_length(meta_example, domain = "df", length_source = "data") %>% expect_message() }) From 758846a4b147b77d41a64941cca321370e730be4 Mon Sep 17 00:00:00 2001 From: Celine Date: Sun, 11 Feb 2024 02:52:49 -0500 Subject: [PATCH 34/37] change argument name to length_source --- R/length.R | 2 +- man/xportr_length.Rd | 2 +- vignettes/deepdive.Rmd | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/length.R b/R/length.R index 15acba9d..d87fe2b2 100644 --- a/R/length.R +++ b/R/length.R @@ -68,7 +68,7 @@ #' length = c(10, 8) #' ) #' -#' adsl <- xportr_length(adsl, metadata, domain = "adsl", length = "metadata") +#' adsl <- xportr_length(adsl, metadata, domain = "adsl", length_source = "metadata") xportr_length <- function(.df, metadata = NULL, domain = NULL, diff --git a/man/xportr_length.Rd b/man/xportr_length.Rd index b288f984..8d034eb8 100644 --- a/man/xportr_length.Rd +++ b/man/xportr_length.Rd @@ -89,5 +89,5 @@ metadata <- data.frame( length = c(10, 8) ) -adsl <- xportr_length(adsl, metadata, domain = "adsl", length = "metadata") +adsl <- xportr_length(adsl, metadata, domain = "adsl", length_source = "metadata") } diff --git a/vignettes/deepdive.Rmd b/vignettes/deepdive.Rmd index b245ad15..ec342ba1 100644 --- a/vignettes/deepdive.Rmd +++ b/vignettes/deepdive.Rmd @@ -194,7 +194,7 @@ To help reduce these repetitive calls, we have created `xportr_metadata()`. A us adsl %>% xportr_metadata(var_spec, "ADSL") %>% xportr_type() %>% - xportr_length(length = "metadata") %>% + xportr_length(length_source = "metadata") %>% xportr_label() %>% xportr_order() %>% xportr_format() %>% @@ -310,7 +310,7 @@ str(adsl) ``` ```{r, echo = TRUE} -adsl_length <- xportr_length(.df = adsl, metadata = var_spec, domain = "ADSL", verbose = "warn", length = "metadata") +adsl_length <- xportr_length(.df = adsl, metadata = var_spec, domain = "ADSL", verbose = "warn", length_source = "metadata") ``` Using `xportr_length()` with `verbose = "warn"` we can apply the length column to all the columns in the dataset. The function detects that two variables, `TRTDUR` and `DCREASCD` are missing from the metadata file. Note that the variables have slight misspellings in the dataset and metadata, which is a great catch! However, lengths are still applied with TRTDUR being give a length of 8 and DCREASCD a length of 200. @@ -325,7 +325,7 @@ str(adsl_length) Just like we did for `xportr_type()`, setting `verbose = "stop"` immediately stops R from processing the lengths. Here the function detects the missing variables and will not apply any lengths to the dataset until corrective action is applied. ```{r, echo = TRUE, error = TRUE} -adsl_length <- xportr_length(.df = adsl, metadata = var_spec, domain = "ADSL", verbose = "stop", length = "metadata") +adsl_length <- xportr_length(.df = adsl, metadata = var_spec, domain = "ADSL", verbose = "stop", length_source = "metadata") ``` @@ -426,7 +426,7 @@ It is also note worthy that you can set the dataset label using the `xportr_df_l adsl %>% xportr_metadata(var_spec, "ADSL") %>% xportr_type() %>% - xportr_length(length = "metadata") %>% + xportr_length(length_source = "metadata") %>% xportr_label() %>% xportr_order() %>% xportr_format() %>% From cc9d0dfe1b2e20831d2af740b4fcce1b91f2fdd1 Mon Sep 17 00:00:00 2001 From: Celine Piraux <69685640+cpiraux@users.noreply.github.com> Date: Sun, 11 Feb 2024 08:54:49 +0100 Subject: [PATCH 35/37] Update NEWS.md Co-authored-by: Ben Straub --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index dafe5599..f8e0e33c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -21,7 +21,7 @@ * All core functions can be run together by using new function `xportr()` (#137) -*New argument in `xportr_length()` allows selection between the length from metadata, as previously done, or from the calculated maximum length per variable when `length_source` is set to “data” (#91) +* New argument in `xportr_length()` allows selection between the length from metadata, as previously done, or from the calculated maximum length per variable when `length_source` is set to “data” (#91) ## Documentation From c7a410b56e3239f556543462d17848a75be1d731 Mon Sep 17 00:00:00 2001 From: Celine Date: Sun, 11 Feb 2024 03:08:10 -0500 Subject: [PATCH 36/37] Reduce line length less than 120 characters --- vignettes/deepdive.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/deepdive.Rmd b/vignettes/deepdive.Rmd index ec342ba1..8bd27f1d 100644 --- a/vignettes/deepdive.Rmd +++ b/vignettes/deepdive.Rmd @@ -325,7 +325,7 @@ str(adsl_length) Just like we did for `xportr_type()`, setting `verbose = "stop"` immediately stops R from processing the lengths. Here the function detects the missing variables and will not apply any lengths to the dataset until corrective action is applied. ```{r, echo = TRUE, error = TRUE} -adsl_length <- xportr_length(.df = adsl, metadata = var_spec, domain = "ADSL", verbose = "stop", length_source = "metadata") +adsl_length <- xportr_length(.df = adsl, metadata = var_spec, domain = "ADSL", verbose = "stop") ``` From 98c075f025de11d56d5ce2bd5bbd3910e49a9157 Mon Sep 17 00:00:00 2001 From: Celine Date: Sun, 11 Feb 2024 03:27:41 -0500 Subject: [PATCH 37/37] lint:reduce lenght of line --- vignettes/deepdive.Rmd | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/vignettes/deepdive.Rmd b/vignettes/deepdive.Rmd index 8bd27f1d..ae920ef1 100644 --- a/vignettes/deepdive.Rmd +++ b/vignettes/deepdive.Rmd @@ -310,7 +310,13 @@ str(adsl) ``` ```{r, echo = TRUE} -adsl_length <- xportr_length(.df = adsl, metadata = var_spec, domain = "ADSL", verbose = "warn", length_source = "metadata") +adsl_length <- xportr_length( + .df = adsl, + metadata = var_spec, + domain = "ADSL", + verbose = "warn", + length_source = "metadata" +) ``` Using `xportr_length()` with `verbose = "warn"` we can apply the length column to all the columns in the dataset. The function detects that two variables, `TRTDUR` and `DCREASCD` are missing from the metadata file. Note that the variables have slight misspellings in the dataset and metadata, which is a great catch! However, lengths are still applied with TRTDUR being give a length of 8 and DCREASCD a length of 200. @@ -325,7 +331,13 @@ str(adsl_length) Just like we did for `xportr_type()`, setting `verbose = "stop"` immediately stops R from processing the lengths. Here the function detects the missing variables and will not apply any lengths to the dataset until corrective action is applied. ```{r, echo = TRUE, error = TRUE} -adsl_length <- xportr_length(.df = adsl, metadata = var_spec, domain = "ADSL", verbose = "stop") +adsl_length <- xportr_length( + .df = adsl, + metadata = var_spec, + domain = "ADSL", + verbose = "stop", + length_source = "metadata" +) ```