jjankowiak
jjankowiak

Reputation: 3269

how to insert image from url in markdown

The way to insert an image to markdown file is (and it is outside "{r}")

<center><src="http://ia.media-imdb.com/images/M/MV5BMTg4NzEyNzQ5OF5BMl5BanBnXkFtZTYwNTY3NDg4._V1._CR24,0,293,443_SX214_AL_.jpg"></center>

But I want to make url as a variable. So I tried something like this:

```{r, result='asis'}
url <- "http://www.online-image-editor.com//styles/2014/images/example_image.png"
print(paste("<center><src=\"", url, "\"></center>", collapse=""))
```

But it doesn't work. The result I get

## [1] "<center><src=\" http://ia.media-imdb.com/images/M/MV5BMTg4NzEyNzQ5OF5BMl5BanBnXkFtZTYwNTY3NDg4._V1._CR24,0,293,443_SX214_AL_.jpg \"></center>"

Is there any chance to make the string of characters like on the top of my question so get rid of ## [1] and quotes?

Upvotes: 15

Views: 23387

Answers (2)

cmndrsn
cmndrsn

Reputation: 11

Generalizing Richard's answer to be able to print the image within the code chunk. You can add this in a code chunk with the following parameters (surrounded with triple back ticks on lines before and after R code chunk):

{r echo = FALSE, results = 'asis'}
image = "https://cdn.britannica.com/84/206384-050-00698723/Javan-gliding-tree-frog.jpg"
cat(paste0('<center><img src="', image,  '"></center>')) 

Upvotes: 0

Richard Ambler
Richard Ambler

Reputation: 5030

We can do it using inline R code. For example:

```{r, echo=FALSE}
# Define variable containing url
url <- "http://www.online-image-editor.com//styles/2014/images/example_image.png"
```
## Some cat!
<center><img src="`r url`"></center>

## Alternatively...
![](`r url`)

Upvotes: 17

Related Questions