From 8000ec833912a749e4fcedd9f82a4f0ab081de6d Mon Sep 17 00:00:00 2001 From: Stephan Daus Date: Wed, 27 Nov 2024 21:58:17 +0100 Subject: [PATCH 1/4] New feature: wrapper_create_mesos_qmd_files --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/wrapper_create_mesos_qmd_files.R | 117 ++++++++++++++++++++++++++ inst/WORDLIST | 2 + man/wrapper_create_mesos_qmd_files.Rd | 32 +++++++ 5 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 R/wrapper_create_mesos_qmd_files.R create mode 100644 man/wrapper_create_mesos_qmd_files.Rd diff --git a/DESCRIPTION b/DESCRIPTION index b05e73a..dc71278 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: saros.base Title: Base Tools for Semi-Automatic Reporting of Ordinary Surveys -Version: 0.2.7 +Version: 0.2.8 Authors@R: c( person(given = "Stephan", family = "Daus", diff --git a/NAMESPACE b/NAMESPACE index 3367148..ea92204 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,6 +15,7 @@ export(read_default_draft_report_args) export(refine_chapter_overview) export(remove_entry_from_sidebar) export(setup_access_restrictions) +export(wrapper_create_mesos_qmd_files) export(write_default_draft_report_args) importFrom(fs,dir_ls) importFrom(rlang,"!!!") diff --git a/R/wrapper_create_mesos_qmd_files.R b/R/wrapper_create_mesos_qmd_files.R new file mode 100644 index 0000000..5523009 --- /dev/null +++ b/R/wrapper_create_mesos_qmd_files.R @@ -0,0 +1,117 @@ +create_mesos_qmd_files <- function( + dir_path = tempdir(), + mesos_var, + mesos_groups, + main_files = c("index", "report")) { + #### + new_qmd_files <- fs::dir_ls( + path = dir_path, regexp = "\\.qmd", + recurse = FALSE + ) + if (length(new_qmd_files) == 0) { + cli::cli_warn("No files found.") + return() + } + new_qmd_files <- + new_qmd_files |> + stringi::stri_replace_last_fixed(pattern = "\\.qmd", 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") + } + ) + + + + + dir.create(dirname(new_qmd_files$new_file_path), + showWarnings = FALSE, recursive = 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 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 mesos_group_path String, path to parent folder where mesos subfolders are found. +#' @param read_syntax_pattern,read_syntax_replacement Strings, any regex pattern to search and replace in the qmd-files. +#' @param files_to_process_regex String. regex pattern to use for finding the files in subfolders. +#' @param main_files Character vector of files for which titles should be set as the mesos_group. Optional but recommended. +#' @export +wrapper_create_mesos_qmd_files <- function( + mesos_var, + mesos_groups, + mesos_group_path = tempdir(), + read_syntax_pattern = "qs::qread\\('", + read_syntax_replacement = "qs::qread('../", + files_to_process_regex = "[0-9]+_|index|report", + main_files = c("index", "report")) { + ## Checks + if (!rlang::is_string(mesos_var) || !is.character(mesos_groups) || !rlang::is_string(mesos_group_path) || !file.exists(mesos_group_path)) { + cli::cli_abort("{.arg mesos_var} must be a string, {.arg mesos_groups} must be a character vector, and {mesos_group_path} must be an existing directory.") + } + + + wd <- getwd() + + setwd(mesos_group_path) + + files_to_be_renamed <- + list.files(path = ".", recursive = FALSE, pattern = files_to_process_regex) + files_to_be_renamed <- + rlang::set_names(paste0("_", files_to_be_renamed), nm = files_to_be_renamed) + if (!all(files_to_be_renamed == "_")) { + file.rename( + from = names(files_to_be_renamed), + to = unname(files_to_be_renamed) + ) + } + + files_to_be_renamed |> + lapply(FUN = function(.x) { + readLines(.x) |> + stringi::stri_replace_all_regex(read_syntax_pattern, read_syntax_replacement) |> + writeLines(.x) + }) + + create_mesos_qmd_files( + dir_path = ".", + mesos_var = mesos_var, + mesos_groups = mesos_groups, + main_files = main_files + ) + setwd(wd) + files_to_be_renamed +} diff --git a/inst/WORDLIST b/inst/WORDLIST index 25b78b6..2d0133d 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -45,6 +45,7 @@ dataset's datetime de dep +docx dta exe filepath @@ -53,6 +54,7 @@ htpasswd indep kbd labelled +makeme mesos netlify pre diff --git a/man/wrapper_create_mesos_qmd_files.Rd b/man/wrapper_create_mesos_qmd_files.Rd new file mode 100644 index 0000000..10c3872 --- /dev/null +++ b/man/wrapper_create_mesos_qmd_files.Rd @@ -0,0 +1,32 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/wrapper_create_mesos_qmd_files.R +\name{wrapper_create_mesos_qmd_files} +\alias{wrapper_create_mesos_qmd_files} +\title{Simply create qmd-files for mesos reports} +\usage{ +wrapper_create_mesos_qmd_files( + mesos_var, + mesos_groups, + mesos_group_path = tempdir(), + read_syntax_pattern = "qs::qread\\\\('", + read_syntax_replacement = "qs::qread('../", + files_to_process_regex = "[0-9]+_|index|report", + main_files = c("index", "report") +) +} +\arguments{ +\item{mesos_var}{String, variable used during rendering with saros::makeme() to obtain the column in the dataset which it will filter on.} + +\item{mesos_groups}{Character vector for which stub-qmd files will be made} + +\item{mesos_group_path}{String, path to parent folder where mesos subfolders are found.} + +\item{read_syntax_pattern, read_syntax_replacement}{Strings, any regex pattern to search and replace in the qmd-files.} + +\item{files_to_process_regex}{String. regex pattern to use for finding the files in subfolders.} + +\item{main_files}{Character vector of files for which titles should be set as the mesos_group. Optional but recommended.} +} +\description{ +Simply create qmd-files for mesos reports +} From 6552e55d6c0f448a150dea8762b2614dd8939cae Mon Sep 17 00:00:00 2001 From: Stephan Daus Date: Wed, 27 Nov 2024 22:10:12 +0100 Subject: [PATCH 2/4] Updated pkgdown --- _pkgdown.yml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/_pkgdown.yml b/_pkgdown.yml index 2ac3a01..2b38683 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -3,12 +3,18 @@ template: bootstrap: 5 light-switch: yes reference: -- title: Useful helper functions that will be later moved to respective packages - contents: refine_chapter_overview +- title: Used to create the structure of your report(s) + contents: + - refine_chapter_overview - title: Main use desc: | The only functions you will probably use, in the order listed - contents: draft_report + contents: + - draft_report + - wrapper_create_mesos_qmd_files +- title: Post-processing tools (possibly movin to a Quarto extension at a later stage) + contents: + - remove_entry_from_sidebar - title: Auxiliary functions to obtain defaults contents: - get_chunk_template_defaults From 8c6cf7f9ee398bbc26f566498aa346b07247436c Mon Sep 17 00:00:00 2001 From: Stephan Daus Date: Wed, 27 Nov 2024 23:41:00 +0100 Subject: [PATCH 3/4] Fixing setup_mesos() and refactoring --- DESCRIPTION | 2 +- NAMESPACE | 2 +- R/draft_report.R | 5 +- R/setup_mesos.R | 144 ++++++++++++++++++ R/wrapper_create_mesos_qmd_files.R | 117 -------------- ...eate_mesos_qmd_files.Rd => setup_mesos.Rd} | 24 +-- 6 files changed, 162 insertions(+), 132 deletions(-) create mode 100644 R/setup_mesos.R delete mode 100644 R/wrapper_create_mesos_qmd_files.R rename man/{wrapper_create_mesos_qmd_files.Rd => setup_mesos.Rd} (51%) diff --git a/DESCRIPTION b/DESCRIPTION index dc71278..bdde6e5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: saros.base Title: Base Tools for Semi-Automatic Reporting of Ordinary Surveys -Version: 0.2.8 +Version: 0.2.9 Authors@R: c( person(given = "Stephan", family = "Daus", diff --git a/NAMESPACE b/NAMESPACE index ea92204..ba1c9cc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -15,7 +15,7 @@ export(read_default_draft_report_args) export(refine_chapter_overview) export(remove_entry_from_sidebar) export(setup_access_restrictions) -export(wrapper_create_mesos_qmd_files) +export(setup_mesos) export(write_default_draft_report_args) importFrom(fs,dir_ls) importFrom(rlang,"!!!") diff --git a/R/draft_report.R b/R/draft_report.R index a61c177..1f583a6 100644 --- a/R/draft_report.R +++ b/R/draft_report.R @@ -260,6 +260,7 @@ draft_report <- serialized_format = args$serialized_format ) + processed_files <- chapter_filepaths @@ -278,6 +279,7 @@ draft_report <- output_filename = NULL, call = rlang::caller_env() ) + processed_files <- c(processed_files, report_filepath) } index_filepath <- @@ -294,6 +296,7 @@ draft_report <- output_filename = args$report_filename, call = rlang::caller_env() ) + processed_files <- c(processed_files, index_filepath) validate_path_lengths_on_win( @@ -302,5 +305,5 @@ draft_report <- ) - stringi::stri_replace_all_regex(index_filepath, pattern = "\\\\+", replacement = "/") + stringi::stri_replace_all_regex(processed_files, pattern = "\\\\+", replacement = "/") } diff --git a/R/setup_mesos.R b/R/setup_mesos.R new file mode 100644 index 0000000..b9a74f9 --- /dev/null +++ b/R/setup_mesos.R @@ -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 + ) +} diff --git a/R/wrapper_create_mesos_qmd_files.R b/R/wrapper_create_mesos_qmd_files.R deleted file mode 100644 index 5523009..0000000 --- a/R/wrapper_create_mesos_qmd_files.R +++ /dev/null @@ -1,117 +0,0 @@ -create_mesos_qmd_files <- function( - dir_path = tempdir(), - mesos_var, - mesos_groups, - main_files = c("index", "report")) { - #### - new_qmd_files <- fs::dir_ls( - path = dir_path, regexp = "\\.qmd", - recurse = FALSE - ) - if (length(new_qmd_files) == 0) { - cli::cli_warn("No files found.") - return() - } - new_qmd_files <- - new_qmd_files |> - stringi::stri_replace_last_fixed(pattern = "\\.qmd", 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") - } - ) - - - - - dir.create(dirname(new_qmd_files$new_file_path), - showWarnings = FALSE, recursive = 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 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 mesos_group_path String, path to parent folder where mesos subfolders are found. -#' @param read_syntax_pattern,read_syntax_replacement Strings, any regex pattern to search and replace in the qmd-files. -#' @param files_to_process_regex String. regex pattern to use for finding the files in subfolders. -#' @param main_files Character vector of files for which titles should be set as the mesos_group. Optional but recommended. -#' @export -wrapper_create_mesos_qmd_files <- function( - mesos_var, - mesos_groups, - mesos_group_path = tempdir(), - read_syntax_pattern = "qs::qread\\('", - read_syntax_replacement = "qs::qread('../", - files_to_process_regex = "[0-9]+_|index|report", - main_files = c("index", "report")) { - ## Checks - if (!rlang::is_string(mesos_var) || !is.character(mesos_groups) || !rlang::is_string(mesos_group_path) || !file.exists(mesos_group_path)) { - cli::cli_abort("{.arg mesos_var} must be a string, {.arg mesos_groups} must be a character vector, and {mesos_group_path} must be an existing directory.") - } - - - wd <- getwd() - - setwd(mesos_group_path) - - files_to_be_renamed <- - list.files(path = ".", recursive = FALSE, pattern = files_to_process_regex) - files_to_be_renamed <- - rlang::set_names(paste0("_", files_to_be_renamed), nm = files_to_be_renamed) - if (!all(files_to_be_renamed == "_")) { - file.rename( - from = names(files_to_be_renamed), - to = unname(files_to_be_renamed) - ) - } - - files_to_be_renamed |> - lapply(FUN = function(.x) { - readLines(.x) |> - stringi::stri_replace_all_regex(read_syntax_pattern, read_syntax_replacement) |> - writeLines(.x) - }) - - create_mesos_qmd_files( - dir_path = ".", - mesos_var = mesos_var, - mesos_groups = mesos_groups, - main_files = main_files - ) - setwd(wd) - files_to_be_renamed -} diff --git a/man/wrapper_create_mesos_qmd_files.Rd b/man/setup_mesos.Rd similarity index 51% rename from man/wrapper_create_mesos_qmd_files.Rd rename to man/setup_mesos.Rd index 10c3872..7ad4f6e 100644 --- a/man/wrapper_create_mesos_qmd_files.Rd +++ b/man/setup_mesos.Rd @@ -1,31 +1,31 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/wrapper_create_mesos_qmd_files.R -\name{wrapper_create_mesos_qmd_files} -\alias{wrapper_create_mesos_qmd_files} +% Please edit documentation in R/setup_mesos.R +\name{setup_mesos} +\alias{setup_mesos} \title{Simply create qmd-files for mesos reports} \usage{ -wrapper_create_mesos_qmd_files( +setup_mesos( + files_to_process, mesos_var, mesos_groups, - mesos_group_path = tempdir(), + main_files = c("index", "report"), read_syntax_pattern = "qs::qread\\\\('", read_syntax_replacement = "qs::qread('../", - files_to_process_regex = "[0-9]+_|index|report", - main_files = c("index", "report") + qmd_regex = "\\\\.qmd" ) } \arguments{ +\item{files_to_process}{Character vector of files used as templates for the mesos stubs.} + \item{mesos_var}{String, variable used during rendering with saros::makeme() to obtain the column in the dataset which it will filter on.} \item{mesos_groups}{Character vector for which stub-qmd files will be made} -\item{mesos_group_path}{String, path to parent folder where mesos subfolders are found.} - -\item{read_syntax_pattern, read_syntax_replacement}{Strings, any regex pattern to search and replace in the qmd-files.} +\item{main_files}{Character vector of files for which titles should be set as the mesos_group. Optional but recommended.} -\item{files_to_process_regex}{String. regex pattern to use for finding the files in subfolders.} +\item{read_syntax_pattern, read_syntax_replacement}{Optional strings, any regex pattern to search and replace in the qmd-files. If NULL, will ignore it.} -\item{main_files}{Character vector of files for which titles should be set as the mesos_group. Optional but recommended.} +\item{qmd_regex}{String. Experimental feature for allowing Rmarkdown, not yet tested.} } \description{ Simply create qmd-files for mesos reports From 3fe0fd91e39cecf450bd085c54c7ec24d2f2fc11 Mon Sep 17 00:00:00 2001 From: Stephan Daus Date: Wed, 27 Nov 2024 23:44:18 +0100 Subject: [PATCH 4/4] Update pkgdown --- _pkgdown.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_pkgdown.yml b/_pkgdown.yml index 2b38683..db3d1ef 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -11,7 +11,7 @@ reference: The only functions you will probably use, in the order listed contents: - draft_report - - wrapper_create_mesos_qmd_files + - setup_mesos - title: Post-processing tools (possibly movin to a Quarto extension at a later stage) contents: - remove_entry_from_sidebar