-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge pull request #86 from shawntschwartz/67-feat-report
67 feature: html/pdf report generator and renderer pipeline
- Loading branch information
Showing
20 changed files
with
366 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
make_report <- function(eyeris, out, plots, ...) { | ||
# get extra subject params from bidsify.R | ||
params <- list(...) | ||
|
||
# temp file | ||
rmd_f <- file.path(out, paste0("sub-", params$sub, "_", | ||
"run-", params$run, ".Rmd")) | ||
|
||
|
||
report_date <- format(Sys.time(), "%B %d, %Y | %H:%M:%OS3") | ||
package_version <- as.character( | ||
utils::packageVersion("eyeris") | ||
) | ||
css <- system.file( | ||
file.path("rmarkdown", "css", "report.css"), | ||
package = "eyeris" | ||
) | ||
|
||
sticker_path <- system.file("figures", "sticker.png", package = "eyeris") | ||
|
||
# eyeris report content | ||
content <- paste0( | ||
"---\n", | ||
"title: '`eyeris` report'\n", | ||
"date: '", report_date, "'\n", | ||
"output:\n", | ||
" html_document:\n", | ||
" df_print: paged\n", | ||
" css: '", css, "'\n", | ||
" pdf_document: default\n", | ||
"---\n\n", | ||
"\n\n<img src='", sticker_path, "' class='top-right-image'>", | ||
"\n\n---\n\n## Summary\n", | ||
" - Subject ID: ", params$sub, "\n", | ||
" - Session: ", params$ses, "\n", | ||
" - Task: ", params$task, "\n", | ||
" - Run: ", params$run, "\n", | ||
" - BIDS Directory: ", out, "\n", | ||
" - Source `.asc` file: ", eyeris$file, "\n", | ||
" - [`eyeris` version](https://github.com/shawntschwartz/eyeris): ", | ||
package_version, "\n", | ||
|
||
"\n## Preprocessed Data Preview\n\n", | ||
print_plots(plots), "\n", | ||
|
||
"\n\n---\n\n## EyeLink Header Metadata\n\n", | ||
make_md_table(eyeris$info), "\n", | ||
|
||
"\n\n---\n\n### Citation\n\n", | ||
"```{r citation, echo=FALSE, comment=NA}\n", | ||
"citation('eyeris')\n", | ||
"```\n\n" | ||
) | ||
|
||
writeLines(content, con = rmd_f) | ||
|
||
return(rmd_f) | ||
} | ||
|
||
render_report <- function(rmd_f, html, pdf) { | ||
rmarkdown::render(rmd_f, output_format = "html_document") | ||
|
||
if (pdf) { | ||
tryCatch({ | ||
rmarkdown::render(rmd_f, output_format = "pdf_document") | ||
}, error = function(e) { | ||
cli::cli_alert_danger(paste("Could not render eyeris report PDF.", | ||
"Do you have a TeX distribution installed?", | ||
"If not, consider TinyTeX:\n", | ||
"## install.packages('tinytex')\n", | ||
"## tinytex::install_tinytex()")) | ||
base_file <- tools::file_path_sans_ext(rmd_f) | ||
unlink(paste0(base_file, ".log")) | ||
unlink(paste0(base_file, ".tex")) | ||
}) | ||
} | ||
|
||
unlink(rmd_f) | ||
} | ||
|
||
# parse eyelink `info` metadata into a markdown table | ||
make_md_table <- function(df) { | ||
md_table <- "| Property | Value |\n|----|----|\n" | ||
for (prop in colnames(df)) { | ||
val <- df[[1, prop]] | ||
md_table <- paste0(md_table, | ||
"| ", | ||
prop, | ||
" | ", | ||
val, | ||
" |\n") | ||
} | ||
|
||
return(md_table) | ||
} | ||
|
||
print_plots <- function(plots) { | ||
md_plots <- "" | ||
num_plots <- length(plots) | ||
before_plot_index <- num_plots - 1 | ||
after_plot_index <- num_plots | ||
|
||
for (i in seq_along(plots)) { | ||
relative_fig_paths <- file.path("source", "figures", basename(plots)) | ||
|
||
if (i < before_plot_index) { | ||
md_plots <- paste0(md_plots, | ||
"### Step ", i, "\n", | ||
"![](", relative_fig_paths[i], ")\n\n") | ||
} else if (i == before_plot_index) { | ||
md_plots <- paste0(md_plots, | ||
"### Before", "\n", | ||
"![](", relative_fig_paths[i], ")\n\n") | ||
} else { | ||
md_plots <- paste0(md_plots, | ||
"### After", "\n", | ||
"![](", relative_fig_paths[i], ")\n\n") | ||
} | ||
|
||
} | ||
|
||
return(md_plots) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#' `eyeris` package logo (hexagon sticker) | ||
#' | ||
#' @format A png image file that is rendered into the README and html reports. | ||
#' | ||
#' @keywords eyeris, sticker, logo, hex | ||
"sticker" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Attributions | ||
|
||
--- | ||
|
||
* Eye by Mashudi from [Noun Project](https://thenounproject.com/browse/icons/term/eye/) (CC BY 3.0) | ||
|
||
* Eye by Skena Grafis from [Noun Project](https://thenounproject.com/browse/icons/term/eye/) (CC BY 3.0) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
## code to prepare `sticker` dataset goes here | ||
|
||
sysfonts::font_add_google("Nunito") | ||
|
||
showtext::showtext_auto() | ||
|
||
imgurl <- file.path("data-raw", "noun-eye-7147281.png") | ||
img <- png::readPNG(imgurl) | ||
img_grob <- grid::rasterGrob(img, interpolate = TRUE) | ||
|
||
sine_wave <- ggplot2::ggplot(data = data.frame( | ||
x = seq(0, 4 * pi, length.out = 500) | ||
), ggplot2::aes(x = x)) + | ||
ggplot2::geom_line(ggplot2::aes(y = sin(x / 2)), | ||
size = 4, linetype = 1, | ||
color = "white", alpha = 0 | ||
) + | ||
ggplot2::theme_void() + | ||
ggimage::theme_transparent() | ||
|
||
combined_plot <- cowplot::ggdraw() + | ||
cowplot::draw_plot(sine_wave, 0, 0.75, 1, 1, scale = 4) + | ||
cowplot::draw_grob(img_grob, 0, -0.1, 1, 1, 2.75) | ||
|
||
sticker <- hexSticker::sticker( | ||
combined_plot, | ||
package = "EYERIS", | ||
h_fill = "#820000", h_color = "#820000", | ||
s_x = 1, s_y = 1.28, s_width = 0.35, | ||
p_x = 1, p_y = 0.78, p_size = 22, p_family = "Nunito", | ||
filename = "inst/figures/sticker.png" | ||
) | ||
|
||
sticker | ||
|
||
usethis::use_data(sticker, overwrite = TRUE) |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.