RStudio include_graphics destroys image ?

Hi Community,
I am surprised about a strange behavior of RStudio. I have a plot of the size of 27 kB which I want to embed in .Rmd using the following code:

plot = "test.png"  
par(mar=c(1,1,1,1))
knitr::include_graphics(plot)

The output html is generated without error messages or warnings. However, instead of the plot, a white area is displayed in the output html. More surprisingly, after knitr, the size of the image shrinked to 1 Kb and it shows indeed a white area only. To summarize, instead of embedding the image, RStudio destroyed the image by converting the content to white space.
Any suggestions?

Hi @thule,

There seems to be a bit of confusion here. This occurrence is independent of RStudio and is something to do with knitr/rmarkdown. My first question, is the test.png file located in the same directory that the R Markdown document is located in? This is a common error, whereby things will run in a notebook interactively in the current working directory, but when the notebook is knit, it can't find certain files since they do not exist in the knit directory.

Hi @mattwarkentin,

thanks for responding to my question. Meanwhile, I examined this a little bit closer. I have to admit that I "simplified" my question a little bit by renaming the image file. The real name was "matrix.validation.plot.png".
If I use this name, the above described error persists, i.e. the output html shows nothing but a blank area and the image file is destroyed (reduced in size from 27 Kb to 1 Kb). Now, if I really rename the file to "test.png" everything works fine:

knitr::include_graphics("test.png")  # works
knitr::include_graphics("matrix.validation.plot.png")  # same file but does not work!

I wonder how such things can happen and if this is the failure of knitr or RStudio. (At the moment, I still do not see that it is my failure :slight_smile: , especially because no error message pops up when I try with the long name).

hi Thule, thanks for describing the file names issue in more detail. Have you considered what mattwarkentin suggested about the directory/path issues ?

Hi @nirgrahamuk, thanks for your comment. Both files (short named and long named) reside in the same folder as the .Rmd file. Is that what you meant? Moreover, if the file really cannot be found by knitr, I would at least expect an error message.

Yes, the multiple dots in the name is probably the issue and best to avoid naming files like that. Dots have specific roles in file names, usually indicating where the extension name begins. Try using underscores or dashes when naming instead.

For example:

# Acceptable
knitr::include_graphics("matrixvalidationplot.png") 
knitr::include_graphics("matrix_validation_plot.png") 
knitr::include_graphics("matrix-validation-plot.png") 

# Avoid this
knitr::include_graphics("matrix.validation.plot.png")
knitr::include_graphics("matrix validation plot.png") 

Thanks a lot @mattwarkentin. So, it is the knitr, right. It's a pity that this is neither documented (to my knowledge) nor throwing warnings or error messages. But nothing we can change, obviously ...

Are you trying to render your R Markdown to PDF format?

No, I'm creating HTML. Just wondering if this could have something to do with my issue ... ?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.