Skip to content

Commit

Permalink
Merge pull request #1 from agdamsbo/report
Browse files Browse the repository at this point in the history
Report export solved
  • Loading branch information
agdamsbo authored Jan 23, 2025
2 parents 32bdea6 + 2554ce5 commit d113845
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 52 deletions.
21 changes: 21 additions & 0 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 45 additions & 11 deletions inst/apps/data_analysis_modules/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -3882,7 +3903,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")
),
)
)
Expand Down Expand Up @@ -3924,7 +3945,7 @@ library(gtsummary)

# source("functions.R")


data(mtcars)

# light <- custom_theme()
#
Expand Down Expand Up @@ -4568,11 +4589,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
},
Expand All @@ -4588,8 +4611,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}**")))
})
Expand Down Expand Up @@ -4668,12 +4692,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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: freesearcheR_dev
title:
username: cognitiveindex
account: cognitiveindex
server: shinyapps.io
hostUrl: https://api.shinyapps.io/v1
appId: 13786206
bundleId: 9688582
url: https://cognitiveindex.shinyapps.io/freesearcheR_dev/
version: 1
33 changes: 23 additions & 10 deletions inst/apps/data_analysis_modules/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ library(gtsummary)

# source("functions.R")


data(mtcars)

# light <- custom_theme()
#
Expand Down Expand Up @@ -673,11 +673,13 @@ server <- function(input, output, session) {
})
}

rv$list$regression$table <- out |>
tbl_merge()
rv$list$regression$tables <- out

# rv$list$regression$table <- out |>
# tbl_merge()

gtsummary::as_kable(rv$list$regression$table) |>
readr::write_lines(file="./www/_regression_table.md")
# gtsummary::as_kable(rv$list$regression$table) |>
# readr::write_lines(file="./www/_regression_table.md")

rv$list$input <- input
},
Expand All @@ -693,8 +695,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}**")))
})
Expand Down Expand Up @@ -773,12 +776,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)
}
Expand Down
2 changes: 1 addition & 1 deletion inst/apps/data_analysis_modules/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,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")
),
)
)
Expand Down
49 changes: 19 additions & 30 deletions inst/apps/data_analysis_modules/www/report.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -55,11 +38,17 @@ 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

{{< include _regression_table.md >}}
```{r, results = 'asis'}
reg_tbl <- web_data$regression$tables
knitr::knit_print(tbl_merge(reg_tbl))
```

## Discussion

Expand Down
68 changes: 68 additions & 0 deletions inst/apps/data_analysis_modules/www/report.rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
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))
}
}
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

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 tolower(vec2sentence(names(web_data$regression$tables)))` `r web_data$regression$params$descr`.

```{r, results = 'asis'}
reg_tbl <- web_data$regression$tables
knitr::knit_print(tbl_merge(reg_tbl))
```

## Discussion

Good luck on your further work!

0 comments on commit d113845

Please sign in to comment.