diff --git a/R/z_knitr.R b/R/z_knitr.R index 35f99000..f7d9ddcd 100644 --- a/R/z_knitr.R +++ b/R/z_knitr.R @@ -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( ##
- '
\n', 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(' + '
\n', 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(' @@ -32,10 +33,22 @@ knit_print.animint <- function(x, options, ...) { %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('', hrefs) + paste0('
', paste(links, collapse = "\n"), '
') +} + #' Shiny ui output function #' @param outputId output variable to read the plot from #' @seealso http://shiny.rstudio.com/articles/building-outputs.html diff --git a/tests/testthat/test-compiler-knit-print-quarto.R b/tests/testthat/test-compiler-knit-print-quarto.R new file mode 100644 index 00000000..a5229068 --- /dev/null +++ b/tests/testthat/test-compiler-knit-print-quarto.R @@ -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"))) +})