Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix folders function #44

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
47 changes: 47 additions & 0 deletions R/create_folders.R
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)
}
}

2 changes: 1 addition & 1 deletion R/gen_clinical_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 2 additions & 0 deletions dev/config_fusen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
73 changes: 72 additions & 1 deletion dev/sample_selections.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions man/create_folders.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/testthat/test-create_folders.R
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"))
})

21 changes: 21 additions & 0 deletions vignettes/sample-selections-vignette.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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")

```






Loading