diff --git a/.Rbuildignore b/.Rbuildignore index ce430ed..9eddb9d 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -4,3 +4,4 @@ ^\.Rproj\.user$ ^LICENSE\.md$ ^dev$ +.github diff --git a/DESCRIPTION b/DESCRIPTION index 11a9987..487366f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 diff --git a/NAMESPACE b/NAMESPACE index 35f70a3..a0f47dc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/helpers.R b/R/helpers.R index 14cb1fb..c965a7e 100644 --- a/R/helpers.R +++ b/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), ... ) } diff --git a/R/report.R b/R/report.R new file mode 100644 index 0000000..1061bce --- /dev/null +++ b/R/report.R @@ -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))) +} diff --git a/inst/apps/data_analysis/rsconnect/shinyapps.io/agdamsbo/webResearch.dcf b/inst/apps/data_analysis/rsconnect/shinyapps.io/agdamsbo/webResearch.dcf index e27fef2..9093a42 100644 --- a/inst/apps/data_analysis/rsconnect/shinyapps.io/agdamsbo/webResearch.dcf +++ b/inst/apps/data_analysis/rsconnect/shinyapps.io/agdamsbo/webResearch.dcf @@ -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 diff --git a/inst/apps/data_analysis/server.R b/inst/apps/data_analysis/server.R index d30f23c..dfac171 100644 --- a/inst/apps/data_analysis/server.R +++ b/inst/apps/data_analysis/server.R @@ -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")) }) }) - # + } diff --git a/inst/apps/data_analysis/ui.R b/inst/apps/data_analysis/ui.R index 3ee8968..6ce36b6 100644 --- a/inst/apps/data_analysis/ui.R +++ b/inst/apps/data_analysis/ui.R @@ -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" ) ), diff --git a/inst/apps/data_analysis/www/report.qmd b/inst/apps/data_analysis/www/report.qmd index d65dbee..931b29a 100644 --- a/inst/apps/data_analysis/www/report.qmd +++ b/inst/apps/data_analysis/www/report.qmd @@ -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 --- diff --git a/inst/apps/data_analysis/www/report_format.pdf b/inst/apps/data_analysis/www/report_format.pdf new file mode 100644 index 0000000..73dad5e Binary files /dev/null and b/inst/apps/data_analysis/www/report_format.pdf differ diff --git a/man/default_format_arguments.Rd b/man/default_format_arguments.Rd new file mode 100644 index 0000000..169f8cc --- /dev/null +++ b/man/default_format_arguments.Rd @@ -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 +} diff --git a/man/factorize.Rd b/man/factorize.Rd new file mode 100644 index 0000000..d66412b --- /dev/null +++ b/man/factorize.Rd @@ -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 +} diff --git a/man/format_writer.Rd b/man/format_writer.Rd new file mode 100644 index 0000000..00bb1ad --- /dev/null +++ b/man/format_writer.Rd @@ -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 +} diff --git a/man/index_embed.Rd b/man/index_embed.Rd new file mode 100644 index 0000000..80e761d --- /dev/null +++ b/man/index_embed.Rd @@ -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 +} diff --git a/man/modify_qmd.Rd b/man/modify_qmd.Rd new file mode 100644 index 0000000..d040691 --- /dev/null +++ b/man/modify_qmd.Rd @@ -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 +} diff --git a/man/specify_qmd_format.Rd b/man/specify_qmd_format.Rd new file mode 100644 index 0000000..3be852a --- /dev/null +++ b/man/specify_qmd_format.Rd @@ -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 +} diff --git a/man/write_quarto.Rd b/man/write_quarto.Rd index 7696d7e..00939b4 100644 --- a/man/write_quarto.Rd +++ b/man/write_quarto.Rd @@ -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()}}