-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from finddx/sample-classification
fix folders function
- Loading branch information
Showing
9 changed files
with
199 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters