From 7173caa9be4394e9a25b330dcaaa934ede768a05 Mon Sep 17 00:00:00 2001 From: Ming Yang Date: Mon, 14 Oct 2024 18:43:41 +0800 Subject: [PATCH] Fix styler issues. --- R/load_data.R | 42 +++++++++++++-------------- R/load_rds.R | 16 +++++----- R/load_sas.R | 16 +++++----- R/load_xpt.R | 16 +++++----- R/utils.R | 8 ++--- tests/testthat/setup.R | 4 +-- tests/testthat/test-data_integrity.R | 18 ++++++------ tests/testthat/test-default_dir.R | 14 ++++----- tests/testthat/test-file_extensions.R | 12 ++++---- tests/testthat/test-file_names.R | 10 +++---- tests/testthat/test-file_type.R | 14 ++++----- tests/testthat/test-meta_data.R | 22 +++++++------- tests/testthat/test-prefer_sas.R | 18 ++++++------ vignettes/integration-guide.Rmd | 2 +- 14 files changed, 106 insertions(+), 106 deletions(-) diff --git a/R/load_data.R b/R/load_data.R index 0d4194f..d9ac839 100644 --- a/R/load_data.R +++ b/R/load_data.R @@ -22,13 +22,13 @@ #' @importFrom lifecycle deprecate_warn load_data <- function(sub_dir = NULL, file_names, use_wd = FALSE, prefer_sas = FALSE) { lifecycle::deprecate_warn("3.0.0", "load_data()", "read_data()") - + if (is.null(file_names)) { stop("Usage: load_data: file_names: Must supply at least one file name") } - + study_path <- "" # will be built using args - + if (is.null(sub_dir)) { study_path <- getwd() } else { @@ -38,10 +38,10 @@ load_data <- function(sub_dir = NULL, file_names, use_wd = FALSE, prefer_sas = F study_path <- file.path(get_cre_path(), sub_dir) } } - + # create the output data_list <- create_data_list(study_path, file_names, prefer_sas) # nolint - + return(data_list) } @@ -75,39 +75,39 @@ create_data_list <- function(file_path, file_names, prefer_sas) { if (prefer_sas) { extensions <- c("", ".sas7bdat", ".rds") } - + file_name_to_load <- NULL - + candidates <- list.files(file_path) uppercase_candidates <- Map(toupper, candidates) - + for (ext in extensions) { # Case insensitive file name match uppercase_file_name <- toupper(paste0(x, ext)) - + match_count <- sum(uppercase_candidates == uppercase_file_name) if (match_count > 1) { stop(paste("create_data_list(): More than one case-insensitive file name match for", file_path, x)) } - + index <- match(uppercase_file_name, uppercase_candidates) if (!is.na(index)) { file_name_to_load <- candidates[[index]] break } } - + if (is.null(file_name_to_load)) { stop(paste("create_data_list(): No RDS or SAS files found for", file_path, x)) } - + output <- read_file(file_path, file_name_to_load) - + return(output) }) - + names(data_list) <- file_names - + return(data_list) } @@ -118,29 +118,29 @@ create_data_list <- function(file_path, file_names, prefer_sas) { #' @return a data object with an extra attribute of metadata read_file <- function(file_path, file_name) { ext <- tools::file_ext(file_name) - + if (!(toupper(ext) %in% c("RDS", "SAS7BDAT"))) { stop("Usage error: read_file: file_name: file must either be RDS or SAS7BDAT.") } - + is_rds <- toupper(ext) == "RDS" - + file <- file.path(file_path, file_name) file_name <- tools::file_path_sans_ext(file_name) - + # grab file info meta <- file.info(file)[1L:6L] meta[["path"]] <- row.names(meta) meta[["file_name"]] <- file_name meta <- data.frame(meta, stringsAsFactors = FALSE) row.names(meta) <- NULL - + if (is_rds) { out <- readRDS(file) } else { out <- haven::read_sas(file) } attr(out, "meta") <- meta - + return(out) } diff --git a/R/load_rds.R b/R/load_rds.R index a1a9008..8211564 100644 --- a/R/load_rds.R +++ b/R/load_rds.R @@ -9,37 +9,37 @@ #' temp_dir <- tempdir() #' adsl_rds_file <- file.path(temp_dir, "adsl.rds") #' adae_rds_file <- file.path(temp_dir, "adae.rds") -#' +#' #' # Write example data to RDS files #' saveRDS(pharmaverseadam::adsl, adsl_rds_file) #' saveRDS(pharmaverseadam::adae, adae_rds_file) -#' +#' #' # Load RDS files #' rds_data_list <- load_rds(c(adsl_rds_file, adae_rds_file)) -#' +#' #' # Clean up #' unlink(c(adsl_rds_file, adae_rds_file)) #' @export load_rds <- function(files) { # Check if files is a character vector checkmate::assert_character(files) - + # Read each file and add metadata data_list <- lapply(files, function(file) { # Check if file exists checkmate::assert_file_exists(file) # Check if file is an RDS file check_file_ext(file, extension = "rds") - + # Read RDS file data <- readRDS(file) - + # Get file info and add to data as an attribute attr(data, "meta") <- file_info(file) - + return(data) }) - + # Set names of data_list to the file names names(data_list) <- basename(files) diff --git a/R/load_sas.R b/R/load_sas.R index 01c7a4c..5707afa 100644 --- a/R/load_sas.R +++ b/R/load_sas.R @@ -9,37 +9,37 @@ #' temp_dir <- tempdir() #' adsl_sas_file <- file.path(temp_dir, "adsl.sas7bdat") #' adae_sas_file <- file.path(temp_dir, "adae.sas7bdat") -#' +#' #' # Write example data to SAS files #' haven::write_sas(pharmaverseadam::adsl, adsl_sas_file) #' haven::write_sas(pharmaverseadam::adae, adae_sas_file) -#' +#' #' # Load SAS files #' sas_data_list <- load_sas(c(adsl_sas_file, adae_sas_file)) -#' +#' #' # Clean up #' unlink(c(adsl_sas_file, adae_sas_file)) #' @export load_sas <- function(files) { # Check if files is a character vector checkmate::assert_character(files) - + # Read each file and add metadata data_list <- lapply(files, function(file) { # Check if file exists checkmate::assert_file_exists(file) # Check if file is a SAS file check_file_ext(file, extension = "sas7bdat") - + # Read SAS file data <- haven::read_sas(file) - + # Get file info and add to data as an attribute attr(data, "meta") <- file_info(file) - + return(data) }) - + # Set names of data_list to the file names names(data_list) <- basename(files) return(data_list) diff --git a/R/load_xpt.R b/R/load_xpt.R index debf34b..2c8644e 100644 --- a/R/load_xpt.R +++ b/R/load_xpt.R @@ -9,16 +9,16 @@ #' temp_dir <- tempdir() #' adsl_xpt_file <- file.path(temp_dir, "adsl.xpt") #' adae_xpt_file <- file.path(temp_dir, "adae.xpt") -#' +#' #' # Write example data to XPT files #' haven::write_xpt(pharmaverseadam::adsl, adsl_xpt_file) #' haven::write_xpt(pharmaverseadam::adae, adae_xpt_file) -#' +#' #' # Load XPT files #' xpt_data_list <- load_xpt(c(adsl_xpt_file, adae_xpt_file)) -#' +#' #' # Clean up -#' unlink(c(adsl_xpt_file, adae_xpt_file)) +#' unlink(c(adsl_xpt_file, adae_xpt_file)) #' @export load_xpt <- function(files) { # Check if files is a character vector @@ -30,16 +30,16 @@ load_xpt <- function(files) { checkmate::assert_file_exists(file) # Check if file is an XPT file check_file_ext(file, extension = "xpt") - + # Read XPT file data <- haven::read_xpt(file) - + # Get file info and add to data as an attribute attr(data, "meta") <- file_info(file) - + return(data) }) - + # Set names of data_list to the file names names(data_list) <- basename(files) diff --git a/R/utils.R b/R/utils.R index 6204502..01fa560 100644 --- a/R/utils.R +++ b/R/utils.R @@ -9,7 +9,7 @@ check_file_ext <- function(file, extension) { # Check input types checkmate::assert_string(file) checkmate::assert_string(extension) - + # Extract file extension (case-insensitive) file_ext <- tolower(tools::file_ext(file)) @@ -18,7 +18,7 @@ check_file_ext <- function(file, extension) { # Check that the file extension is one of the allowed choices checkmate::assert_choice(file_ext, choices = c("rds", "sas7bdat", "xpt")) - + # Compare with the given extension (case-insensitive) return(file_ext == tolower(extension)) } @@ -39,7 +39,7 @@ file_info <- function(file) { # Get the path from the rownames path <- rownames(info) - + # Check file and path are the same checkmate::assert_true(file == path) @@ -49,7 +49,7 @@ file_info <- function(file) { # Convert to list to remove row names info <- as.list(info) - + # Return the file information as a list return(info) } diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R index ac6ce9e..1580a08 100644 --- a/tests/testthat/setup.R +++ b/tests/testthat/setup.R @@ -47,7 +47,7 @@ Sys.setenv(RXD_DATA = temp_dir) # Load RDS data via load_data() lifecycle::expect_deprecated( iris_data_rds <- dv.loader::load_data( - sub_dir = ".", + sub_dir = ".", file_names = "iris.rds" ) ) @@ -55,7 +55,7 @@ lifecycle::expect_deprecated( # Load SAS data via load_data() lifecycle::expect_deprecated( iris_data_sas <- dv.loader::load_data( - sub_dir = ".", + sub_dir = ".", file_names = "iris.sas7bdat" ) ) diff --git a/tests/testthat/test-data_integrity.R b/tests/testthat/test-data_integrity.R index f845987..38f37f4 100644 --- a/tests/testthat/test-data_integrity.R +++ b/tests/testthat/test-data_integrity.R @@ -1,17 +1,17 @@ test_that( desc = vdoc[["add_spec"]]( - desc = "Ensures that the data integrity is maintained across different file types and loading methods, + desc = "Ensures that the data integrity is maintained across different file types and loading methods, comparing the loaded data against a known reference dataset.", spec = specs$data_integrity - ), - code = { + ), + code = { # load_data(): check that the RDS file is loaded correctly expect_equal( object = iris_data_rds[["iris.rds"]], expected = iris_data, ignore_attr = TRUE ) - + # load_data(): check that the SAS file is loaded correctly expect_equal( object = iris_data_sas[["iris.sas7bdat"]], @@ -21,21 +21,21 @@ test_that( # load_rds(): check that the RDS file is loaded correctly expect_equal( - object = iris_rds[["iris.rds"]], + object = iris_rds[["iris.rds"]], expected = iris_data, ignore_attr = TRUE ) - + # load_sas(): check that the SAS file is loaded correctly expect_equal( - object = iris_sas[["iris.sas7bdat"]], + object = iris_sas[["iris.sas7bdat"]], expected = iris_data, ignore_attr = TRUE ) - + # load_xpt(): check that the XPT file is loaded correctly expect_equal( - object = iris_xpt[["iris.xpt"]], + object = iris_xpt[["iris.xpt"]], expected = iris_data, ignore_attr = TRUE ) diff --git a/tests/testthat/test-default_dir.R b/tests/testthat/test-default_dir.R index d9e8a12..44bea9d 100644 --- a/tests/testthat/test-default_dir.R +++ b/tests/testthat/test-default_dir.R @@ -1,23 +1,23 @@ -test_that( +test_that( desc = vdoc[["add_spec"]]( - desc = "Verifies that load_data() can correctly locate and load data files using + desc = "Verifies that load_data() can correctly locate and load data files using relative paths from the current working directory.", spec = specs$default_dir - ), + ), code = { # Save the current working directory old_wd <- getwd() - + # Change the working directory to the temporary directory setwd(temp_dir) - + lifecycle::expect_deprecated( # load_data(): load the RDS file with use_wd = TRUE data1 <- dv.loader::load_data( sub_dir = ".", file_names = "iris.rds", use_wd = TRUE - ) + ) ) lifecycle::expect_deprecated( @@ -26,7 +26,7 @@ test_that( sub_dir = ".", file_names = "iris.sas7bdat", use_wd = TRUE - ) + ) ) # Expect that the RDS file is loaded diff --git a/tests/testthat/test-file_extensions.R b/tests/testthat/test-file_extensions.R index dd9a6dd..da53f60 100644 --- a/tests/testthat/test-file_extensions.R +++ b/tests/testthat/test-file_extensions.R @@ -2,27 +2,27 @@ test_that( desc = vdoc[["add_spec"]]( desc = "appropriate error messages are thrown when attempting to load or read files without valid extensions", spec = specs$file_extensions - ), + ), code = { # load_data(): load data with no file extension lifecycle::expect_deprecated( dv.loader::load_data(sub_dir = ".", file_names = "iris") ) |> expect_error("file must either be RDS or SAS7BDAT") - + # Expected error message error_msg <- "Assertion on 'file_ext != \"\"' failed: Must be TRUE." # load_rds(): expect an error when the file extension is empty - dv.loader::load_rds(files = iris_file) |> - expect_error(error_msg) + dv.loader::load_rds(files = iris_file) |> + expect_error(error_msg) # load_sas(): expect an error when the file extension is empty - dv.loader::load_sas(files = iris_file) |> + dv.loader::load_sas(files = iris_file) |> expect_error(error_msg) # load_xpt(): expect an error when the file extension is empty - dv.loader::load_xpt(files = iris_file) |> + dv.loader::load_xpt(files = iris_file) |> expect_error(error_msg) } ) diff --git a/tests/testthat/test-file_names.R b/tests/testthat/test-file_names.R index 4212ae9..7e8066f 100644 --- a/tests/testthat/test-file_names.R +++ b/tests/testthat/test-file_names.R @@ -2,23 +2,23 @@ test_that( desc = vdoc[["add_spec"]]( desc = "appropriate error messages are thrown when the required 'file_names' argument is not provided", spec = specs$file_names - ), + ), code = { lifecycle::expect_deprecated( - # load_data(): expect an error when the file_names argument is missing + # load_data(): expect an error when the file_names argument is missing load_data(sub_dir = ".") ) |> expect_error('argument "file_names" is missing, with no default') - + # Expected error message error_msg <- 'argument "files" is missing, with no default' # load_rds(): expect an error when the files argument is missing expect_error(dv.loader::load_rds(), error_msg) - + # load_sas(): expect an error when the files argument is missing expect_error(dv.loader::load_sas(), error_msg) - + # load_xpt(): expect an error when the files argument is missing expect_error(dv.loader::load_xpt(), error_msg) } diff --git a/tests/testthat/test-file_type.R b/tests/testthat/test-file_type.R index b77411e..6489118 100644 --- a/tests/testthat/test-file_type.R +++ b/tests/testthat/test-file_type.R @@ -1,25 +1,25 @@ test_that( desc = vdoc[["add_spec"]]( - desc = "appropriate error messages are thrown when attempting to load or + desc = "appropriate error messages are thrown when attempting to load or read file with types that are not supported.", spec = specs$file_type - ), - code = { + ), + code = { # load_data(): expect an error when the file type is not supported lifecycle::expect_deprecated( dv.loader::load_data(sub_dir = ".", file_names = "iris.txt") ) |> expect_error("file must either be RDS or SAS7BDAT") - + # Expected error message error_msg <- "Must be element of set \\{'rds','sas7bdat','xpt'\\}, but is 'txt'." - + # load_rds(): expect an error when the file extension is not supported expect_error(dv.loader::load_rds(files = iris_txt_file), error_msg) - + # load_sas(): expect an error when the file extension is not supported expect_error(dv.loader::load_sas(files = iris_txt_file), error_msg) - + # load_xpt(): expect an error when the file extension is not supported expect_error(dv.loader::load_xpt(files = iris_txt_file), error_msg) } diff --git a/tests/testthat/test-meta_data.R b/tests/testthat/test-meta_data.R index 126862d..8f27d8a 100644 --- a/tests/testthat/test-meta_data.R +++ b/tests/testthat/test-meta_data.R @@ -1,6 +1,6 @@ test_that( desc = vdoc[["add_spec"]]( - desc = "load_data(), load_rds(), load_sas(), and load_xpt() functions correctly extract and + desc = "load_data(), load_rds(), load_sas(), and load_xpt() functions correctly extract and attach metadata of the file to the loaded data", spec = specs$meta_data ), @@ -9,34 +9,34 @@ test_that( rds_file_info <- file.info(iris_rds_file, extra_cols = FALSE) sas_file_info <- file.info(iris_sas_file, extra_cols = FALSE) xpt_file_info <- file.info(iris_xpt_file, extra_cols = FALSE) - + # load_data(): check metadata for RDS file expect_equal( - as.list(attr(iris_data_rds[["iris.rds"]], "meta")[names(rds_file_info)]), + as.list(attr(iris_data_rds[["iris.rds"]], "meta")[names(rds_file_info)]), as.list(rds_file_info) ) - + # load_data(): check metadata for SAS file expect_equal( - as.list(attr(iris_data_sas[["iris.sas7bdat"]], "meta")[names(sas_file_info)]), + as.list(attr(iris_data_sas[["iris.sas7bdat"]], "meta")[names(sas_file_info)]), as.list(sas_file_info) ) - + # load_rds(): check metadata for RDS file expect_equal( - as.list(attr(iris_rds[["iris.rds"]], "meta")[names(rds_file_info)]), + as.list(attr(iris_rds[["iris.rds"]], "meta")[names(rds_file_info)]), as.list(rds_file_info) ) - + # load_sas(): check metadata for SAS file expect_equal( - as.list(attr(iris_sas[["iris.sas7bdat"]], "meta")[names(sas_file_info)]), + as.list(attr(iris_sas[["iris.sas7bdat"]], "meta")[names(sas_file_info)]), as.list(sas_file_info) ) - + # load_xpt(): check metadata for XPT file expect_equal( - as.list(attr(iris_xpt[["iris.xpt"]], "meta")[names(xpt_file_info)]), + as.list(attr(iris_xpt[["iris.xpt"]], "meta")[names(xpt_file_info)]), as.list(xpt_file_info) ) } diff --git a/tests/testthat/test-prefer_sas.R b/tests/testthat/test-prefer_sas.R index 837ba34..5174432 100644 --- a/tests/testthat/test-prefer_sas.R +++ b/tests/testthat/test-prefer_sas.R @@ -2,35 +2,35 @@ test_that( desc = vdoc[["add_spec"]]( desc = "load_data() loads the RDS file if prefer_sas is FALSE, and it loads the SAS file if prefer_sas is TRUE.", spec = specs$prefer_sas - ), + ), code = { # load_data(): load the RDS file with prefer_sas = FALSE lifecycle::expect_deprecated( data_rds <- dv.loader::load_data( sub_dir = ".", file_names = "iris.rds", - prefer_sas = FALSE + prefer_sas = FALSE ) ) - + # load_data(): load the SAS file with prefer_sas = TRUE lifecycle::expect_deprecated( data_sas <- load_data( - sub_dir = ".", + sub_dir = ".", file_names = "iris.sas7bdat", prefer_sas = TRUE ) ) - + # Get metadata for RDS file meta_rds <- attr(data_rds[["iris.rds"]], "meta") - + # Get metadata for SAS file meta_sas <- attr(data_sas[["iris.sas7bdat"]], "meta") - + # Check if the correct RDS file is loaded - expect_equal(basename(meta_rds[["path"]]), "iris.rds") - + expect_equal(basename(meta_rds[["path"]]), "iris.rds") + # Check if the correct file is loaded expect_equal(basename(meta_sas[["path"]]), "iris.sas7bdat") } diff --git a/vignettes/integration-guide.Rmd b/vignettes/integration-guide.Rmd index f6393de..95a8e0f 100644 --- a/vignettes/integration-guide.Rmd +++ b/vignettes/integration-guide.Rmd @@ -66,7 +66,7 @@ Execute the `dv.manager::run_app()` function to initiate the DaVinci application ```{r, eval=FALSE} # Run the DaVinci application with data and module lists defined earlier dv.manager::run_app( - data = list("pharmaverseadam" = data_list), + data = list("pharmaverseadam" = data_list), module_list = module_list, filter_data = "adsl", title = "DaVinci Application"