Search code examples
rr-markdownknitrhugoblogdown

Knitr cannot find img files in /static/ folder


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/


Solution

  • 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)