6

I have a hugo-academic website (methods101.com) that has been working well for last year.

I just went to edit some pages and I've started getting a new error.

The code that seems to be creating the problems is the knitr::include_graphics() function.

This is an example of the text that produces an error:

{r, echo=FALSE, out.width=600, 
fig.cap="Newspaper article in Word document, next to same article on internet.", 
fig.align='center'}

knitr::include_graphics("/img/soc224_qual_analysis_eg_figure_1.png")

This is the error message:

Rendering content/docs/SOC224_qual_analysis_eg.Rmd
Quitting from lines 80-81 (SOC224_qual_analysis_eg.Rmd) 
Error in knitr::include_graphics("/img/soc224_qual_analysis_eg_figure_1.png") : 
  Cannot find the file(s): "/img/soc224_qual_analysis_eg_figure_1.png"
Calls: local ... withCallingHandlers -> withVisible -> eval -> eval -> <Anonymous>
Execution halted
<simpleError in render_page(f): Failed to render 'content/docs/SOC224_qual_analysis_eg.Rmd'>

I get the same problem on different computers, and after fresh re-installing/downloading the website contents.

The image file is definitely inside the folder:

/static/img/

2 Answers2

3

You may see the help page ?knitr::include_graphics. In your case, you need

knitr::include_graphics("/img/soc224_qual_analysis_eg_figure_1.png", error = FALSE)
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • Thanks for this Yihui! We found we could fix the problem by rolling back to the previous version of knitr, with this code: remove.packages("knitr") packageurl <- "https://cran.r-project.org/src/contrib/Archive/knitr/knitr_1.27.tar.gz" install.packages(packageurl, repos=NULL, type="source") – Nicholas Harrigan Feb 19 '20 at 04:07
  • I did try to set options for knitr to error=FALSE, but that seemed to just stop the compile problems. I still had the problem that knitr couldn't find the image. It seems to me/us that it was an issue with the assumed file path in the new version of knitr? Maybe I'm wrong. – Nicholas Harrigan Feb 19 '20 at 04:09
  • 1
    It was a change in knitr 1.28: https://github.com/yihui/knitr/releases/tag/v1.28 – Yihui Xie Feb 19 '20 at 21:21
  • Does this mean I need to change the code for all include_graphics() functions in all markdown documents? Is there a setting I can change the default for, because this is a large number of files than now need to be changed to be compliant with this new convention. – Nicholas Harrigan Feb 20 '20 at 15:40
  • It looks like I need to change global `knitr.graphics.error` – Nicholas Harrigan Feb 20 '20 at 15:43
  • 1
    Yes, if you install the dev version of **knitr** from Github, you can then set `options(knitr.graphics.error = FALSE)` in an appropriate `.Rprofile`: https://bookdown.org/yihui/blogdown/global-options.html – Yihui Xie Feb 20 '20 at 16:19
  • 5
    I solved a similar problem by changing all my references to: `knitr::include_graphics(file.path(here::here(),"image","myImage.jpg"))` – Richard Sprague Mar 27 '20 at 21:08
1

We managed to solve this problem by rolling back to the earlier version of Knitr.

This was the code we used:

remove.packages("knitr")
packageurl <- "https://cran.r-project.org/src/contrib/Archive/knitr/knitr_1.27.tar.gz"
install.packages(packageurl, repos=NULL, type="source")

We had no problems on 5th Feb, but noticed the new version of knitr came out on 6th Feb, and so thought this could be causing the problems.

We don't get the problems with the old version of knitr.

Not sure what the underlying cause is.

  • 1
    I stopped using blogdown for that reason. Spent few hours going through my code in the repo, going back through tags and branches, and always the error with the images, even though they were working fine the week before. Never could have thought it was a change in `knitr`. Even adding the `error=FALSE` option – f0nzie Jun 20 '20 at 18:33