Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add and resize a local image to a .Rmd file in RStudio that will produce a pdf

Tags:

r

r-markdown

I am trying to add and resize a local image to a .Rmd file in RStudio that will produce a pdf. I can add the file easily with

![My caption.](path/file.png)

but I have not figured out how to control the size of the image. I tried HTML code with a width attribute, but the image would not appear (I think this only works if outputting to HTML).

<img src="path/file.png" width="200px" />

I could not get this idea to work:

![My caption.](path/file.png =250x)

Is there a way to modify the Rmarkdown script to modify the size of the local image with only RMarkdown and base R?

There is a suggestion to use the png and grid packages, but I am limited to base R for my problem. For other users, however, I think this looks like a good solution.

like image 657
Eric Green Avatar asked Jan 19 '15 20:01

Eric Green


People also ask

How do you insert a picture in RStudio?

The best way is to group all images in a folder, to be put in a directory, the directory to which RStudio has access. 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.

How do I add graphics to R markdown?

Including images in an R markdown File. We can include an image using {knitr} and the include_graphics() function, e.g. Typically the chunk would use echo = FALSE as we don't want to see the actual R code.


1 Answers

You can also specify the size of the image like so:

![](filepath\file.jpg){ width=50% }

The width and height attributes on images are treated specially. When used without a unit, the unit is assumed to be pixels. However, any of the following unit identifiers can be used: px, cm, mm, in, inch and %. There must not be any spaces between the number and the unit.

Source: Pandoc's RMarkdown Documentation - Images

like image 144
Johnny Avatar answered Oct 19 '22 04:10

Johnny