Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions R/z_knitr.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ knit_print.animint <- function(x, options, ...) {
viz_id <- gsub("[^[:alnum:]]", "", options$label)
out.dir <- file.path(output.dir, viz_id)
animint2dir(x, out.dir = out.dir, open.browser = FALSE)
res <- if(knitr::is_latex_output())sprintf(
"\\IfFileExists{%s/Capture.PNG}{\\includegraphics[width=\\textwidth]{%s/Capture.PNG}}{}", out.dir, out.dir
) else sprintf(
if(knitr::is_latex_output()){
res <- sprintf(
"\\IfFileExists{%s/Capture.PNG}{\\includegraphics[width=\\textwidth]{%s/Capture.PNG}}{}", out.dir, out.dir)
}else{
res <- sprintf(
## <div id="Ch01vizKeeling"></div><script>var Ch01vizKeeling = new animint("#Ch01vizKeeling","Ch01vizKeeling/plot.json");</script>
'<div id="%s"></div>\n<script>var %s = new animint("#%s","%s/plot.json");</script>', viz_id, viz_id, viz_id, viz_id
)
# if this is the first plot, place scripts just before the plot
# there has to be a better way to do this, but this will do for now -- http://stackoverflow.com/questions/14308240/how-to-add-javascript-in-the-head-of-a-html-knitr-document
if (length(knitr::knit_meta(class = "animint", clean = FALSE)) == 0) {
res <- sprintf('
'<div id="%s"></div>\n<script>var %s = new animint("#%s","%s/plot.json");</script>', viz_id, viz_id, viz_id, viz_id)
# if this is the first plot, place scripts just before the plot
# there has to be a better way to do this, but this will do for now -- http://stackoverflow.com/questions/14308240/how-to-add-javascript-in-the-head-of-a-html-knitr-document
if (length(knitr::knit_meta(class = "animint", clean = FALSE)) == 0) {
res <- sprintf('
<script type="text/javascript" src="%s/vendor/d3.v3.js"></script>
<script type="text/javascript" src="%s/animint.js"></script>
<script type="text/javascript" src="%s/vendor/quadprog.js"></script>
Expand All @@ -32,10 +33,22 @@ knit_print.animint <- function(x, options, ...) {
<script type="text/javascript" src="%s/vendor/driver.js.iife.js"></script>
<link rel="stylesheet" href="%s/vendor/driver.css" />
%s', viz_id, viz_id, viz_id, viz_id, viz_id, viz_id, viz_id, viz_id, viz_id, res)
}
res <- paste(res, animint_resources(out.dir, viz_id), sep = "\n")
}
knitr::asis_output(res, meta = list(animint = structure("", class = "animint")))
}

animint_resources <- function(out.dir, viz_id) {
files <- list.files(out.dir, recursive = TRUE, all.files = TRUE, no.. = TRUE)
files <- files[!dir.exists(file.path(out.dir, files))]
if(!length(files))return("")
hrefs <- file.path(viz_id, files)
hrefs <- gsub("\\\\", "/", hrefs)
links <- sprintf('<a href="%s"></a>', hrefs)
paste0('<div style="display:none">', paste(links, collapse = "\n"), '</div>')
}

#' Shiny ui output function
#' @param outputId output variable to read the plot from
#' @seealso http://shiny.rstudio.com/articles/building-outputs.html
Expand Down
37 changes: 37 additions & 0 deletions tests/testthat/test-compiler-knit-print-quarto.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
acontext("knitting quarto website outputs")

test_that("quarto website output includes animint directory", {
skip_if_not_installed("quarto")
skip_if_not(nzchar(quarto::quarto_path()), "Quarto CLI not installed")

temp.dir <- tempfile()
dir.create(temp.dir)
on.exit(unlink(temp.dir, recursive = TRUE), add = TRUE)

writeLines(c(
"project:",
" type: website",
" output-dir: _site"
), file.path(temp.dir, "_quarto.yml"))

writeLines(c(
"---",
"title: Home",
"---",
"",
"```{r myplot}",
"library(animint2)",
"p <- ggplot() + geom_point(aes(x = 1:10, y = 1:10))",
"animint(plot = p)",
"```"
), file.path(temp.dir, "index.qmd"))

old_wd <- getwd()
setwd(temp.dir)
on.exit(setwd(old_wd), add = TRUE)

quarto::quarto_render("index.qmd", quiet = TRUE)

expect_true(file.exists(file.path(temp.dir, "_site", "myplot", "plot.json")))
expect_true(file.exists(file.path(temp.dir, "_site", "myplot", "animint.js")))
})