Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paths to images produced by magick gets absolute paths #2334

Closed
LDalby opened this issue Aug 16, 2023 · 2 comments · Fixed by #2502
Closed

Paths to images produced by magick gets absolute paths #2334

LDalby opened this issue Aug 16, 2023 · 2 comments · Fixed by #2502
Labels
articles ✍️ bug an unexpected problem or unintended behavior
Milestone

Comments

@LDalby
Copy link
Contributor

LDalby commented Aug 16, 2023

This seem related to #1027, but to be honest I'm not entirely sure where the issue is.

I have an article in a package with a pkgdown site. When I call pkgdown::build_site() the images are shown correctly in the rendered html, but when inspecting the images I can see that the path are absolute. In contrast the plots produced using base plot (or ggplot2) have the correct, relative paths. When the pkgdown site is shown in its final location the magick images are then missing because the png can't be found.

Not sure how to produce a proper reprex for this as it involves building the site. To create the content below I started a new project in RStudio as a package and called:

  1. usethis::use_pkgdown()
  2. usethis::use_article("imgpathdebug")
  3. pkgdown::build_site()

Below is a minimal rmd (the content of the "imgpathdebug" article mentioned in 2) ) which when rendered via pkgdown::build_site() will produce the two different paths shown in the screen shot below:
image

---
title: "imgpathdebug"
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(magick)
```

```{r}
plot(1:10, 1:10)
```

```{r}
frink <- image_read("https://jeroen.github.io/images/frink.png")
print(frink)
```
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.1 (2023-06-16)
#>  os       macOS Ventura 13.5
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Europe/Copenhagen
#>  date     2023-08-22
#>  pandoc   3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.1   2023-03-23 [1] CRAN (R 4.3.0)
#>  digest        0.6.33  2023-07-07 [1] CRAN (R 4.3.0)
#>  evaluate      0.21    2023-05-05 [1] CRAN (R 4.3.0)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.3.0)
#>  fs            1.6.3   2023-07-20 [1] CRAN (R 4.3.0)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.3.0)
#>  htmltools     0.5.6   2023-08-10 [1] CRAN (R 4.3.0)
#>  knitr         1.43    2023-05-25 [1] CRAN (R 4.3.0)
#>  lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.3.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.3.0)
#>  purrr         1.0.2   2023-08-10 [1] CRAN (R 4.3.0)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.3.0)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.3.0)
#>  R.oo          1.25.0  2022-06-12 [1] CRAN (R 4.3.0)
#>  R.utils       2.12.2  2022-11-11 [1] CRAN (R 4.3.0)
#>  reprex        2.0.2   2022-08-17 [1] CRAN (R 4.3.0)
#>  rlang         1.1.1   2023-04-28 [1] CRAN (R 4.3.0)
#>  rmarkdown     2.24    2023-08-14 [1] CRAN (R 4.3.0)
#>  rstudioapi    0.15.0  2023-07-07 [1] CRAN (R 4.3.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.3.0)
#>  styler        1.10.1  2023-06-05 [1] CRAN (R 4.3.0)
#>  vctrs         0.6.3   2023-06-14 [1] CRAN (R 4.3.0)
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.3.0)
#>  xfun          0.40    2023-08-09 [1] CRAN (R 4.3.0)
#>  yaml          2.3.7   2023-01-23 [1] CRAN (R 4.3.0)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Is there a way to fix this?

@hadley
Copy link
Member

hadley commented Apr 17, 2024

I think this is a problem either with magick:::`knit_print.magick-image`, or possible the way that pkgdown builds rmarkdown files using an intermediates_dir. But it looks like we already have code to convert paths to relative in tweak_rmarkdown_html() so maybe there's something more I can do there.

Ah the problem is that it's not an absolute path here, but a weird relative path:
../../docs/dev/articles/test/widgets_files/figure-html/unnamed-chunk-2-1.png. Looking at what a regular plot generates, this path should be more like widgets_files/figure-html/unnamed-chunk-2-1.png.

@hadley
Copy link
Member

hadley commented Apr 17, 2024

Interestingly if I set options(knitr.graphics.rel_path = FALSE), the problem goes away. So maybe I should always do that?

hadley added a commit that referenced this issue Apr 25, 2024
For some unknown reason, `knitr_print()` methods sometimes produces paths are relative to the input directory, rather than to the output directory. This PR re-parents those to paths to be relative to the output directory making them work again.

Fixes #2334. Fixes #2341.
hadley added a commit that referenced this issue Apr 30, 2024
SebKrantz pushed a commit to SebKrantz/pkgdown that referenced this issue Jun 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
articles ✍️ bug an unexpected problem or unintended behavior
Projects
None yet
2 participants