I want to programmatically include a lot of images in my .Rmd markdown document. Something like
```{r echo=FALSE}
cat("![](myfile_1.png)")
```
will not work, as the resulting .md
output is
```
## ![](myfile_1.png)
```
I would need to get rid of the code tags ```
and the leading ##
.
Is there an option to directly inject markdown code from within the R chunk?
BTY: The same issue applies to HTML as well. Here also a HTML code injection from within an R chunk would be really helpful.
To add an image in markdown you must stop text editing, and you do this with the command [Alt text] precedeed by a ! Then you have to add the path to the image in brackets. The path to the image is the path from your directory to the image.
You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).
The usual way to compile an R Markdown document is to click the Knit button as shown in Figure 2.1, and the corresponding keyboard shortcut is Ctrl + Shift + K ( Cmd + Shift + K on macOS). Under the hood, RStudio calls the function rmarkdown::render() to render the document in a new R session.
Using results ='asis'
means that you don't have to mess with the hooks, comments etc as the results are not considered code, but markdown (or whatever the output format happens to be)
```{r myfile-1-plot, echo = F, results = 'asis'}
cat('\n![This is myfile_1.png](myfile1.png)\n')
```
Will result in
![This is myfile_1.png](myfile1.png)
Note that I wrapped the output text with new line markers to ensure that it is on a separate line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With