-
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 #131 from NIFU-NO/dev
New feature: wrapper_create_mesos_qmd_files
- Loading branch information
Showing
7 changed files
with
193 additions
and
5 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
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,144 @@ | ||
set_underscore_on_filenames <- function(files_to_process) { | ||
if (!is.character(files_to_process) || | ||
length(files_to_process) == 0 || | ||
!all(file.exists(files_to_process))) { | ||
cli::cli_abort("{.arg files_to_process} must be a character vector of paths to existing files, not: {.obj_type_friendly {files_to_process}}.") | ||
} | ||
|
||
|
||
|
||
files_to_be_renamed <- | ||
rlang::set_names(paste0("_", files_to_process), nm = files_to_process) | ||
file.rename( | ||
from = names(files_to_be_renamed), | ||
to = unname(files_to_be_renamed) | ||
) | ||
files_to_be_renamed | ||
} | ||
|
||
|
||
search_and_replace_files <- function( | ||
files, | ||
pattern, | ||
replacement) { | ||
if (!is.character(pattern) || | ||
!is.character(replacement) || | ||
length(pattern) != length(replacement)) { | ||
cli::cli_abort("{.arg pattern} and {.arg replacement} must be character vectors of same length.") | ||
} | ||
files |> | ||
unname() |> | ||
lapply(FUN = function(.x) { | ||
readLines(.x) |> | ||
stringi::stri_replace_all_regex(pattern, replacement) |> | ||
writeLines(.x) | ||
}) | ||
files | ||
} | ||
|
||
create_mesos_stubs_from_main_files <- function( | ||
files, | ||
mesos_var, | ||
mesos_groups, | ||
qmd_regex = "\\.qmd", | ||
main_files = c("index", "report")) { | ||
#### | ||
|
||
if (!is.character(files) || !file.exists(files)) { | ||
cli::cli_warn("No files found.") | ||
return() | ||
} | ||
if (!rlang::is_string(mesos_var) || !is.character(mesos_groups)) { | ||
cli::cli_abort("{.arg mesos_var} must be a string and {.arg mesos_groups} must be a character vector.") | ||
} | ||
|
||
new_qmd_files <- | ||
files |> | ||
stringi::stri_replace_last_fixed(pattern = qmd_regex, replacement = "") |> | ||
basename() |> | ||
tidyr::expand_grid( | ||
main_file = _, | ||
mesos_group = mesos_groups | ||
) |> | ||
dplyr::rowwise() |> | ||
dplyr::mutate( | ||
main_file_no_ = stringi::stri_replace_first_regex(.data$main_file, | ||
pattern = "^_", replacement = "" | ||
), | ||
new_file_path = fs::path( | ||
.env$dir_path, | ||
.data$mesos_group, | ||
paste0(.data$main_file_no_, ".qmd") | ||
), | ||
contents = { | ||
yaml <- list(params = list( | ||
mesos_var = .env$mesos_var, | ||
mesos_group = .data$mesos_group | ||
)) | ||
if (.data$main_file_no_ %in% main_files) { | ||
yaml$title <- paste0(.data$mesos_group) | ||
} | ||
yaml <- yaml::as.yaml(x = yaml) | ||
paste0("---\n", yaml, "---\n", paste0( | ||
"\n{{< include ../", | ||
.data$main_file, ".qmd >}}\n" | ||
), sep = "\n") | ||
} | ||
) | ||
|
||
|
||
fs::dir_create(dirname(new_qmd_files$new_file_path), recurse = TRUE) | ||
for (i in seq_len(nrow(new_qmd_files))) { | ||
cat(new_qmd_files[i, "contents", drop = TRUE], file = new_qmd_files[i, | ||
"new_file_path", | ||
drop = TRUE | ||
]) | ||
} | ||
new_qmd_files | ||
} | ||
|
||
|
||
#' Simply create qmd-files for mesos reports | ||
#' @param files_to_process Character vector of files used as templates for the mesos stubs. | ||
#' @param mesos_var String, variable used during rendering with saros::makeme() to obtain the column in the dataset which it will filter on. | ||
#' @param mesos_groups Character vector for which stub-qmd files will be made | ||
#' @param main_files Character vector of files for which titles should be set as the mesos_group. Optional but recommended. | ||
#' @param read_syntax_pattern,read_syntax_replacement Optional strings, any regex pattern to search and replace in the qmd-files. If NULL, will ignore it. | ||
#' @param qmd_regex String. Experimental feature for allowing Rmarkdown, not yet tested. | ||
#' @export | ||
setup_mesos <- function( | ||
files_to_process, | ||
mesos_var, | ||
mesos_groups, | ||
main_files = c("index", "report"), | ||
read_syntax_pattern = "qs::qread\\('", | ||
read_syntax_replacement = "qs::qread('../", | ||
qmd_regex = "\\.qmd") { | ||
## Checks | ||
|
||
|
||
files_to_be_renamed <- | ||
set_underscore_on_filenames(files_to_process = files_to_process) | ||
|
||
if (is.character(read_syntax_pattern) && | ||
is.character(read_syntax_replacement)) { | ||
search_and_replace_files( | ||
files = files_to_be_renamed, | ||
pattern = read_syntax_pattern, replacement = read_syntax_replacement | ||
) | ||
} | ||
|
||
mesos_files <- | ||
create_mesos_stubs_from_main_files( | ||
files = unname(files_to_be_renamed), | ||
mesos_var = mesos_var, | ||
mesos_groups = mesos_groups, | ||
main_files = main_files, | ||
qmd_regex = qmd_regex | ||
) | ||
|
||
list( | ||
files_renamed = files_to_be_renamed, | ||
mesos_subfolder_files = mesos_files | ||
) | ||
} |
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.