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
include_graphics() cannot find file when 1) working folder is not where the document is and 2) knit from working folder. #1825
Comments
|
I would like to upvote this issue. |
|
You see a difference between ```r
knitr::include_graphics("subdir/emoji.png")
```
However, that means that pandoc is expecting the file to be in If you trick rmarkdown by writing the intermediates file in working dir too, it will work as expected. work_dir <- getwd()
# html file will be written to Rmd dir
rmarkdown::render("Rmd/main.Rmd",
# chunks will be evaluated with this working dir
knit_root_dir = work_dir,
# md file to be converted by pandoc will be in working dir
intermediates_dir = work_dir)There is no pandoc error in this case. You could think that a full path is better for include_graphics, but a relative path is used here, because if HTML is not self contained, the image file need to be accessible from the html file that will be produce inside the Rmd directory. With your example, try setting a full path knitr::include_graphics(normalizePath("subdir/emoji.png"))It will work with self-contained. But not without as you can't load an image in web page from a full path on disk. You can try. I hope this helps understand how all this works under the hood. In the end, we may improve how As often, I think rmarkdown works better if anything is accessible relatively from the directory were the Rmarkdown file is. If not, you need to take care on where and how it is included. managing path for cache, intermediates, dependencies and all is not easy, and having it all relatively to the Rmd file is a lot easier. Thanks for the report though ! |
|
I appreciate your detailed reply. As you suggested, I will use relative paths to Rmd documents. |
|
I'm having a similar issue with A minimal reproducible example is here:
The resulting PDF file will contain the histogram generated in the first chunk, but it will not find the figure in However, this used to work, at least with knitr 1.23 (I tested with this version now and it really works). But, with the last version (1.28.9) it does not work anymore. I believe that if this change wasn't intentional, then it must be a bug in recent versions. |
I think the change you are looking for is the one describe in NEWS
In last dev version, you can now change the value through a global R option
Try setting reproducible example# Using dev version
# remotes::install_github("yihui/knitr")
dir.create(tmp_dir <- tempfile())
old <- setwd(tmp_dir)
xfun::download_file("https://github.com/yihui/knitr/files/4784206/rnw-example.zip")
unzip("rnw-example.zip")
setwd("rnw-example/")
fs::dir_ls()
# rstudioapi::navigateToFile("main.Rnw")
# rstudioapi::navigateToFile("subdir/file1.Rnw")
knitr::knit("main.Rnw")
# we have the error
# rstudioapi::navigateToFile("main.tex")
xfun::read_utf8("main.tex")[78]
# do not throw an error
options(knitr.graphics.error = FALSE)
knitr::knit("main.Rnw")
# rstudioapi::navigateToFile("main.tex")
xfun::read_utf8("main.tex")[77]
fs::dir_ls()
tinytex::pdflatex("main.tex")
# open pdf (I am on windows)
# shell.exec("main.pdf")
# clean
unlink(tmp_dir, recursive = TRUE)
setwd(old)
However, you're issue is with the child document, used in |
This is confirmed. The child document is evaluated as any other document and by default each chunks is executed in the directory of the input document, i.e include_graphics("img/Rlogo.pdf")You can use include_graphics("img/Rlogo.pdf", error = FALSE)and it will not error. (as before knitr < 1.28) I think everything is already here so that it works correctly, isn't it ? Hope it helps. |
|
Not sure if this helps. |
|
Reading this thread again I am not sure there is any issue with this apart from the one with child document where the new default I'll close this one then. Please open new issues if you have some unexpected behavior with this. Thanks everyone. |
|
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary. |
I found that
include_graphics()cannot find a file when 1) working folder is not where the Rmd document is and 2) knit from working folder.This zip file has a reproducible example:
path_issue.zip
To see the bug (I think it is), please follow the steps:
path_issue.Rproj(to set the working folder to be the root of path_issue folder.)main.Rmdin Rmd subfolder. (Note the Rmd file location differs from the working folder.)main.Rmdin the Source pane. BothreadRDS()andinclude_graphics()can find files insubdirfolder.readRDS()works butinclude_graphics()cannot find file.Thank you.
Locale:
LC_COLLATE=English_United States.1252
LC_CTYPE=English_United States.1252
LC_MONETARY=English_United States.1252
LC_NUMERIC=C
LC_TIME=English_United States.1252
Package version:
assertthat_0.2.1 backports_1.1.5
base64enc_0.1.3 callr_3.4.3
cli_2.0.2 compiler_3.6.3
crayon_1.3.4 curl_4.3
desc_1.2.0 digest_0.6.25
evaluate_0.14 fansi_0.4.1
glue_1.3.2 graphics_3.6.3
grDevices_3.6.3 highr_0.8
htmltools_0.4.0 jsonlite_1.6.1
knitr_1.28.2 magrittr_1.5
markdown_1.1 methods_3.6.3
mime_0.9 pkgbuild_1.0.6
prettyunits_1.1.1 processx_3.4.2
ps_1.3.2 R6_2.4.1
Rcpp_1.0.4 remotes_2.1.1
rlang_0.4.5 rmarkdown_2.1
rprojroot_1.3-2 rstudioapi_0.11
stats_3.6.3 stringi_1.4.6
stringr_1.4.0 tinytex_0.20
tools_3.6.3 utils_3.6.3
withr_2.1.2 xfun_0.12
yaml_2.2.1
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.
The text was updated successfully, but these errors were encountered: