R blogdown fails to find image files

Viewed 769

I have (had) a working website made with blogdown. I tried to rebuild the site today for the first time in a couple months and I am getting errors because it apparently can't find my images.

To make a reprex I made a new project, and chose the "website with blogdown" option and took all the defaults. The website built without issues with blogdown:::serve_site(). I put a image named x.png into the static directory and added:

```{r x, fig.cap='x', tidy=FALSE}
knitr::include_graphics("x.png")
```

into the 2015-07-23-r-rmarkdown.Rmd file. When I tell it to blogdown:::serve_site() I get:

Error in knitr::include_graphics("x.png") : 
  Cannot find the file(s): "x.png"
Calls: local ... withCallingHandlers -> withVisible -> eval -> eval -> <Anonymous>
Execution halted

I then tried to add the image using the "Addins" menu and chose Insert Image and pointed it to the file. It made a copy of the file and wrote the expected markdown into 2015-07-23-r-rmarkdown.Rmd:

![](/post/2015-07-23-r-rmarkdown_files/x.png)

That threw this error:

pandoc: /post/2015-07-23-r-rmarkdown_files/x.png: openBinaryFile: does not exist (No such file or directory)
Error: pandoc document conversion failed with error 1
Execution halted

Any ideas on what the heck is wrong?

> sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.3

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.3           later_1.0.0          compiler_3.6.2       pillar_1.4.3        
 [5] prettyunits_1.1.1    tools_3.6.2          digest_0.6.25        packrat_0.5.0-25    
 [9] pkgbuild_1.0.6       jsonlite_1.6.1       evaluate_0.14        lifecycle_0.2.0     
[13] tibble_2.1.3         gtable_0.3.0         pkgconfig_2.0.3      rlang_0.4.5         
[17] cli_2.0.2            rstudioapi_0.11      parallel_3.6.2       yaml_2.2.1          
[21] blogdown_0.18        xfun_0.12            loo_2.2.0            gridExtra_2.3       
[25] stringr_1.4.0        dplyr_0.8.4          knitr_1.28           tidyselect_1.0.0    
[29] stats4_3.6.2         grid_3.6.2           glue_1.3.1           inline_0.3.15       
[33] R6_2.4.1             processx_3.4.2       fansi_0.4.1          rmarkdown_2.1       
[37] bookdown_0.18        rstan_2.19.3         servr_0.16           purrr_0.3.3         
[41] magrittr_1.5         callr_3.4.2          ggplot2_3.3.0        promises_1.1.0      
[45] matrixStats_0.55.0   scales_1.1.0         ps_1.3.2             StanHeaders_2.21.0-1
[49] htmltools_0.4.0      rsconnect_0.8.16     assertthat_0.2.1     mime_0.9            
[53] colorspace_1.4-1     httpuv_1.5.2         stringi_1.4.6        munsell_0.5.0       
[57] crayon_1.3.4  
> rmarkdown::pandoc_version()
[1] ‘2.7.3’
4 Answers

To your second method with the addon: You should not knit this RMD file. If you just use the addin "serve site" it works as expected. Of course it will not work if you knit it because the file is copied to "static/post/...." and your RMD file is in "content/post/..."

Try put your image inside de "content/post". You can use the Addin "new post" and create a subdirectory to each post "content/post/my_incredible_post", then your files will be all in the same place. Then you can use:

knitr::include_graphics("x.png")

and

![](x.png)

Here's an example of how I organized my blog post:

site/content/blog/veterans/ contains my blog post as index.md

site/content/blog/veterans/gallary contains my pictures used in the blog post. To call in my picture in the blog post, I use ![png](./gallery/uspop.png)

If it was in the same directory as my blog post, I would use ![png](./uspop.png)

You can take a look at my GitHub repository if that helps: https://github.com/datasurfergtx/site/tree/master/content/blog/veterans

Thank you everybody. My original website needed the "error = FALSE" option added. I think because of changes made in knitr 1.28, this code failed:

knitr::include_graphics("/post/2019-12-10-installing-r-rstudio-on-windows_files/marginWin.png")

but this works:

knitr::include_graphics("/post/2019-12-10-installing-r-rstudio-on-windows_files/marginWin.png", error = FALSE)

I introduced a second problem when I was making the reprex. sigh... The path in the include_graphics() call needs to begin with a /

Related