From 159750fa62a077fad05e8b182f67e80973e78f61 Mon Sep 17 00:00:00 2001 From: Andreas Gammelgaard Damsbo Date: Thu, 23 Jan 2025 08:56:32 +0100 Subject: [PATCH 1/3] first steps to finding a working path --- .../shinyapps.io/cognitiveindex/freesearcheR_dev.dcf | 10 ++++++++++ inst/apps/data_analysis_modules/www/report.qmd | 5 ++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 inst/apps/data_analysis_modules/rsconnect/shinyapps.io/cognitiveindex/freesearcheR_dev.dcf diff --git a/inst/apps/data_analysis_modules/rsconnect/shinyapps.io/cognitiveindex/freesearcheR_dev.dcf b/inst/apps/data_analysis_modules/rsconnect/shinyapps.io/cognitiveindex/freesearcheR_dev.dcf new file mode 100644 index 0000000..76b8df5 --- /dev/null +++ b/inst/apps/data_analysis_modules/rsconnect/shinyapps.io/cognitiveindex/freesearcheR_dev.dcf @@ -0,0 +1,10 @@ +name: freesearcheR_dev +title: +username: cognitiveindex +account: cognitiveindex +server: shinyapps.io +hostUrl: https://api.shinyapps.io/v1 +appId: 13786206 +bundleId: 9687600 +url: https://cognitiveindex.shinyapps.io/freesearcheR_dev/ +version: 1 diff --git a/inst/apps/data_analysis_modules/www/report.qmd b/inst/apps/data_analysis_modules/www/report.qmd index d70b84d..188b572 100644 --- a/inst/apps/data_analysis_modules/www/report.qmd +++ b/inst/apps/data_analysis_modules/www/report.qmd @@ -59,7 +59,10 @@ Below are the baseline characteristics. Below are results from the univariable and multivariable regression analyses. -{{< include _regression_table.md >}} + +```{r} +gtsummary::tbl_summary(mtcars,by=cyl) +``` ## Discussion From d51fd279965c216d0dd26169758b4c33e3414fd5 Mon Sep 17 00:00:00 2001 From: Andreas Gammelgaard Damsbo Date: Thu, 23 Jan 2025 13:21:41 +0100 Subject: [PATCH 2/3] abandoned quarto to render report using rmarkdown instead. works smoothly! --- R/helpers.R | 21 +++++++ inst/apps/data_analysis_modules/app.R | 56 +++++++++++++++---- .../cognitiveindex/freesearcheR_dev.dcf | 2 +- inst/apps/data_analysis_modules/server.R | 33 +++++++---- inst/apps/data_analysis_modules/ui.R | 2 +- .../apps/data_analysis_modules/www/report.qmd | 50 ++++++----------- .../apps/data_analysis_modules/www/report.rmd | 56 +++++++++++++++++++ 7 files changed, 165 insertions(+), 55 deletions(-) create mode 100644 inst/apps/data_analysis_modules/www/report.rmd diff --git a/R/helpers.R b/R/helpers.R index 5e21b4b..aecfce7 100644 --- a/R/helpers.R +++ b/R/helpers.R @@ -50,6 +50,27 @@ write_quarto <- function(data,...) { ) } +write_rmd <- function(data,...) { + # Exports data to temporary location + # + # I assume this is more secure than putting it in the www folder and deleting + # on session end + + # temp <- base::tempfile(fileext = ".rds") + # readr::write_rds(data, file = here) + + readr::write_rds(data, file = "www/web_data.rds") + + ## 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 + rmarkdown::render( + params = list(data.file = "web_data.rds"), + # execute_params = list(data.file = temp), + ... + ) +} + #' Flexible file import based on extension #' #' @param file file name diff --git a/inst/apps/data_analysis_modules/app.R b/inst/apps/data_analysis_modules/app.R index 54186df..ef3bf70 100644 --- a/inst/apps/data_analysis_modules/app.R +++ b/inst/apps/data_analysis_modules/app.R @@ -1160,6 +1160,27 @@ write_quarto <- function(data,...) { ) } +write_rmd <- function(data,...) { + # Exports data to temporary location + # + # I assume this is more secure than putting it in the www folder and deleting + # on session end + + # temp <- base::tempfile(fileext = ".rds") + # readr::write_rds(data, file = here) + + readr::write_rds(data, file = "www/web_data.rds") + + ## 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 + rmarkdown::render( + params = list(data.file = "web_data.rds"), + # execute_params = list(data.file = temp), + ... + ) +} + #' Flexible file import based on extension #' #' @param file file name @@ -3874,7 +3895,7 @@ ui <- bslib::page_fixed( ), shiny::p( style = "margin: 1; color: #888;", - "AG Damsbo | v", format(Sys.Date(),format = '%y%m%d')," | AGPLv3 license | ", shiny::tags$a("Source on Github", href = "https://github.com/agdamsbo/freesearcheR/", target = "_blank", rel = "noopener noreferrer") + "AG Damsbo | v", format(Sys.time(),format = '%y%m%d_%H%M')," | AGPLv3 license | ", shiny::tags$a("Source on Github", href = "https://github.com/agdamsbo/freesearcheR/", target = "_blank", rel = "noopener noreferrer") ), ) ) @@ -3916,7 +3937,7 @@ library(gtsummary) # source("functions.R") - +data(mtcars) # light <- custom_theme() # @@ -4559,11 +4580,13 @@ server <- function(input, output, session) { }) } - rv$list$regression$table <- out |> - tbl_merge() + rv$list$regression$tables <- out - gtsummary::as_kable(rv$list$regression$table) |> - readr::write_lines(file="./www/_regression_table.md") + # rv$list$regression$table <- out |> + # tbl_merge() + + # gtsummary::as_kable(rv$list$regression$table) |> + # readr::write_lines(file="./www/_regression_table.md") rv$list$input <- input }, @@ -4579,8 +4602,9 @@ server <- function(input, output, session) { ) output$table2 <- gt::render_gt({ - shiny::req(rv$list$regression$table) - rv$list$regression$table |> + shiny::req(rv$list$regression$tables) + rv$list$regression$tables |> + tbl_merge() |> gtsummary::as_gt() |> gt::tab_header(gt::md(glue::glue("**Table 2: {rv$list$regression$params$descr}**"))) }) @@ -4659,12 +4683,22 @@ server <- function(input, output, session) { # shiny::req(rv$list$regression) ## Notification is not progressing ## Presumably due to missing + + #Simplified for .rmd output attempt + format <- ifelse(type=="docx","word_document","odt_document") + shiny::withProgress(message = "Generating the report. Hold on for a moment..", { + rv$list |> - write_quarto( - output_format = type, - input = file.path(getwd(), "www/report.qmd") + write_rmd( + output_format = format, + input = file.path(getwd(), "www/report.rmd") ) + + # write_quarto( + # output_format = type, + # input = file.path(getwd(), "www/report.qmd") + # ) }) file.rename(paste0("www/report.", type), file) } diff --git a/inst/apps/data_analysis_modules/rsconnect/shinyapps.io/cognitiveindex/freesearcheR_dev.dcf b/inst/apps/data_analysis_modules/rsconnect/shinyapps.io/cognitiveindex/freesearcheR_dev.dcf index 76b8df5..568771d 100644 --- a/inst/apps/data_analysis_modules/rsconnect/shinyapps.io/cognitiveindex/freesearcheR_dev.dcf +++ b/inst/apps/data_analysis_modules/rsconnect/shinyapps.io/cognitiveindex/freesearcheR_dev.dcf @@ -5,6 +5,6 @@ account: cognitiveindex server: shinyapps.io hostUrl: https://api.shinyapps.io/v1 appId: 13786206 -bundleId: 9687600 +bundleId: 9688503 url: https://cognitiveindex.shinyapps.io/freesearcheR_dev/ version: 1 diff --git a/inst/apps/data_analysis_modules/server.R b/inst/apps/data_analysis_modules/server.R index d0206a7..94c7713 100644 --- a/inst/apps/data_analysis_modules/server.R +++ b/inst/apps/data_analysis_modules/server.R @@ -29,7 +29,7 @@ library(gtsummary) # source("functions.R") - +data(mtcars) # light <- custom_theme() # @@ -672,11 +672,13 @@ server <- function(input, output, session) { }) } - rv$list$regression$table <- out |> - tbl_merge() + rv$list$regression$tables <- out - gtsummary::as_kable(rv$list$regression$table) |> - readr::write_lines(file="./www/_regression_table.md") + # rv$list$regression$table <- out |> + # tbl_merge() + + # gtsummary::as_kable(rv$list$regression$table) |> + # readr::write_lines(file="./www/_regression_table.md") rv$list$input <- input }, @@ -692,8 +694,9 @@ server <- function(input, output, session) { ) output$table2 <- gt::render_gt({ - shiny::req(rv$list$regression$table) - rv$list$regression$table |> + shiny::req(rv$list$regression$tables) + rv$list$regression$tables |> + tbl_merge() |> gtsummary::as_gt() |> gt::tab_header(gt::md(glue::glue("**Table 2: {rv$list$regression$params$descr}**"))) }) @@ -772,12 +775,22 @@ server <- function(input, output, session) { # shiny::req(rv$list$regression) ## Notification is not progressing ## Presumably due to missing + + #Simplified for .rmd output attempt + format <- ifelse(type=="docx","word_document","odt_document") + shiny::withProgress(message = "Generating the report. Hold on for a moment..", { + rv$list |> - write_quarto( - output_format = type, - input = file.path(getwd(), "www/report.qmd") + write_rmd( + output_format = format, + input = file.path(getwd(), "www/report.rmd") ) + + # write_quarto( + # output_format = type, + # input = file.path(getwd(), "www/report.qmd") + # ) }) file.rename(paste0("www/report.", type), file) } diff --git a/inst/apps/data_analysis_modules/ui.R b/inst/apps/data_analysis_modules/ui.R index 3c883f9..c6f22cf 100644 --- a/inst/apps/data_analysis_modules/ui.R +++ b/inst/apps/data_analysis_modules/ui.R @@ -510,7 +510,7 @@ ui <- bslib::page_fixed( ), shiny::p( style = "margin: 1; color: #888;", - "AG Damsbo | v", format(Sys.Date(),format = '%y%m%d')," | AGPLv3 license | ", shiny::tags$a("Source on Github", href = "https://github.com/agdamsbo/freesearcheR/", target = "_blank", rel = "noopener noreferrer") + "AG Damsbo | v", format(Sys.time(),format = '%y%m%d_%H%M')," | AGPLv3 license | ", shiny::tags$a("Source on Github", href = "https://github.com/agdamsbo/freesearcheR/", target = "_blank", rel = "noopener noreferrer") ), ) ) diff --git a/inst/apps/data_analysis_modules/www/report.qmd b/inst/apps/data_analysis_modules/www/report.qmd index 188b572..7715c24 100644 --- a/inst/apps/data_analysis_modules/www/report.qmd +++ b/inst/apps/data_analysis_modules/www/report.qmd @@ -13,34 +13,17 @@ params: ```{r} #| message: false #| warning: false -# if (!requireNamespace("gtsummary")){ -# install.packages("gtsummary") -# } else { -# library(gtsummary) -# } -# -# if (!requireNamespace("gt")){ -# install.packages("gt") -# } else { -# library(gt) -# } -# -# if (!requireNamespace("readr")){ -# install.packages("readr") -# } else { -# library(readr) -# } -# requireNamespace("gtsummary") -# requireNamespace("gt") -# require(gt) -# require(flextable) -# if (!requireNamespace("readr")){ -# install.packages("readr") -# } web_data <- readr::read_rds(file = params$data.file) -# library(gt) -# library(flextable) -# library(freesearcheR) +library(gtsummary) +library(gt) + +tbl_merge <- function(data) { + if (is.null(names(data))) { + data |> gtsummary::tbl_merge() + } else { + data |> gtsummary::tbl_merge(tab_spanner = names(data)) + } +} ``` ## Introduction @@ -55,13 +38,16 @@ Analyses were conducted in the *freesearcheR* data analysis web-tool based on R Below are the baseline characteristics. -{{< include _table1.md >}} +```{r, results = 'asis'} +tbl <- gtsummary::as_gt(web_data$table1) +knitr::knit_print(tbl) +``` -Below are results from the univariable and multivariable regression analyses. +Below are the results from the - -```{r} -gtsummary::tbl_summary(mtcars,by=cyl) +```{r, results = 'asis'} +reg_tbl <- web_data$regression$tables +knitr::knit_print(tbl_merge(reg_tbl)) ``` ## Discussion diff --git a/inst/apps/data_analysis_modules/www/report.rmd b/inst/apps/data_analysis_modules/www/report.rmd new file mode 100644 index 0000000..5bad138 --- /dev/null +++ b/inst/apps/data_analysis_modules/www/report.rmd @@ -0,0 +1,56 @@ +--- +title: "freesearcheR analysis results" +date: today +format: docx +author: freesearcheR Tool +toc: false +params: + data.file: NA +--- + +```{r setup, echo = FALSE} +knitr::opts_chunk$set(echo = FALSE, message=FALSE, warning=FALSE) +``` + + +```{r} +web_data <- readr::read_rds(file = params$data.file) +library(gtsummary) +library(gt) + +tbl_merge <- function(data) { + if (is.null(names(data))) { + data |> gtsummary::tbl_merge() + } else { + data |> gtsummary::tbl_merge(tab_spanner = names(data)) + } +} +``` + +## Introduction + +Research should be free and open with easy access for all. The freesearcheR tool attempts to help lower the bar to participate in contributing to science by making guided data analysis easily accessible in the web-browser. + +## Methods + +Analyses were conducted in the *freesearcheR* data analysis web-tool based on R version 4.4.1. + +## Results + +Below are the baseline characteristics. + +```{r, results = 'asis'} +tbl <- gtsummary::as_gt(web_data$table1) +knitr::knit_print(tbl) +``` + +Below are the results from the + +```{r, results = 'asis'} +reg_tbl <- web_data$regression$tables +knitr::knit_print(tbl_merge(reg_tbl)) +``` + +## Discussion + +Good luck on your further work! From 2554ce50a4274de3c182f529630fc3cba68a9b38 Mon Sep 17 00:00:00 2001 From: Andreas Gammelgaard Damsbo Date: Thu, 23 Jan 2025 14:25:04 +0100 Subject: [PATCH 3/3] added regression types to text --- .../cognitiveindex/freesearcheR_dev.dcf | 2 +- inst/apps/data_analysis_modules/www/report.rmd | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/inst/apps/data_analysis_modules/rsconnect/shinyapps.io/cognitiveindex/freesearcheR_dev.dcf b/inst/apps/data_analysis_modules/rsconnect/shinyapps.io/cognitiveindex/freesearcheR_dev.dcf index 568771d..ab5be8e 100644 --- a/inst/apps/data_analysis_modules/rsconnect/shinyapps.io/cognitiveindex/freesearcheR_dev.dcf +++ b/inst/apps/data_analysis_modules/rsconnect/shinyapps.io/cognitiveindex/freesearcheR_dev.dcf @@ -5,6 +5,6 @@ account: cognitiveindex server: shinyapps.io hostUrl: https://api.shinyapps.io/v1 appId: 13786206 -bundleId: 9688503 +bundleId: 9688582 url: https://cognitiveindex.shinyapps.io/freesearcheR_dev/ version: 1 diff --git a/inst/apps/data_analysis_modules/www/report.rmd b/inst/apps/data_analysis_modules/www/report.rmd index 5bad138..f314587 100644 --- a/inst/apps/data_analysis_modules/www/report.rmd +++ b/inst/apps/data_analysis_modules/www/report.rmd @@ -9,7 +9,7 @@ params: --- ```{r setup, echo = FALSE} -knitr::opts_chunk$set(echo = FALSE, message=FALSE, warning=FALSE) +knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE) ``` @@ -25,6 +25,18 @@ tbl_merge <- function(data) { data |> gtsummary::tbl_merge(tab_spanner = names(data)) } } + +vec2sentence <- function(data, sep.word = "and") { + sep.word <- paste0(" ", gsub(" ", "", sep.word), " ") + if (length(data) < 2) { + out <- data + } else if (length(data) == 2) { + out <- paste(data, collapse = sep.word) + } else { + out <- paste(paste(data[-length(data)], collapse = ","), data[length(data)], sep = sep.word) + } + return(out) +} ``` ## Introduction @@ -44,7 +56,7 @@ tbl <- gtsummary::as_gt(web_data$table1) knitr::knit_print(tbl) ``` -Below are the results from the +Below are the results from the `r tolower(vec2sentence(names(web_data$regression$tables)))` `r web_data$regression$params$descr`. ```{r, results = 'asis'} reg_tbl <- web_data$regression$tables