-
-
Notifications
You must be signed in to change notification settings - Fork 879
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
default relative path processing in include_graphics()
can create broken path
#2171
Comments
When passing I was able to get the normalized path to work by adding the images to the (second) parent directory of the project: I don't understand how to tell |
Well, I've found a workaround. Here is an updated example: test3.zip
It seems to me that the error is raised mistakenly by Does this make sense?
|
Possible culpirt: Line 492 in 6bfffe9
|
I have also been struggling with and trying to figure out this issue. I believe that part of the issue is that if you pass an absolute path to knitr::include_graphics that it will try to convert it to a relative path, but it converts it to a path relative to the Rmd file, not relative to the output directory as is should. This in turn is because Reprex here: root_dir <- tempdir()
knitr_output_dir <- "to receive path from knit"
fs::dir_create(root_dir, "rmd-dir")
cat(
"---\ntitle: test\noutput: html_document\n---\n\n```{r}\n
# only important part of Rmd file:
knitr_output_dir <<- knitr::opts_knit$get('output.dir')\n```",
file = fs::path(root_dir, "rmd-dir/test.Rmd"))
fs::dir_create(root_dir, "output")
rmarkdown::render(
fs::path(root_dir, "rmd-dir/test.Rmd"),
output_dir = fs::path(root_dir, "output")
)
#> processing file: test.Rmd
#> output file: test.knit.md
#> "C:/Program Files/RStudio/bin/quarto/bin/tools/pandoc" +RTS -K512m -RTS test.knit.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc4d2879d05b93.html --lua-filter "C:\Users\jalsal02\R\win-library\4.1\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\jalsal02\R\win-library\4.1\rmarkdown\rmarkdown\lua\latex-div.lua" --self-contained --variable bs3=TRUE --section-divs --template "C:\Users\jalsal02\R\win-library\4.1\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable theme=bootstrap --mathjax --variable "mathjax-url=https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" --include-in-header "C:\Users\jalsal02\AppData\Local\Temp\RtmpklzUsb\rmarkdown-str4d283a383e3d.html"
#>
#> Output created: C:/Users/jalsal02/AppData/Local/Temp/RtmpklzUsb/output/test.html
print(knitr_output_dir)
#> [1] "C:/Users/jalsal02/AppData/Local/Temp/RtmpklzUsb/rmd-dir" Created on 2022-09-21 by the reprex package (v2.0.1) Session infosessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#> setting value
#> version R version 4.1.3 (2022-03-10)
#> os Windows 10 x64 (build 19042)
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate English_United States.1252
#> ctype English_United States.1252
#> tz America/New_York
#> date 2022-09-21
#> pandoc 2.18 @ C:/Program Files/RStudio/bin/quarto/bin/tools/ (via rmarkdown)
#>
#> - Packages -------------------------------------------------------------------
#> package * version date (UTC) lib source
#> bslib 0.3.1 2021-10-06 [1] CRAN (R 4.1.1)
#> cli 3.3.0 2022-04-25 [1] CRAN (R 4.1.3)
#> digest 0.6.29 2021-12-01 [1] CRAN (R 4.1.3)
#> evaluate 0.15 2022-02-18 [1] CRAN (R 4.1.3)
#> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.1)
#> fs 1.5.2 2021-12-08 [1] CRAN (R 4.1.3)
#> glue 1.6.2 2022-02-24 [1] CRAN (R 4.1.3)
#> highr 0.9 2021-04-16 [1] CRAN (R 4.1.1)
#> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.1)
#> jquerylib 0.1.4 2021-04-26 [1] CRAN (R 4.1.1)
#> jsonlite 1.8.0 2022-02-22 [1] CRAN (R 4.1.3)
#> knitr 1.40 2022-08-24 [1] CRAN (R 4.1.3)
#> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.1.3)
#> R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.1)
#> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.1.1)
#> rlang 1.0.5 2022-08-31 [1] CRAN (R 4.1.3)
#> rmarkdown 2.16 2022-08-24 [1] CRAN (R 4.1.3)
#> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.1)
#> sass 0.4.1 2022-03-23 [1] CRAN (R 4.1.3)
#> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.1.3)
#> stringi 1.7.6 2021-11-29 [1] CRAN (R 4.1.2)
#> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.1.1)
#> withr 2.5.0 2022-03-03 [1] CRAN (R 4.1.3)
#> xfun 0.33 2022-09-12 [1] CRAN (R 4.1.3)
#> yaml 2.3.5 2022-02-21 [1] CRAN (R 4.1.2)
#>
#> [1] C:/Users/jalsal02/R/win-library/4.1
#> [2] C:/Program Files/R/R-4.1.3/library
#>
#> ------------------------------------------------------------------------------ |
@naikymen Another approach you should be aware of is to pass absolute path to include_graphics (e.g., what your first example did with here::here()), and then |
it's unlikely for an img src to contain line breaks, so it shouldn't be necessary to one_string(html) beforehand and split it afterwards
The issue is here is mixing quite a few path handling and so create some unfound file at the end, here is the structure of project > fs::dir_tree()
.
├── output
│ └── renders
├── R
│ └── Rmd1
│ └── notebook1.Rmd
├── results
│ └── image_1.png
└── test.Rproj Here is the Rmd used in ---
title: "Test"
output:
pdf_document:
latex_engine: xelatex
toc: true
toc_depth: 4
number_sections: true
editor_options:
chunk_output_type: inline
author: NM
date: "`r format(Sys.time(), '%d %B, %Y')`"
urlcolor: blue
knit: (function(inputFile, encoding) {
rmarkdown::render(inputFile, encoding = encoding, output_dir = "output/renders") })
---
```{r setup, message=F}
library(here)
library(magrittr)
knitr::opts_chunk$set(message = F)
knitr::opts_knit$set(root.dir = here::here())
```
## Test
### File path
```{r}
file.path <- paste0(
here::here(),
"/results/image_1.png"
)
```
### Test path
```{r}
file.exists(file.path)
```
### Test include
```{r}
file.path %>%
normalizePath() %>%
knitr::include_graphics()
```
So you can resolve your problem quite easily by configuring the right options, of just using relative path directly with Though it is a symptom of a general issue with |
General issue here is related to current absolute to relative processing in Lines 473 to 479 in 44a7bee
Currently, Other examples of this issue Using
|
include_graphics()
can create broken path
…ve path processing. This processing is by default, and paths are made relative to input directory and not output. It should be fixed in later knitr version yihui/knitr#2171
…ve path processing. This processing is by default, and paths are made relative to input directory and not output. It should be fixed in later knitr version yihui/knitr#2171
Works around yihui/knitr#2171 Fixes #2334. Fixes #2341.
Works around yihui/knitr#2171 Fixes r-lib#2334. Fixes r-lib#2341.
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:
Relevant bits about the example:
R/
.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.output/renders
(again relative to the project's root) by settingknit
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 withfile.exists
: notebook1.pdfIf 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: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
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')
.I understand that my issue may be closed if I don't fulfill my promises.
Session info:
The text was updated successfully, but these errors were encountered: