diff --git a/DESCRIPTION b/DESCRIPTION index 5f12b17..a91246e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,6 +22,7 @@ Imports: readxl (>= 1.3.1), rlang (>= 0.4.11), scales (>= 1.1.1), + stats, stringr (>= 1.4.0), tidyr (>= 1.2.0), tools (>= 4.1.0), diff --git a/NAMESPACE b/NAMESPACE index 4282f18..653850d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,6 +12,7 @@ export(as_label) export(as_name) export(auth_os) export(convert_os_df_to_wide) +export(create_folders) export(create_save_workbook) export(data_table) export(enquo) @@ -105,6 +106,7 @@ importFrom(rlang,as_name) importFrom(rlang,enquo) importFrom(rlang,enquos) importFrom(rlang,sym) +importFrom(stats,setNames) importFrom(stringr,str_extract) importFrom(stringr,str_replace_all) importFrom(stringr,str_split) diff --git a/R/create_folders.R b/R/create_folders.R new file mode 100644 index 0000000..05ac125 --- /dev/null +++ b/R/create_folders.R @@ -0,0 +1,47 @@ +# WARNING - Generated by {fusen} from dev/sample_selections.Rmd: do not edit by hand + +#' Create Project Folders +#' +#' This function creates a set of folders for a project, including a main folder and specified subfolders. +#' It optionally assigns global variable names for each folder path, using a provided suffix. +#' +#' @param main_folder A string specifying the path of the main project folder. +#' @param subfolders A character vector of subfolders to be created within the main folder. +#' Default is c("data", "results", "doc"). +#' @param suffix A string to append to each subfolder name when creating global variables. +#' Default is "_folder". +#' @importFrom stats setNames +#' @export +#' @examples +#' path_name <- tempfile(pattern = "main_folder") %>% +#' normalizePath(winslash = "/") +#' +#' create_folders(path_name) +#' +#' # Specify different subfolders and a custom suffix +#' create_folders(path_name, c("archive", "temp", "shared"), "_dir") +#' +create_folders <- function(main_folder, subfolders = c("data", "results", "doc"), suffix = "_folder") { + # Create a list of folder paths by appending each subfolder name to the main folder path + folders_to_create <- setNames( + lapply(subfolders, function(subfolder) paste0(main_folder, "/", subfolder)), + paste0(subfolders, suffix) + ) + + # Add the main folder path to the list, without the suffix + folders_to_create$main_folder <- main_folder + + # Create each folder if it does not exist + for (folder_name in names(folders_to_create)) { + folder_path <- folders_to_create[[folder_name]] + if (!dir.exists(folder_path)) { + dir.create(folder_path, recursive = TRUE) + cat(sprintf("Created folder: %s\n", folder_path)) + } else { + cat(sprintf("Folder already exists: %s\n", folder_path)) + } + # Optionally, assign folder path to global environment + assign(folder_name, folder_path, envir = .GlobalEnv) + } +} + diff --git a/R/gen_clinical_data.R b/R/gen_clinical_data.R index 77efe29..fde4784 100644 --- a/R/gen_clinical_data.R +++ b/R/gen_clinical_data.R @@ -120,7 +120,7 @@ gen_clinical_data <- function(clinical_data, stop("ppid_col is not present in the additional data set") } clinical_data <- merge(additional_data, - clinical_data, by = ppid_col, all.x = TRUE) + clinical_data, by = ppid_col, all.y = TRUE) }else{ diff --git a/dev/config_fusen.yaml b/dev/config_fusen.yaml index 941b128..15c3889 100644 --- a/dev/config_fusen.yaml +++ b/dev/config_fusen.yaml @@ -99,6 +99,7 @@ sample_selections.Rmd: R: - R/append_check_col.R - R/convert_os_df_to_wide.R + - R/create_folders.R - R/create_save_workbook.R - R/gen_clinical_data.R - R/make_clean_os_names.R @@ -126,6 +127,7 @@ sample_selections.Rmd: - tests/testthat/test-gen_clinical_data.R - tests/testthat/test-samples_distributed_summary.R - tests/testthat/test-mark_all_duplicates.R + - tests/testthat/test-create_folders.R vignettes: vignettes/sample-selections-vignette.Rmd inflate: flat_file: dev/sample_selections.Rmd diff --git a/dev/sample_selections.Rmd b/dev/sample_selections.Rmd index 92d8c93..0f7045a 100644 --- a/dev/sample_selections.Rmd +++ b/dev/sample_selections.Rmd @@ -1256,7 +1256,7 @@ gen_clinical_data <- function(clinical_data, stop("ppid_col is not present in the additional data set") } clinical_data <- merge(additional_data, - clinical_data, by = ppid_col, all.x = TRUE) + clinical_data, by = ppid_col, all.y = TRUE) }else{ @@ -1536,6 +1536,77 @@ test_that("mark_all_duplicates works", { }) ``` +# create_folders + +```{r function-create_folders} +#' Create Project Folders +#' +#' This function creates a set of folders for a project, including a main folder and specified subfolders. +#' It optionally assigns global variable names for each folder path, using a provided suffix. +#' +#' @param main_folder A string specifying the path of the main project folder. +#' @param subfolders A character vector of subfolders to be created within the main folder. +#' Default is c("data", "results", "doc"). +#' @param suffix A string to append to each subfolder name when creating global variables. +#' Default is "_folder". +#' @importFrom stats setNames +#' @export +create_folders <- function(main_folder, subfolders = c("data", "results", "doc"), suffix = "_folder") { + # Create a list of folder paths by appending each subfolder name to the main folder path + folders_to_create <- setNames( + lapply(subfolders, function(subfolder) paste0(main_folder, "/", subfolder)), + paste0(subfolders, suffix) + ) + + # Add the main folder path to the list, without the suffix + folders_to_create$main_folder <- main_folder + + # Create each folder if it does not exist + for (folder_name in names(folders_to_create)) { + folder_path <- folders_to_create[[folder_name]] + if (!dir.exists(folder_path)) { + dir.create(folder_path, recursive = TRUE) + cat(sprintf("Created folder: %s\n", folder_path)) + } else { + cat(sprintf("Folder already exists: %s\n", folder_path)) + } + # Optionally, assign folder path to global environment + assign(folder_name, folder_path, envir = .GlobalEnv) + } +} + +``` + +```{r example-create_folders} +path_name <- tempfile(pattern = "main_folder") %>% + normalizePath(winslash = "/") + +create_folders(path_name) + +# Specify different subfolders and a custom suffix +create_folders(path_name, c("archive", "temp", "shared"), "_dir") + +``` + +```{r tests-create_folders} +# Test the function with custom subfolders and a custom suffix +test_that("create_folders with custom subfolders and suffix", { + # Test setup: create a temporary path for testing + path_name <- tempfile(pattern = "main_folder") %>% + normalizePath(winslash = "/") + + create_folders(path_name, c("archive", "temp", "shared"), "_dir") + expect_true(dir.exists(file.path(path_name, "archive"))) + expect_true(dir.exists(file.path(path_name, "temp"))) + expect_true(dir.exists(file.path(path_name, "shared"))) + # Check if global variables are correctly assigned + expect_identical(get("archive_dir", envir = .GlobalEnv), file.path(path_name, "archive")) + expect_identical(get("temp_dir", envir = .GlobalEnv), file.path(path_name, "temp")) + expect_identical(get("shared_dir", envir = .GlobalEnv), file.path(path_name, "shared")) +}) + +``` + ```{r development-load} # Load already included functions if relevant diff --git a/man/create_folders.Rd b/man/create_folders.Rd new file mode 100644 index 0000000..a3d5d1e --- /dev/null +++ b/man/create_folders.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/create_folders.R +\name{create_folders} +\alias{create_folders} +\title{Create Project Folders} +\usage{ +create_folders( + main_folder, + subfolders = c("data", "results", "doc"), + suffix = "_folder" +) +} +\arguments{ +\item{main_folder}{A string specifying the path of the main project folder.} + +\item{subfolders}{A character vector of subfolders to be created within the main folder. +Default is c("data", "results", "doc").} + +\item{suffix}{A string to append to each subfolder name when creating global variables. +Default is "_folder".} +} +\description{ +This function creates a set of folders for a project, including a main folder and specified subfolders. +It optionally assigns global variable names for each folder path, using a provided suffix. +} +\examples{ +path_name <- tempfile(pattern = "main_folder") \%>\% + normalizePath(winslash = "/") + +create_folders(path_name) + +# Specify different subfolders and a custom suffix +create_folders(path_name, c("archive", "temp", "shared"), "_dir") + +} diff --git a/tests/testthat/test-create_folders.R b/tests/testthat/test-create_folders.R new file mode 100644 index 0000000..e0edb4e --- /dev/null +++ b/tests/testthat/test-create_folders.R @@ -0,0 +1,18 @@ +# WARNING - Generated by {fusen} from dev/sample_selections.Rmd: do not edit by hand + +# Test the function with custom subfolders and a custom suffix +test_that("create_folders with custom subfolders and suffix", { + # Test setup: create a temporary path for testing + path_name <- tempfile(pattern = "main_folder") %>% + normalizePath(winslash = "/") + + create_folders(path_name, c("archive", "temp", "shared"), "_dir") + expect_true(dir.exists(file.path(path_name, "archive"))) + expect_true(dir.exists(file.path(path_name, "temp"))) + expect_true(dir.exists(file.path(path_name, "shared"))) + # Check if global variables are correctly assigned + expect_identical(get("archive_dir", envir = .GlobalEnv), file.path(path_name, "archive")) + expect_identical(get("temp_dir", envir = .GlobalEnv), file.path(path_name, "temp")) + expect_identical(get("shared_dir", envir = .GlobalEnv), file.path(path_name, "shared")) +}) + diff --git a/vignettes/sample-selections-vignette.Rmd b/vignettes/sample-selections-vignette.Rmd index 08ea6a8..f820414 100644 --- a/vignettes/sample-selections-vignette.Rmd +++ b/vignettes/sample-selections-vignette.Rmd @@ -477,4 +477,25 @@ mark_all_duplicates(vec) +# create_folders + + + + + +```{r example-create_folders} +path_name <- tempfile(pattern = "main_folder") %>% + normalizePath(winslash = "/") + +create_folders(path_name) + +# Specify different subfolders and a custom suffix +create_folders(path_name, c("archive", "temp", "shared"), "_dir") + +``` + + + + +