Skip to content

Commit

Permalink
Merge pull request #132 from NIFU-NO/dev
Browse files Browse the repository at this point in the history
setup_mesos handles absolute paths
  • Loading branch information
sda030 authored Nov 28, 2024
2 parents 44afce2 + 7062a48 commit 842e33b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 71 deletions.
136 changes: 71 additions & 65 deletions R/gen_qmd_chapters.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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)


}
17 changes: 11 additions & 6 deletions R/setup_mesos.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -44,14 +46,17 @@ 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()
}
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.")
}

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 = "") |>
Expand All @@ -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(
Expand All @@ -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")
}
)
Expand Down Expand Up @@ -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
)
}
Expand Down

0 comments on commit 842e33b

Please sign in to comment.