Ranji Raj
Ranji Raj

Reputation: 818

Inserting images from url to Rmakdown using the knitr::include_graphics

I am trying to reproduce an image from pandoc to Rmarkdown.

The pandoc way :

 ![](https://images.unsplash.com/photo-1563204996-8965f0a4a860?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=934&q=80)

My trial Rmarkdown:

url <- "https://images.unsplash.com/photo-1563204996-8965f0a4a860?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=934&q=80"

knitr::include_graphics(url, dpi = 600)

I am able to knit the file and able to generate the image same as pandoc but also additionally would like to set the output width of the figure to 600px which I am trying by placing in dpi=600.

I am skeptical about that is the image actually resized after performing this or is there an alternative way?

Upvotes: 1

Views: 1842

Answers (1)

Till
Till

Reputation: 6628

For me the dpi argument of knitr::include_graphics() also doesn't change the size of the image for HTML or DOCX output. What does work though issetting the chunk-option out.width. You can use pixel values (see below) or percent (e.g. "25%").

```{r out.width="300px"}
url <- "https://images.unsplash.com/photo-1563204996-8965f0a4a860?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=934&q=80"
knitr::include_graphics(url)

```

See chapter 5.4 of the R-Markdown Cookbook for more details.

Upvotes: 2

Related Questions