How to insert a picture in an R Markdown chunk using a relative file path

Viewed 832

I have two folders (folder1, and folder2) nested inside my project_folder. Folder2 is my working directory and it is where I kept my RMarkdown files and their output. Folder1 is where I keep my picture and data files. I am trying to insert a .jpg file from folder1 while compiling and working in the folder 2 directory. I tried to do this:

```{r Chunk1, echo = FALSE, message = FALSE, warning = FALSE, error = FALSE, comment = NA, fig.align = "center", results = 'asis'}

#Set the wd one step back

knitr::opts_knit$set(root.dir = setwd('..')) 

#Call the picture

knitr::include_graphics('/folder1/external/picture1.jpg')


```

But I keep receiving the error that the program cannot find the file:

Error in knitr::include_graphics("/folder1/external/picture1.jpg") : Cannot find the file(s): "/folder1/external/picture1.jpg" Calls: <Anonymous> . . . withCallingHandlers -> withVisible -> eval -> eval -> <Anonymous> In addition: Warning message: package 'knitr' was built under R version 3.5.2. Execution halted.

Is there another way to do this? What am I doing wrong here?

1 Answers

the problem is you are giving it a wring path. Try addind "../" before the path and make sure that you get to the right place. (After putting "../" if you press TAB you can see in which directory you are and choose the relevant picture file name.

knitr::include_graphics('../folder1/external/picture1.jpg')
Related