functions to modify qmd for specific exports

This commit is contained in:
Andreas Gammelgaard Damsbo 2024-11-23 10:07:26 +01:00
parent ad99b5c46f
commit d71d6169ff
No known key found for this signature in database
17 changed files with 246 additions and 29 deletions

View file

@ -4,3 +4,4 @@
^\.Rproj\.user$
^LICENSE\.md$
^dev$
.github

View file

@ -22,9 +22,12 @@ Imports:
readODS,
readr,
shiny,
MASS
MASS,
REDCapCAST,
purrr
Suggests:
styler,
devtools
devtools,
rhub
URL: https://github.com/agdamsbo/webResearch
BugReports: https://github.com/agdamsbo/webResearch/issues

View file

@ -2,11 +2,17 @@
export(argsstring2list)
export(baseline_table)
export(default_format_arguments)
export(factorize)
export(format_writer)
export(getfun)
export(index_embed)
export(modify_qmd)
export(panel_space)
export(read_input)
export(regression_model)
export(regression_table)
export(shiny_webResearch)
export(specify_qmd_format)
export(write_quarto)
importFrom(stats,as.formula)

View file

@ -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
View 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)))
}

View file

@ -5,6 +5,6 @@ account: agdamsbo
server: shinyapps.io
hostUrl: https://api.shinyapps.io/v1
appId: 13276335
bundleId:
bundleId: 9402292
url: https://agdamsbo.shinyapps.io/webResearch/
version: 1

View file

@ -162,9 +162,25 @@ server <- function(input, output, session) {
v$list$table2 |>
gtsummary::as_gt()
)
}
)
# renderUI({
# tags$iframe(seamless="seamless",
# src= "Hub_Infographic.html",
# width=800,
# height=800)
# })
#
#
# getPage<-shiny::reactive({
# shiny::req(file.exists(file.path(getwd(), "www/report_format.html")))
# return(shiny::includeHTML(file.path(getwd(), "www/report_format.html")))
# })
output$uploaded <- shiny::reactive({
if (is.null(v$ds)) {
"no"
@ -195,8 +211,8 @@ server <- function(input, output, session) {
content = function(file, type = input$output_type) {
v$list |>
write_quarto(
fileformat = type,
qmd.file = file.path(getwd(), "www/report.qmd")
output_format = type,
input = file.path(getwd(), "www/report.qmd")
)
file.rename(paste0("www/report.", type), file)
}
@ -210,5 +226,5 @@ server <- function(input, output, session) {
print(paste(.x, "deleted"))
})
})
#
}

View file

@ -47,7 +47,19 @@ panels <- list(
ui <- bslib::page(
theme = bslib::bs_theme(bootswatch = "minty"),
theme = bslib::bs_theme(bootswatch = "minty",
base_font = font_google("Inter"),
code_font = font_google("JetBrains Mono")
),
# theme = bslib::bs_theme(
# bg = "#101010",
# fg = "#FFF",
# primary = "#E69F00",
# secondary = "#0072B2",
# success = "#009E73",
# base_font = font_google("Inter"),
# code_font = font_google("JetBrains Mono")
# ),
title = "webResearcher for easy data analysis",
bslib::page_navbar(
title = "webResearcher",
@ -159,7 +171,8 @@ ui <- bslib::page(
choices = list(
"Word" = "docx",
"LibreOffice" = "odt",
"PDF" = "pdf"
"PDF" = "pdf",
"All the above" = "all"
)
),

View file

@ -1,16 +1,13 @@
---
format:
html:
embed-resources: true
title: "webResearch analysis results"
date: today
author: webResearch Tool
toc: true
execute:
echo: false
format:
html:
embed-resources: true
docx: default
odt: default
pdf: default
params:
data.file: NA
---

Binary file not shown.

View file

@ -0,0 +1,14 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/report.R
\name{default_format_arguments}
\alias{default_format_arguments}
\title{Defaults qmd formats}
\usage{
default_format_arguments()
}
\value{
list
}
\description{
Defaults qmd formats
}

19
man/factorize.Rd Normal file
View file

@ -0,0 +1,19 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/helpers.R
\name{factorize}
\alias{factorize}
\title{Factorize variables in data.frame}
\usage{
factorize(data, vars)
}
\arguments{
\item{data}{data.frame}
\item{vars}{variables to force factorize}
}
\value{
data.frame
}
\description{
Factorize variables in data.frame
}

19
man/format_writer.Rd Normal file
View file

@ -0,0 +1,19 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/report.R
\name{format_writer}
\alias{format_writer}
\title{Merges list of named arguments for qmd header generation}
\usage{
format_writer(data, name)
}
\arguments{
\item{data}{vector}
\item{name}{name}
}
\value{
vector
}
\description{
Merges list of named arguments for qmd header generation
}

21
man/index_embed.Rd Normal file
View file

@ -0,0 +1,21 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/report.R
\name{index_embed}
\alias{index_embed}
\title{Split vector by an index and embed addition}
\usage{
index_embed(data, index, add = NULL)
}
\arguments{
\item{data}{vector}
\item{index}{split index}
\item{add}{addition}
}
\value{
vector
}
\description{
Split vector by an index and embed addition
}

19
man/modify_qmd.Rd Normal file
View file

@ -0,0 +1,19 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/report.R
\name{modify_qmd}
\alias{modify_qmd}
\title{Wrapper to modify quarto file to render specific formats}
\usage{
modify_qmd(file, format)
}
\arguments{
\item{file}{filename}
\item{format}{desired output}
}
\value{
none
}
\description{
Wrapper to modify quarto file to render specific formats
}

19
man/specify_qmd_format.Rd Normal file
View file

@ -0,0 +1,19 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/report.R
\name{specify_qmd_format}
\alias{specify_qmd_format}
\title{Specify format arguments to include in qmd header/frontmatter}
\usage{
specify_qmd_format(data, fileformat = c("docx", "odt", "pdf", "all"))
}
\arguments{
\item{data}{vector}
\item{fileformat}{format to include}
}
\value{
vector
}
\description{
Specify format arguments to include in qmd header/frontmatter
}

View file

@ -4,18 +4,11 @@
\alias{write_quarto}
\title{Wrapper to save data in RDS, load into specified qmd and render}
\usage{
write_quarto(
data,
fileformat = c("html", "docx", "odt", "pdf", "all"),
qmd.file = here::here("report.qmd"),
...
)
write_quarto(data, qmd.file = here::here("report.qmd"), ...)
}
\arguments{
\item{data}{list to pass to qmd}
\item{fileformat}{output format. Ignored if file!=NULL}
\item{qmd.file}{qmd file to render. Default is 'here::here("report.qmd")'}
\item{...}{Passed to \code{quarto::quarto_render()}}