Skip to content

Commit

Permalink
Fix styler issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
mingstat committed Oct 14, 2024
1 parent f26d277 commit 7173caa
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 106 deletions.
42 changes: 21 additions & 21 deletions R/load_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}
16 changes: 8 additions & 8 deletions R/load_rds.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 8 additions & 8 deletions R/load_sas.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions R/load_xpt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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))
}
Expand All @@ -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)

Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ 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"
)
)

# Load SAS data via load_data()
lifecycle::expect_deprecated(
iris_data_sas <- dv.loader::load_data(
sub_dir = ".",
sub_dir = ".",
file_names = "iris.sas7bdat"
)
)
Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test-data_integrity.R
Original file line number Diff line number Diff line change
@@ -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"]],
Expand All @@ -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
)
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-default_dir.R
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -26,7 +26,7 @@ test_that(
sub_dir = ".",
file_names = "iris.sas7bdat",
use_wd = TRUE
)
)
)

# Expect that the RDS file is loaded
Expand Down
Loading

0 comments on commit 7173caa

Please sign in to comment.