Skip to content

Commit

Permalink
Merge pull request #131 from NIFU-NO/dev
Browse files Browse the repository at this point in the history
New feature: wrapper_create_mesos_qmd_files
  • Loading branch information
sda030 authored Nov 27, 2024
2 parents 252dec9 + 3fe0fd9 commit 44afce2
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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.9
Authors@R: c(
person(given = "Stephan",
family = "Daus",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export(read_default_draft_report_args)
export(refine_chapter_overview)
export(remove_entry_from_sidebar)
export(setup_access_restrictions)
export(setup_mesos)
export(write_default_draft_report_args)
importFrom(fs,dir_ls)
importFrom(rlang,"!!!")
Expand Down
5 changes: 4 additions & 1 deletion R/draft_report.R
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ draft_report <-
serialized_format = args$serialized_format
)

processed_files <- chapter_filepaths



Expand All @@ -278,6 +279,7 @@ draft_report <-
output_filename = NULL,
call = rlang::caller_env()
)
processed_files <- c(processed_files, report_filepath)
}

index_filepath <-
Expand All @@ -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(
Expand All @@ -302,5 +305,5 @@ draft_report <-
)


stringi::stri_replace_all_regex(index_filepath, pattern = "\\\\+", replacement = "/")
stringi::stri_replace_all_regex(processed_files, pattern = "\\\\+", replacement = "/")
}
144 changes: 144 additions & 0 deletions R/setup_mesos.R
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
)
}
12 changes: 9 additions & 3 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
- setup_mesos
- 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
Expand Down
2 changes: 2 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dataset's
datetime
de
dep
docx
dta
exe
filepath
Expand All @@ -53,6 +54,7 @@ htpasswd
indep
kbd
labelled
makeme
mesos
netlify
pre
Expand Down
32 changes: 32 additions & 0 deletions man/setup_mesos.Rd

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

0 comments on commit 44afce2

Please sign in to comment.