Skip to content

default relative path processing in include_graphics() can create broken path #2171

@naikymen

Description

@naikymen

Hi!

I'm having difficulties including images during knitting.

Here is a reproducible example: test.zip

The notebook is in R/Rmd1/notebook1.Rmd. If I run the notebook in RStudio everything works.

Knit the document to get the following error:

image

Relevant bits about the example:

  • I usually keep notebooks in subdirectories of the project, tipically under R/.
  • I like to set knitr::opts_knit$set(root.dir = here::here()) at the setup chunk, so paths in chunks are always relative to the project while I'm working.
  • I like to save knitted documents to output/renders (again relative to the project's root) by setting knit in the YAML header to the following function: rmarkdown::render(inputFile, encoding = encoding, output_dir = "output/renders").

If i omit the include_graphics call, Knit works and R is able to find the file with file.exists: notebook1.pdf

If instead of normalizing the path I pass a path relative to the project's root, using knitr::include_graphics("results/image_1.png"). I get a different error:

! Unable to load picture or PDF file 'results/image_1.png'.
<to be read again> 
                   }
l.174 \includegraphics{results/image_1.png}

I would appreciate if someone could point me in the right direction. What path should I pass to include_graphics in this case?

I'm submitting this as a bug report because I have been unable to find out why this happens, everything else seems to work, and it is unexpected behavior from my perspective.

Thank you for all the work you've done :)

Best!

N.


By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.org/issue/.
  • I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('knitr'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('yihui/knitr').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

Session info:

> xfun::session_info("knitr")
R version 4.2.1 (2022-06-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Arch Linux, RStudio 2022.2.0.443

Locale:
  LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=es_AR.UTF-8       
  LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
  LC_PAPER=en_GB.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
  LC_TELEPHONE=C             LC_MEASUREMENT=es_AR.UTF-8 LC_IDENTIFICATION=C       

Package version:
  evaluate_0.16   glue_1.6.2      graphics_4.2.1  grDevices_4.2.1 highr_0.9       knitr_1.40     
  magrittr_2.0.3  methods_4.2.1   stats_4.2.1     stringi_1.7.8   stringr_1.4.1   tools_4.2.1    
  utils_4.2.1     xfun_0.33       yaml_2.3.5 

Metadata

Metadata

Assignees

Labels

bugBugsnextIssues/PRs considered for the next release

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions