rstudio / blogdown

Create Blogs and Websites with R Markdown
https://pkgs.rstudio.com/blogdown/
1.73k stars 335 forks source link

knitr::include_graphics in blogdown #184

Closed xie186 closed 7 years ago

xie186 commented 7 years ago

I'm trying to use knitr::include_graphics. But it seemed that in the html file generated by blogdown::build_site(). The src value is the absolute value on the linux server which is not I want. Is this a bug? Or there is a better way to do this? Thanks in advance. Rmd code:

knitr::include_graphics("../../static/img/test.jpg")

Html output:

<img src="/home/xie186/github_repo/xie186_perlsonal_websrt/static/img/test.jpg" alt="(ref:test)"  />
yihui commented 7 years ago

Could you try knitr::include_graphics("/img/test.jpg") instead? See https://bookdown.org/yihui/blogdown/static-files.html

ulyngs commented 4 years ago

This didn't work for me - following the usual advice, I continued to get an error when running serve_site (also on the demo sites) when blogdown tries to build the html file to pass on to hugo.

Error in knitr::include_graphics("/img/test.jpg") : 
  Cannot find the file(s): "/img/test.jpg"

The solution for me was to add error = FALSE to include_graphics - that way the html gets generated correctly, even if knitr is momentarily confused that the file that is referenced doesn't yet live in the place the path points to.

# this works
knitr::include_graphics("/img/test.jpg", error = FALSE)
cderv commented 4 years ago

For reference, this is a major change in knitr 1.28 https://github.com/yihui/knitr/blob/master/NEWS.md#changes-in-knitr-version-128

And there is now a way in knitr 1.29 to change the new default behavior globally https://github.com/yihui/knitr/blob/master/NEWS.md#changes-in-knitr-version-129 That would be interesting for bookdown.