15

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?

2 Answers 2

17

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`)
Sign up to request clarification or add additional context in comments.

4 Comments

Very clever solution. Out of interest, why do some tags work normally, like <center>, but others (like <img ..> do not?
Is there a way to resize the images in the first method?
We can use html properties or css styling to resize the images...
Brilliant idea.
0

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>')) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.