Skip to content

Commit e5f69bd

Browse files
committed
make a start on #37
1 parent 8d44201 commit e5f69bd

File tree

5 files changed

+78
-52
lines changed

5 files changed

+78
-52
lines changed

R/create_template.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ create_template <- function(path, name, common_objects, modules, author,
328328
# Create global ====
329329

330330
full_component_list <- c(components$component, "rep")
331+
names(full_component_list) <- c(components$long_component, "Reproduce")
331332

332333
global_params <- c(
333334
file = system.file("app_skeleton", "global.Rmd", package = "shinyscholar"),

inst/shiny/Rmd/quarto_header.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
format:
3+
html:
4+
embed-resources: true
5+
code-overflow: wrap
6+
code-fold: true
7+
code-summary: "View code chunk"
8+
page-layout: article
9+
grid:
10+
body-width: 1200px
11+
fig-format: svg
12+
execute:
13+
warning: false
14+
message: false
15+
theme: spacelab
16+
---
17+
18+
::: {.panel-tabset}
19+
20+
## Introduction

inst/shiny/Rmd/userReport_intro.Rmd

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
---
2-
title: shinyscholar session `r Sys.Date()`
3-
---
4-
51
```{r, include=FALSE}
62
library(knitr)
73
knit_engines$set(asis = function(options) {
@@ -12,11 +8,11 @@ knitr::opts_chunk$set(message = FALSE, warning = FALSE, eval = FALSE)
128

139
Please find below the R code history from your *shinyscholar* v0.4.2 session.
1410

15-
You can reproduce your session results by running this R Markdown file in RStudio.
11+
You can reproduce your session results by running this quarto file in RStudio.
1612

17-
Each code block is called a "chunk", and you can run them either one-by-one or all at once by choosing an option in the "Run" menu at the top-right corner of the "Source" pane in RStudio. The file can also be rendered into an html file using `rmarkdown::render()` which will contain all of the outputs alongside the code used to generate them.
13+
Each code block is called a "chunk", and you can run them either one-by-one or all at once by choosing an option in the "Run" menu at the top-right corner of the "Source" pane in RStudio. The file can also be rendered into an html file using `quarto::quarto_render()` which will contain all of the outputs alongside the code used to generate them.
1814

19-
For more detailed information see <http://rmarkdown.rstudio.com>.
15+
For more detailed information see <https://quarto.org/>.
2016

2117
### Package installation
2218

inst/shiny/global.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ source("ui_helpers.R")
1515

1616
# The components that have modules. These names must match the values of the
1717
# tabs of the components in the UI.
18-
COMPONENTS <- c("select", "plot", "rep", "template")
18+
COMPONENTS <- c(`Select data` = "select", `Plot data` = "plot", Reproduce = "rep", Template = "template")
1919

2020
# Information about modules that various parts of the app need access to
2121
COMPONENT_MODULES <- list()

inst/shiny/modules/rep_markdown.R

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
rep_markdown_module_ui <- function(id) {
22
ns <- shiny::NS(id)
33
tagList(
4-
selectInput(ns("rmdFileType"), label = "Select download file type",
5-
choices = c("Rmd" = ".Rmd", "PDF" = ".pdf", "HTML" = ".html", "Word" = ".docx")),
4+
selectInput(ns("file_type"), label = "Select download file type",
5+
choices = c("HTML" = ".html", "Quarto" = ".qmd")),
6+
conditionalPanel("input.file_type == '.html'",
7+
bslib::input_switch(ns("render"), "Include outputs?", value = TRUE), ns = ns),
68
downloadButton(ns("download"), "Download session code")
79
)
810
}
@@ -44,6 +46,10 @@ rep_markdown_module_server <- function(id, common, parent_session, map, COMPONEN
4446
)
4547
module_rmd <- do.call(knitr::knit_expand, knit_params)
4648

49+
# add a section header to create tabs
50+
full_component_name <- names(COMPONENTS[COMPONENTS == component])
51+
module_rmd <- c(glue::glue("## {full_component_name}"), module_rmd)
52+
4753
module_rmd_file <- tempfile(pattern = paste0(module$id, "_"),
4854
fileext = ".Rmd")
4955
writeLines(module_rmd, module_rmd_file)
@@ -67,55 +73,58 @@ rep_markdown_module_server <- function(id, common, parent_session, map, COMPONEN
6773
lapply(paste, collapse = "\n") |>
6874
paste(collapse = "\n\n")
6975

70-
result_file <- tempfile(pattern = "result_", fileext = input$rmdFileType)
71-
if (input$rmdFileType == ".Rmd") {
72-
combined_rmd <- gsub("``` r", "```{r}", combined_md)
73-
combined_rmd <- unlist(strsplit(combined_rmd , "\n"))
76+
combined_rmd <- gsub("``` r", "```{r}", combined_md)
77+
combined_rmd <- unlist(strsplit(combined_rmd , "\n"))
7478

75-
# add title section
76-
header <- c("---", paste0("title: ", combined_rmd[1]), "---")
77-
combined_rmd <- append(combined_rmd, header, after = 0)
78-
combined_rmd <- combined_rmd[-c(4, 5)]
79+
# remove ## for unused components and duplicates
80+
is_tag <- grepl("^## ", combined_rmd)
81+
tag_names <- sub("^## ", "", combined_rmd)
82+
if (!is.null(names(common$meta))){
83+
used_components <- unique(sapply(strsplit(names(common$meta), "_"), function(x) x[1]))
84+
} else {
85+
used_components <- c()
86+
}
87+
used_full_components <- names(COMPONENTS[COMPONENTS %in% used_components])
88+
lines_to_keep <- (!is_tag) | (tag_names %in% used_full_components & !duplicated(tag_names))
89+
combined_rmd <- combined_rmd[lines_to_keep]
7990

80-
# convert chunk control lines
81-
chunk_control_lines <- grep("\\{r,", combined_rmd)
82-
if (length(chunk_control_lines) > 0){
83-
chunk_starts <- grep("```\\{r\\}", combined_rmd)
84-
chunks_to_remove <- NA
85-
for (i in seq_along(chunk_control_lines)) {
86-
chunks_to_remove[i] <- min(chunk_starts[chunk_starts > chunk_control_lines[i]])
87-
}
88-
if (any(!is.na(chunks_to_remove))){
89-
combined_rmd <- combined_rmd[-chunks_to_remove]
90-
}
91-
combined_rmd <- gsub("\\{r,", "```{r,", combined_rmd)
92-
}
91+
# add quarto header
92+
quarto_header <- readLines("Rmd/quarto_header.txt")
93+
quarto_header <- append(quarto_header, glue::glue("title: shinyscholar Session {Sys.Date()}"), 1)
94+
combined_rmd <- c(quarto_header, combined_rmd)
9395

94-
# fix any very long lines
95-
long_lines <- which(nchar(combined_rmd) > 4000)
96-
for (l in long_lines){
97-
split_lines <- strwrap(combined_rmd[l], 4000)
98-
combined_rmd <- combined_rmd[-l]
99-
combined_rmd <- append(combined_rmd, split_lines, l-1)
96+
# convert chunk control lines
97+
chunk_control_lines <- grep("\\{r,", combined_rmd)
98+
if (length(chunk_control_lines) > 0){
99+
chunk_starts <- grep("```\\{r\\}", combined_rmd)
100+
chunks_to_remove <- NA
101+
for (i in seq_along(chunk_control_lines)) {
102+
chunks_to_remove[i] <- min(chunk_starts[chunk_starts > chunk_control_lines[i]])
103+
}
104+
if (any(!is.na(chunks_to_remove))){
105+
combined_rmd <- combined_rmd[-chunks_to_remove]
100106
}
107+
combined_rmd <- gsub("\\{r,", "```{r,", combined_rmd)
108+
}
109+
110+
# fix any very long lines
111+
long_lines <- which(nchar(combined_rmd) > 4000)
112+
for (l in long_lines){
113+
split_lines <- strwrap(combined_rmd[l], 4000)
114+
combined_rmd <- combined_rmd[-l]
115+
combined_rmd <- append(combined_rmd, split_lines, l-1)
116+
}
101117

118+
result_file <- paste0("combined", input$file_type)
119+
if (input$file_type == ".qmd") {
102120
writeLines(combined_rmd, result_file, useBytes = TRUE)
103121
} else {
104-
combined_md_file <- tempfile(pattern = "combined_", fileext = ".md")
105-
writeLines(combined_md, combined_md_file)
106-
rmarkdown::render(
107-
input = combined_md_file,
108-
output_format =
109-
switch(
110-
input$rmdFileType,
111-
".pdf" = rmarkdown::pdf_document(),
112-
".html" = rmarkdown::html_document(),
113-
".docx" = rmarkdown::word_document()
114-
),
115-
output_file = result_file,
116-
clean = TRUE,
117-
encoding = "UTF-8"
118-
)
122+
writeLines(combined_rmd, "combined.qmd")
123+
on.exit(unlink("combined.qmd"))
124+
quarto::quarto_render(
125+
input = "combined.qmd",
126+
output_format = "html",
127+
execute = input$render)
119128
}
120129

121130
file.rename(result_file, file)

0 commit comments

Comments
 (0)