Knitr cannot find img files in /static/ folder
Asked Answered
T

2

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/

Turne answered 19/2, 2020 at 2:12 Comment(0)
W
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)
Wizard answered 19/2, 2020 at 3:38 Comment(7)
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 <- "cran.r-project.org/src/contrib/Archive/knitr/knitr_1.27.tar.gz" install.packages(packageurl, repos=NULL, type="source")Turne
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.Turne
It was a change in knitr 1.28: github.com/yihui/knitr/releases/tag/v1.28Wizard
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.Turne
It looks like I need to change global knitr.graphics.errorTurne
Yes, if you install the dev version of knitr from Github, you can then set options(knitr.graphics.error = FALSE) in an appropriate .Rprofile: bookdown.org/yihui/blogdown/global-options.htmlWizard
I solved a similar problem by changing all my references to: knitr::include_graphics(file.path(here::here(),"image","myImage.jpg"))Investigation
T
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.

Turne answered 19/2, 2020 at 4:11 Comment(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 optionZagreb

© 2022 - 2025 — McMap. All rights reserved.