Include program images in a .md document from fragment R using knitr

I want to programmatically include many images in my .Rmd document. Sort of

```{r echo=FALSE}
cat("![](myfile_1.png)")
```

will not work as the final conclusion .md

```
## ![](myfile_1.png)
```

I need to get rid of the code ```and host tags ##. Is it possible to directly enter the markdown code from the R-block?

BTY: The same problem applies to HTML. The HTML injection from the R snippet will also be very useful here.

+5
source share
3 answers

Use results ='asis'means you don’t have to bother with hooks, comments, etc., since the results are not considered a code, but a markdown (or regardless of the output format)

```{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)

, , , .

+10

, knitr, comment:

```{r echo=FALSE, comment=""}
cat("![](myfile_1.png)")
```

:

```{r echo=FALSE, comment=""}
knit_hooks$set(output = function(x,
        options) x)
cat("![](myfile_1.png)")
```

, , reset , render_markdown().

```{r b, echo=FALSE, comment=""}
render_markdown()
a <- 1
```
+1

To use in a loop if you need to insert a bunch of images from a data frame:

for(h in 1:nrow(file_names)){
   image_file<-paste('\n![](', file_names[h],')\n',sep="") 
   cat('\n')
   cat(image_file)
   cat('\n')
}
0
source

All Articles