From 0869f4aa76a689f4e87ec5a0e831719dca8aded4 Mon Sep 17 00:00:00 2001 From: Stephan Daus Date: Thu, 28 Nov 2024 07:10:15 +0100 Subject: [PATCH 1/2] gen_qmd_chapters return absolute filepaths for easier post-processing --- R/gen_qmd_chapters.R | 136 ++++++++++++++++++++++--------------------- 1 file changed, 71 insertions(+), 65 deletions(-) diff --git a/R/gen_qmd_chapters.R b/R/gen_qmd_chapters.R index f93881a..b74de82 100644 --- a/R/gen_qmd_chapters.R +++ b/R/gen_qmd_chapters.R @@ -30,18 +30,16 @@ gen_qmd_chapters <- chapter_qmd_end_section_filepath = NULL, attach_chapter_dataset = TRUE, auxiliary_variables = NULL, - serialized_format = "rds" - ) { - - - + serialized_format = "rds") { path <- fs::as_fs_path(path) grouping_structure <- dplyr::group_vars(chapter_structure) chapter_structure_chapter_groups <- - dplyr::group_by(chapter_structure, - dplyr::pick(tidyselect::all_of(grouping_structure[1]))) + dplyr::group_by( + chapter_structure, + dplyr::pick(tidyselect::all_of(grouping_structure[1])) + ) ## Generate each chapter. Returns paths to files, which are then used for index.qmd @@ -52,14 +50,11 @@ gen_qmd_chapters <- ## ALL OF THIS MOVED TO NEW FUNCTION gen_qmd_chapter and convert group_map to a for-loop? .f = function(chapter_structure_chapter, key_chapter) { - - - - - chapter_structure_chapter <- - dplyr::group_by(chapter_structure_chapter, - dplyr::pick(tidyselect::all_of(grouping_structure))) + dplyr::group_by( + chapter_structure_chapter, + dplyr::pick(tidyselect::all_of(grouping_structure)) + ) # Paths @@ -71,89 +66,100 @@ gen_qmd_chapters <- chapter_folderpath_absolute <- file.path(path, chapter_foldername_clean) dir.create(path = chapter_folderpath_absolute, recursive = TRUE, showWarnings = FALSE) - chapter_filepath_relative <- stringi::stri_c(chapter_foldername_clean, ".qmd", ignore_null=TRUE) + chapter_filepath_relative <- stringi::stri_c(chapter_foldername_clean, ".qmd", ignore_null = TRUE) chapter_filepath_absolute <- file.path(path, chapter_filepath_relative) authors <- get_authors(data = chapter_structure_chapter, col = authors_col) - yaml_section <- process_yaml(yaml_file = chapter_yaml_file, - title = NULL, - authors = authors, - chapter_number = chapter_number) - - if(!all(is.na(chapter_structure_chapter$.variable_name_dep))) { - + yaml_section <- process_yaml( + yaml_file = chapter_yaml_file, + title = NULL, + authors = authors, + chapter_number = chapter_number + ) + + if (!all(is.na(chapter_structure_chapter$.variable_name_dep))) { chapter_contents <- - gen_qmd_structure(chapter_structure = chapter_structure_chapter, - ignore_heading_for_group = ignore_heading_for_group, - replace_heading_for_group = replace_heading_for_group, - prefix_heading_for_group = prefix_heading_for_group, - suffix_heading_for_group = suffix_heading_for_group) - - } else chapter_contents <- NULL + gen_qmd_structure( + chapter_structure = chapter_structure_chapter, + ignore_heading_for_group = ignore_heading_for_group, + replace_heading_for_group = replace_heading_for_group, + prefix_heading_for_group = prefix_heading_for_group, + suffix_heading_for_group = suffix_heading_for_group + ) + } else { + chapter_contents <- NULL + } ### - if(inherits(chapter_structure_chapter, "data.frame")) { + if (inherits(chapter_structure_chapter, "data.frame")) { chapter_structure_chapter_simplified <- collapse_chapter_structure_to_chr(chapter_structure_chapter) } qmd_start_section <- - if(rlang::is_string(chapter_qmd_start_section_filepath)) { + if (rlang::is_string(chapter_qmd_start_section_filepath)) { out <- - stringi::stri_c(collapse = "\n", - ignore_null = TRUE, - readLines(con = chapter_qmd_start_section_filepath) - ) + stringi::stri_c( + collapse = "\n", + ignore_null = TRUE, + readLines(con = chapter_qmd_start_section_filepath) + ) - if(inherits(chapter_structure_chapter, "data.frame")) { + if (inherits(chapter_structure_chapter, "data.frame")) { tryCatch(glue::glue_data(chapter_structure_chapter_simplified, out, .na = ""), - error = function(cnd) glue_err(cnd=cnd, arg_name="chapter_qmd_start_section")) + error = function(cnd) glue_err(cnd = cnd, arg_name = "chapter_qmd_start_section") + ) } } qmd_end_section <- - if(rlang::is_string(chapter_qmd_end_section_filepath)) { + if (rlang::is_string(chapter_qmd_end_section_filepath)) { out <- - stringi::stri_c(collapse = "\n", - ignore_null = TRUE, - readLines(con = chapter_qmd_end_section_filepath) - ) - if(inherits(chapter_structure_chapter, "data.frame")) { + stringi::stri_c( + collapse = "\n", + ignore_null = TRUE, + readLines(con = chapter_qmd_end_section_filepath) + ) + if (inherits(chapter_structure_chapter, "data.frame")) { tryCatch(glue::glue_data(chapter_structure_chapter_simplified, out, .na = ""), - error = function(cnd) glue_err(cnd=cnd, arg_name="chapter_qmd_end_section")) + error = function(cnd) glue_err(cnd = cnd, arg_name = "chapter_qmd_end_section") + ) } } load_dataset <- - if(isTRUE(attach_chapter_dataset)) { - attach_chapter_dataset(data = data, - chapter_structure_chapter = chapter_structure_chapter, - chapter_foldername_clean = chapter_foldername_clean, - path = path, - auxiliary_variables = auxiliary_variables, - serialized_format = serialized_format) + if (isTRUE(attach_chapter_dataset)) { + attach_chapter_dataset( + data = data, + chapter_structure_chapter = chapter_structure_chapter, + chapter_foldername_clean = chapter_foldername_clean, + path = path, + auxiliary_variables = auxiliary_variables, + serialized_format = serialized_format + ) } - out <- c(yaml_section, - stringi::stri_c("# ", chapter), # Should use generalized function to get also reference, prefix and suffix - load_dataset, - qmd_start_section, - chapter_contents, - qmd_end_section) + out <- c( + yaml_section, + stringi::stri_c("# ", chapter), # Should use generalized function to get also reference, prefix and suffix + load_dataset, + qmd_start_section, + chapter_contents, + qmd_end_section + ) out <- stringi::stri_remove_na(out) - out <- stringi::stri_c(out, collapse = "\n", ignore_null=TRUE) + out <- stringi::stri_c(out, collapse = "\n", ignore_null = TRUE) out <- stringi::stri_replace_all_regex(out, - pattern = "\n{3,}", - replacement = "\n\n\n") + pattern = "\n{3,}", + replacement = "\n\n\n" + ) cat(out, file = chapter_filepath_absolute, append = FALSE) - chapter_filepath_relative - - }) + chapter_filepath_absolute + } + ) unlist(chapter_filepaths) - - } From 7062a487a71301e49968ea856d73d3ca94576ef7 Mon Sep 17 00:00:00 2001 From: Stephan Daus Date: Thu, 28 Nov 2024 07:10:49 +0100 Subject: [PATCH 2/2] setup_mesos handles absolute paths --- R/setup_mesos.R | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/R/setup_mesos.R b/R/setup_mesos.R index b9a74f9..ef5c5a7 100644 --- a/R/setup_mesos.R +++ b/R/setup_mesos.R @@ -2,13 +2,15 @@ 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}}.") + 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_process}.") } files_to_be_renamed <- - rlang::set_names(paste0("_", files_to_process), nm = files_to_process) + rlang::set_names(stringi::stri_replace_last_fixed(files_to_process, pattern = "/", replacement = "/_"), + nm = files_to_process + ) file.rename( from = names(files_to_be_renamed), to = unname(files_to_be_renamed) @@ -44,7 +46,7 @@ create_mesos_stubs_from_main_files <- function( main_files = c("index", "report")) { #### - if (!is.character(files) || !file.exists(files)) { + if (!is.character(files) || !any(file.exists(files))) { cli::cli_warn("No files found.") return() } @@ -52,6 +54,9 @@ create_mesos_stubs_from_main_files <- function( cli::cli_abort("{.arg mesos_var} must be a string and {.arg mesos_groups} must be a character vector.") } + dir_path <- unique(dirname(files)) + if (length(dir_path) > 1) cli::cli_abort("All files must be in the same folder.") + new_qmd_files <- files |> stringi::stri_replace_last_fixed(pattern = qmd_regex, replacement = "") |> @@ -68,7 +73,7 @@ create_mesos_stubs_from_main_files <- function( new_file_path = fs::path( .env$dir_path, .data$mesos_group, - paste0(.data$main_file_no_, ".qmd") + .data$main_file_no_ ), contents = { yaml <- list(params = list( @@ -81,7 +86,7 @@ create_mesos_stubs_from_main_files <- function( yaml <- yaml::as.yaml(x = yaml) paste0("---\n", yaml, "---\n", paste0( "\n{{< include ../", - .data$main_file, ".qmd >}}\n" + .data$main_file, " >}}\n" ), sep = "\n") } ) @@ -123,7 +128,7 @@ setup_mesos <- function( if (is.character(read_syntax_pattern) && is.character(read_syntax_replacement)) { search_and_replace_files( - files = files_to_be_renamed, + files = unname(files_to_be_renamed), pattern = read_syntax_pattern, replacement = read_syntax_replacement ) }