From df6e6ea3e5a57fa0ae24e424694bfb8383401149 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Wed, 1 May 2024 08:45:17 +1200 Subject: [PATCH] Fix issue with knitr relative paths (#2502) Works around https://github.com/yihui/knitr/issues/2171 Fixes #2334. Fixes #2341. --- DESCRIPTION | 5 ++++- R/rmarkdown.R | 5 ++++- tests/testthat/_snaps/build-articles.md | 17 ----------------- .../.gitignore | 0 .../DESCRIPTION | 0 .../README.md | 0 .../_pkgdown.yml | 0 .../man-figures.Rproj | 0 .../man/figures/kitten.jpg | Bin .../man/kitten.Rd | 0 .../vignettes/.gitignore | 0 .../vignettes/another-kitten.jpg | Bin .../vignettes/kitten.Rmd | 18 ++++++++++++++++-- tests/testthat/test-build-articles.R | 17 ++++++++++++----- tests/testthat/test-package.R | 2 +- tests/testthat/test-preview.R | 2 +- 16 files changed, 38 insertions(+), 28 deletions(-) rename tests/testthat/assets/{man-figures => articles-images}/.gitignore (100%) rename tests/testthat/assets/{man-figures => articles-images}/DESCRIPTION (100%) rename tests/testthat/assets/{man-figures => articles-images}/README.md (100%) rename tests/testthat/assets/{man-figures => articles-images}/_pkgdown.yml (100%) rename tests/testthat/assets/{man-figures => articles-images}/man-figures.Rproj (100%) rename tests/testthat/assets/{man-figures => articles-images}/man/figures/kitten.jpg (100%) rename tests/testthat/assets/{man-figures => articles-images}/man/kitten.Rd (100%) rename tests/testthat/assets/{man-figures => articles-images}/vignettes/.gitignore (100%) rename tests/testthat/assets/{man-figures => articles-images}/vignettes/another-kitten.jpg (100%) rename tests/testthat/assets/{man-figures => articles-images}/vignettes/kitten.Rmd (70%) diff --git a/DESCRIPTION b/DESCRIPTION index 379a92463..3bbef16bd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,7 +34,7 @@ Imports: purrr (>= 1.0.0), ragg, rlang (>= 1.1.0), - rmarkdown (>= 1.1.9007), + rmarkdown (>= 2.26.2), tibble, whisker, withr (>= 2.4.3), @@ -50,6 +50,7 @@ Suggests: htmlwidgets, knitr, lifecycle, + magick, methods, openssl, pkgload (>= 1.0.2), @@ -71,3 +72,5 @@ Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.1 SystemRequirements: pandoc +Remotes: + rstudio/rmarkdown diff --git a/R/rmarkdown.R b/R/rmarkdown.R index d8641d986..ef72379ad 100644 --- a/R/rmarkdown.R +++ b/R/rmarkdown.R @@ -108,7 +108,10 @@ rmarkdown_render_with_seed <- function(..., seed = NULL) { # envir$.Random.seed <- .GlobalEnv$.Random.seed # } } - + # Ensure paths from output are not made relative to input + # https://github.com/yihui/knitr/issues/2171 + options(knitr.graphics.rel_path = FALSE) + rmarkdown::render(envir = globalenv(), ...) } diff --git a/tests/testthat/_snaps/build-articles.md b/tests/testthat/_snaps/build-articles.md index 63d48d8f8..473bcbdb9 100644 --- a/tests/testthat/_snaps/build-articles.md +++ b/tests/testthat/_snaps/build-articles.md @@ -1,20 +1,3 @@ -# links to man/figures are automatically relocated - - Code - copy_figures(pkg) - Message - Copying man/figures/kitten.jpg to reference/figures/kitten.jpg - ---- - - Code - build_articles(pkg, lazy = FALSE) - Message - -- Building articles ----------------------------------------------------------- - Writing `articles/index.html` - Reading vignettes/kitten.Rmd - Writing `articles/kitten.html` - # warns about missing images Code diff --git a/tests/testthat/assets/man-figures/.gitignore b/tests/testthat/assets/articles-images/.gitignore similarity index 100% rename from tests/testthat/assets/man-figures/.gitignore rename to tests/testthat/assets/articles-images/.gitignore diff --git a/tests/testthat/assets/man-figures/DESCRIPTION b/tests/testthat/assets/articles-images/DESCRIPTION similarity index 100% rename from tests/testthat/assets/man-figures/DESCRIPTION rename to tests/testthat/assets/articles-images/DESCRIPTION diff --git a/tests/testthat/assets/man-figures/README.md b/tests/testthat/assets/articles-images/README.md similarity index 100% rename from tests/testthat/assets/man-figures/README.md rename to tests/testthat/assets/articles-images/README.md diff --git a/tests/testthat/assets/man-figures/_pkgdown.yml b/tests/testthat/assets/articles-images/_pkgdown.yml similarity index 100% rename from tests/testthat/assets/man-figures/_pkgdown.yml rename to tests/testthat/assets/articles-images/_pkgdown.yml diff --git a/tests/testthat/assets/man-figures/man-figures.Rproj b/tests/testthat/assets/articles-images/man-figures.Rproj similarity index 100% rename from tests/testthat/assets/man-figures/man-figures.Rproj rename to tests/testthat/assets/articles-images/man-figures.Rproj diff --git a/tests/testthat/assets/man-figures/man/figures/kitten.jpg b/tests/testthat/assets/articles-images/man/figures/kitten.jpg similarity index 100% rename from tests/testthat/assets/man-figures/man/figures/kitten.jpg rename to tests/testthat/assets/articles-images/man/figures/kitten.jpg diff --git a/tests/testthat/assets/man-figures/man/kitten.Rd b/tests/testthat/assets/articles-images/man/kitten.Rd similarity index 100% rename from tests/testthat/assets/man-figures/man/kitten.Rd rename to tests/testthat/assets/articles-images/man/kitten.Rd diff --git a/tests/testthat/assets/man-figures/vignettes/.gitignore b/tests/testthat/assets/articles-images/vignettes/.gitignore similarity index 100% rename from tests/testthat/assets/man-figures/vignettes/.gitignore rename to tests/testthat/assets/articles-images/vignettes/.gitignore diff --git a/tests/testthat/assets/man-figures/vignettes/another-kitten.jpg b/tests/testthat/assets/articles-images/vignettes/another-kitten.jpg similarity index 100% rename from tests/testthat/assets/man-figures/vignettes/another-kitten.jpg rename to tests/testthat/assets/articles-images/vignettes/another-kitten.jpg diff --git a/tests/testthat/assets/man-figures/vignettes/kitten.Rmd b/tests/testthat/assets/articles-images/vignettes/kitten.Rmd similarity index 70% rename from tests/testthat/assets/man-figures/vignettes/kitten.Rmd rename to tests/testthat/assets/articles-images/vignettes/kitten.Rmd index 0481c299b..332ecf94b 100644 --- a/tests/testthat/assets/man-figures/vignettes/kitten.Rmd +++ b/tests/testthat/assets/articles-images/vignettes/kitten.Rmd @@ -20,10 +20,24 @@ knitr::opts_chunk$set( knitr::include_graphics("../man/figures/kitten.jpg") ``` +``` {r} +knitr::include_graphics("another-kitten.jpg") +``` + ## rmarkdown ![](../man/figures/kitten.jpg) -## Another kitten - ![](another-kitten.jpg) + +## External package + +```{r magick} +magick::image_read("another-kitten.jpg") +``` + +## Plot + +```{r plot} +plot(1:3) +``` \ No newline at end of file diff --git a/tests/testthat/test-build-articles.R b/tests/testthat/test-build-articles.R index 99f9c57a1..a09134e25 100644 --- a/tests/testthat/test-build-articles.R +++ b/tests/testthat/test-build-articles.R @@ -5,21 +5,28 @@ test_that("can recognise intro variants", { expect_true(article_is_intro("articles/pack-age", "pack.age")) }) -test_that("links to man/figures are automatically relocated", { +test_that("image links relative to output", { # weird path differences that I don't have the energy to dig into skip_on_cran() - pkg <- local_pkgdown_site(test_path("assets/man-figures")) + pkg <- local_pkgdown_site(test_path("assets/articles-images")) - expect_snapshot(copy_figures(pkg)) - expect_snapshot(build_articles(pkg, lazy = FALSE)) + suppressMessages(copy_figures(pkg)) + suppressMessages(build_article("kitten", pkg)) html <- xml2::read_html(path(pkg$dst_path, "articles", "kitten.html")) src <- xpath_attr(html, "//img", "src") expect_equal(src, c( + # knitr::include_graphics() "../reference/figures/kitten.jpg", + "another-kitten.jpg", + # rmarkdown image "../reference/figures/kitten.jpg", - "another-kitten.jpg" + "another-kitten.jpg", + # magick::image_read() + "kitten_files/figure-html/magick-1.png", + # figure + "kitten_files/figure-html/plot-1.jpg" )) # And files aren't copied diff --git a/tests/testthat/test-package.R b/tests/testthat/test-package.R index d2f4528a4..ce3dd158a 100644 --- a/tests/testthat/test-package.R +++ b/tests/testthat/test-package.R @@ -55,7 +55,7 @@ test_that("package_vignettes() sorts articles alphabetically by file name", { }) test_that("override works correctly for as_pkgdown", { - pkgdown <- as_pkgdown("assets/man-figures") + pkgdown <- as_pkgdown(test_path("assets/articles-images")) expected_list <- list(figures = list(dev = "jpeg", fig.ext = "jpg", fig.width = 3, fig.asp = 1)) expect_equal(pkgdown$meta, expected_list) modified_pkgdown <- as_pkgdown(pkgdown, override = list(figures = list(dev = "png"))) diff --git a/tests/testthat/test-preview.R b/tests/testthat/test-preview.R index 35ee01f93..74e261285 100644 --- a/tests/testthat/test-preview.R +++ b/tests/testthat/test-preview.R @@ -1,5 +1,5 @@ test_that("checks its inputs", { - pkg <- local_pkgdown_site(test_path("assets/man-figures")) + pkg <- local_pkgdown_site(test_path("assets/articles-images")) expect_snapshot(error = TRUE, { preview_site(pkg, path = 1)