mirror of
https://github.com/agdamsbo/FreesearchR.git
synced 2025-09-12 09:59:39 +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
|
@ -4,3 +4,4 @@
|
||||||
^\.Rproj\.user$
|
^\.Rproj\.user$
|
||||||
^LICENSE\.md$
|
^LICENSE\.md$
|
||||||
^dev$
|
^dev$
|
||||||
|
.github
|
||||||
|
|
|
@ -22,9 +22,12 @@ Imports:
|
||||||
readODS,
|
readODS,
|
||||||
readr,
|
readr,
|
||||||
shiny,
|
shiny,
|
||||||
MASS
|
MASS,
|
||||||
|
REDCapCAST,
|
||||||
|
purrr
|
||||||
Suggests:
|
Suggests:
|
||||||
styler,
|
styler,
|
||||||
devtools
|
devtools,
|
||||||
|
rhub
|
||||||
URL: https://github.com/agdamsbo/webResearch
|
URL: https://github.com/agdamsbo/webResearch
|
||||||
BugReports: https://github.com/agdamsbo/webResearch/issues
|
BugReports: https://github.com/agdamsbo/webResearch/issues
|
||||||
|
|
|
@ -2,11 +2,17 @@
|
||||||
|
|
||||||
export(argsstring2list)
|
export(argsstring2list)
|
||||||
export(baseline_table)
|
export(baseline_table)
|
||||||
|
export(default_format_arguments)
|
||||||
|
export(factorize)
|
||||||
|
export(format_writer)
|
||||||
export(getfun)
|
export(getfun)
|
||||||
|
export(index_embed)
|
||||||
|
export(modify_qmd)
|
||||||
export(panel_space)
|
export(panel_space)
|
||||||
export(read_input)
|
export(read_input)
|
||||||
export(regression_model)
|
export(regression_model)
|
||||||
export(regression_table)
|
export(regression_table)
|
||||||
export(shiny_webResearch)
|
export(shiny_webResearch)
|
||||||
|
export(specify_qmd_format)
|
||||||
export(write_quarto)
|
export(write_quarto)
|
||||||
importFrom(stats,as.formula)
|
importFrom(stats,as.formula)
|
||||||
|
|
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
|
#' Wrapper to save data in RDS, load into specified qmd and render
|
||||||
#'
|
#'
|
||||||
#' @param data list to pass to qmd
|
#' @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()`
|
#' @param ... Passed to `quarto::quarto_render()`
|
||||||
#'
|
#'
|
||||||
#' @return output file name
|
#' @return output file name
|
||||||
#' @export
|
#' @export
|
||||||
#'
|
#'
|
||||||
write_quarto <- function(data,fileformat=c("html","docx","odt","pdf","all"),qmd.file=here::here("report.qmd"),...){
|
write_quarto <- function(data,...){
|
||||||
fileformat <- match.arg(fileformat)
|
|
||||||
# Exports data to temporary location
|
# Exports data to temporary location
|
||||||
#
|
#
|
||||||
# I assume this is more secure than putting it in the www folder and deleting
|
# 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
|
## Specifying a output path will make the rendering fail
|
||||||
## Ref: https://github.com/quarto-dev/quarto-cli/discussions/4041
|
## Ref: https://github.com/quarto-dev/quarto-cli/discussions/4041
|
||||||
## Outputs to the same as the .qmd file
|
## Outputs to the same as the .qmd file
|
||||||
quarto::quarto_render(qmd.file,
|
quarto::quarto_render(execute_params = list(data.file=temp),
|
||||||
output_format = fileformat,
|
|
||||||
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)))
|
||||||
|
}
|
|
@ -5,6 +5,6 @@ account: agdamsbo
|
||||||
server: shinyapps.io
|
server: shinyapps.io
|
||||||
hostUrl: https://api.shinyapps.io/v1
|
hostUrl: https://api.shinyapps.io/v1
|
||||||
appId: 13276335
|
appId: 13276335
|
||||||
bundleId:
|
bundleId: 9402292
|
||||||
url: https://agdamsbo.shinyapps.io/webResearch/
|
url: https://agdamsbo.shinyapps.io/webResearch/
|
||||||
version: 1
|
version: 1
|
||||||
|
|
|
@ -162,9 +162,25 @@ server <- function(input, output, session) {
|
||||||
v$list$table2 |>
|
v$list$table2 |>
|
||||||
gtsummary::as_gt()
|
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({
|
output$uploaded <- shiny::reactive({
|
||||||
if (is.null(v$ds)) {
|
if (is.null(v$ds)) {
|
||||||
"no"
|
"no"
|
||||||
|
@ -195,8 +211,8 @@ server <- function(input, output, session) {
|
||||||
content = function(file, type = input$output_type) {
|
content = function(file, type = input$output_type) {
|
||||||
v$list |>
|
v$list |>
|
||||||
write_quarto(
|
write_quarto(
|
||||||
fileformat = type,
|
output_format = type,
|
||||||
qmd.file = file.path(getwd(), "www/report.qmd")
|
input = file.path(getwd(), "www/report.qmd")
|
||||||
)
|
)
|
||||||
file.rename(paste0("www/report.", type), file)
|
file.rename(paste0("www/report.", type), file)
|
||||||
}
|
}
|
||||||
|
@ -210,5 +226,5 @@ server <- function(input, output, session) {
|
||||||
print(paste(.x, "deleted"))
|
print(paste(.x, "deleted"))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
#
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,19 @@ panels <- list(
|
||||||
|
|
||||||
|
|
||||||
ui <- bslib::page(
|
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",
|
title = "webResearcher for easy data analysis",
|
||||||
bslib::page_navbar(
|
bslib::page_navbar(
|
||||||
title = "webResearcher",
|
title = "webResearcher",
|
||||||
|
@ -159,7 +171,8 @@ ui <- bslib::page(
|
||||||
choices = list(
|
choices = list(
|
||||||
"Word" = "docx",
|
"Word" = "docx",
|
||||||
"LibreOffice" = "odt",
|
"LibreOffice" = "odt",
|
||||||
"PDF" = "pdf"
|
"PDF" = "pdf",
|
||||||
|
"All the above" = "all"
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,13 @@
|
||||||
---
|
---
|
||||||
|
format:
|
||||||
|
html:
|
||||||
|
embed-resources: true
|
||||||
title: "webResearch analysis results"
|
title: "webResearch analysis results"
|
||||||
date: today
|
date: today
|
||||||
author: webResearch Tool
|
author: webResearch Tool
|
||||||
toc: true
|
toc: true
|
||||||
execute:
|
execute:
|
||||||
echo: false
|
echo: false
|
||||||
format:
|
|
||||||
html:
|
|
||||||
embed-resources: true
|
|
||||||
docx: default
|
|
||||||
odt: default
|
|
||||||
pdf: default
|
|
||||||
params:
|
params:
|
||||||
data.file: NA
|
data.file: NA
|
||||||
---
|
---
|
||||||
|
|
BIN
inst/apps/data_analysis/www/report_format.pdf
Normal file
BIN
inst/apps/data_analysis/www/report_format.pdf
Normal file
Binary file not shown.
14
man/default_format_arguments.Rd
Normal file
14
man/default_format_arguments.Rd
Normal 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
19
man/factorize.Rd
Normal 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
19
man/format_writer.Rd
Normal 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
21
man/index_embed.Rd
Normal 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
19
man/modify_qmd.Rd
Normal 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
19
man/specify_qmd_format.Rd
Normal 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
|
||||||
|
}
|
|
@ -4,18 +4,11 @@
|
||||||
\alias{write_quarto}
|
\alias{write_quarto}
|
||||||
\title{Wrapper to save data in RDS, load into specified qmd and render}
|
\title{Wrapper to save data in RDS, load into specified qmd and render}
|
||||||
\usage{
|
\usage{
|
||||||
write_quarto(
|
write_quarto(data, qmd.file = here::here("report.qmd"), ...)
|
||||||
data,
|
|
||||||
fileformat = c("html", "docx", "odt", "pdf", "all"),
|
|
||||||
qmd.file = here::here("report.qmd"),
|
|
||||||
...
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{data}{list to pass to qmd}
|
\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{qmd.file}{qmd file to render. Default is 'here::here("report.qmd")'}
|
||||||
|
|
||||||
\item{...}{Passed to \code{quarto::quarto_render()}}
|
\item{...}{Passed to \code{quarto::quarto_render()}}
|
||||||
|
|
Loading…
Add table
Reference in a new issue