mirror of
https://github.com/agdamsbo/FreesearchR.git
synced 2026-06-19 04:27:30 +02:00
functions to modify qmd for specific exports
This commit is contained in:
parent
ad99b5c46f
commit
d71d6169ff
17 changed files with 246 additions and 29 deletions
10
R/helpers.R
10
R/helpers.R
|
|
@ -24,15 +24,13 @@ getfun <- function(x) {
|
|||
#' Wrapper to save data in RDS, load into specified qmd and render
|
||||
#'
|
||||
#' @param data list to pass to qmd
|
||||
#' @param fileformat output format. Ignored if file!=NULL
|
||||
#' @param qmd.file qmd file to render. Default is 'here::here("report.qmd")'
|
||||
#' @param ... Passed to `quarto::quarto_render()`
|
||||
#'
|
||||
#' @return output file name
|
||||
#' @export
|
||||
#'
|
||||
write_quarto <- function(data,fileformat=c("html","docx","odt","pdf","all"),qmd.file=here::here("report.qmd"),...){
|
||||
fileformat <- match.arg(fileformat)
|
||||
write_quarto <- function(data,...){
|
||||
|
||||
# Exports data to temporary location
|
||||
#
|
||||
# I assume this is more secure than putting it in the www folder and deleting
|
||||
|
|
@ -43,9 +41,7 @@ write_quarto <- function(data,fileformat=c("html","docx","odt","pdf","all"),qmd.
|
|||
## Specifying a output path will make the rendering fail
|
||||
## Ref: https://github.com/quarto-dev/quarto-cli/discussions/4041
|
||||
## Outputs to the same as the .qmd file
|
||||
quarto::quarto_render(qmd.file,
|
||||
output_format = fileformat,
|
||||
execute_params = list(data.file=temp),
|
||||
quarto::quarto_render(execute_params = list(data.file=temp),
|
||||
...
|
||||
)
|
||||
}
|
||||
|
|
|
|||
81
R/report.R
Normal file
81
R/report.R
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#' Split vector by an index and embed addition
|
||||
#'
|
||||
#' @param data vector
|
||||
#' @param index split index
|
||||
#' @param add addition
|
||||
#'
|
||||
#' @return vector
|
||||
#' @export
|
||||
#'
|
||||
index_embed <- function(data, index, add = NULL) {
|
||||
start <- seq_len(index)
|
||||
end <- seq_along(data)[-start]
|
||||
c(
|
||||
data[start],
|
||||
add,
|
||||
data[end]
|
||||
)
|
||||
}
|
||||
|
||||
#' Specify format arguments to include in qmd header/frontmatter
|
||||
#'
|
||||
#' @param data vector
|
||||
#' @param fileformat format to include
|
||||
#'
|
||||
#' @return vector
|
||||
#' @export
|
||||
#'
|
||||
specify_qmd_format <- function(data, fileformat = c("docx", "odt", "pdf", "all")) {
|
||||
fileformat <- match.arg(fileformat)
|
||||
args_list <- default_format_arguments() |> purrr::imap(format_writer)
|
||||
|
||||
if (fileformat == "all") {
|
||||
out <- data |> index_embed(index = 4, add = Reduce(c, args_list))
|
||||
} else {
|
||||
out <- data |> index_embed(index = 4, add = args_list[[fileformat]])
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#' Merges list of named arguments for qmd header generation
|
||||
#'
|
||||
#' @param data vector
|
||||
#' @param name name
|
||||
#'
|
||||
#' @return vector
|
||||
#' @export
|
||||
#'
|
||||
format_writer <- function(data, name) {
|
||||
if (data == "default") {
|
||||
glue::glue(" {name}: {data}")
|
||||
} else {
|
||||
warning("Not implemented")
|
||||
}
|
||||
}
|
||||
|
||||
#' Defaults qmd formats
|
||||
#'
|
||||
#' @return list
|
||||
#' @export
|
||||
#'
|
||||
default_format_arguments <- function() {
|
||||
list(
|
||||
docx = list("default"),
|
||||
odt = list("default"),
|
||||
pdf = list("default")
|
||||
)
|
||||
}
|
||||
|
||||
#' Wrapper to modify quarto file to render specific formats
|
||||
#'
|
||||
#' @param file filename
|
||||
#' @param format desired output
|
||||
#'
|
||||
#' @return none
|
||||
#' @export
|
||||
#'
|
||||
modify_qmd <- function(file, format) {
|
||||
readLines(file) |>
|
||||
specify_qmd_format(fileformat = "all") |>
|
||||
writeLines(paste0(tools::file_path_sans_ext(file), "_format.", tools::file_ext(file)))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue