Skip to content

Commit

Permalink
Merge pull request #129 from ljwoodley/update_render_report_for_qmd
Browse files Browse the repository at this point in the history
Update render report for qmd
  • Loading branch information
pbchase authored Jul 31, 2023
2 parents c074328 + be0fdb3 commit 0e58dbd
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 32 deletions.
39 changes: 23 additions & 16 deletions report/render_report.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,50 @@ if (!interactive()) {
stop(sprintf("Specified file, %s, does not exist", script_name))
}
} else {
script_name <- "dummy.qmd"
stop(sprintf("Specified file, %s, does not exist", script_name))
script_name <- "report/sample_report.qmd"
if(!fs::file_exists(script_name)) {
stop(sprintf("Specified file, %s, does not exist", script_name))
}
}

report_name <- word(script_name, 1, sep = "\\.")
report_type <- word(script_name, 2, sep = "\\.")

script_run_time <- set_script_run_time()
output_file <-
paste0(str_replace(report_name, ".*/", ""),
"_",
format(script_run_time, "%Y%m%d%H%M%S"),
if_else(report_type == "qmd", ".pdf", "")
)
output_filename <-
paste0(
str_replace(report_name, ".*/", ""),
"_",
format(script_run_time, "%Y%m%d%H%M%S")
)

if (report_type == "qmd") {
quarto::quarto_render(
full_path_to_output_file <- quarto::quarto_render(
script_name,
output_file = output_file,
output_file = paste0(output_filename, ".pdf"),
output_format = "pdf"
)

attachment_object <- mime_part(paste0(output_filename, ".pdf"))
} else {
render(
full_path_to_output_file <- render(
script_name,
output_file = output_file
output_file = output_filename
)
}

output_file_extension <- word(output_file, 2 , sep = "\\.")
attachment_object <- mime_part(output_file, output_file)
attachment_object <- mime_part(
full_path_to_output_file,
basename(full_path_to_output_file)
)
}

email_subject <- paste(report_name, "|", script_run_time)
email_subject <- paste(basename(report_name), "|", script_run_time)
body <- "Please see the attached report."

email_body <- list(body, attachment_object)

# send the email with the attached output file
send_email(email_body, email_subject)

set_script_name("render_report")
log_job_success(jsonlite::toJSON(script_name))
29 changes: 29 additions & 0 deletions report/sample_report.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: "sample_report"
format:
html:
toc: true
pdf:
toc: true
---

## Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.

## Running Code

When you click the **Render** button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

```{r}
1 + 1
```

You can add options to executable code like this

```{r}
#| echo: false
2 * 2
```

The `echo: false` option disables the printing of code (only output is displayed).
39 changes: 23 additions & 16 deletions study_template/report/render_report.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,50 @@ if (!interactive()) {
stop(sprintf("Specified file, %s, does not exist", script_name))
}
} else {
script_name <- "dummy.qmd"
stop(sprintf("Specified file, %s, does not exist", script_name))
script_name <- "report/sample_report.qmd"
if(!fs::file_exists(script_name)) {
stop(sprintf("Specified file, %s, does not exist", script_name))
}
}

report_name <- word(script_name, 1, sep = "\\.")
report_type <- word(script_name, 2, sep = "\\.")

script_run_time <- set_script_run_time()
output_file <-
paste0(str_replace(report_name, ".*/", ""),
"_",
format(script_run_time, "%Y%m%d%H%M%S"),
if_else(report_type == "qmd", ".pdf", "")
)
output_filename <-
paste0(
str_replace(report_name, ".*/", ""),
"_",
format(script_run_time, "%Y%m%d%H%M%S")
)

if (report_type == "qmd") {
quarto::quarto_render(
full_path_to_output_file <- quarto::quarto_render(
script_name,
output_file = output_file,
output_file = paste0(output_filename, ".pdf"),
output_format = "pdf"
)

attachment_object <- mime_part(paste0(output_filename, ".pdf"))
} else {
render(
full_path_to_output_file <- render(
script_name,
output_file = output_file
output_file = output_filename
)
}

output_file_extension <- word(output_file, 2 , sep = "\\.")
attachment_object <- mime_part(output_file, output_file)
attachment_object <- mime_part(
full_path_to_output_file,
basename(full_path_to_output_file)
)
}

email_subject <- paste(report_name, "|", script_run_time)
email_subject <- paste(basename(report_name), "|", script_run_time)
body <- "Please see the attached report."

email_body <- list(body, attachment_object)

# send the email with the attached output file
send_email(email_body, email_subject)

set_script_name("render_report")
log_job_success(jsonlite::toJSON(script_name))
29 changes: 29 additions & 0 deletions study_template/report/sample_report.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: "sample_report"
format:
html:
toc: true
pdf:
toc: true
---

## Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.

## Running Code

When you click the **Render** button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

```{r}
1 + 1
```

You can add options to executable code like this

```{r}
#| echo: false
2 * 2
```

The `echo: false` option disables the printing of code (only output is displayed).

0 comments on commit 0e58dbd

Please # to comment.