Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R markdown: can I insert a pdf to the r markdown file as an image?

Tags:

r

I am trying to insert a pdf image into an r markdown file. I know it is possible to insert jpg or png images. I was just wondering if it is also possible to insert a pdf image. Thanks very much!

like image 751
l0110 Avatar asked Aug 26 '16 19:08

l0110


People also ask

Can you put a PDF in markdown?

Markdown itself doesn't have a mechanism for embedding a PDF. However, Markdown accepts raw HTML in its input and passes it through unaltered.

Can you put images in R markdown?

We include external images in our R markdown documents using the include_graphics function from the knitr package. Images can also be included using either raw HTML with img tags ( <img src = "" /> ) or using markdown directly ( ![ image](imagepath) ).

How do I read a PDF in R?

You can name the function whatever you like e.g Rpdf. The readPDF function has a control argument which we use to pass options to our PDF extraction engine. This has to be in the form of a list, so we wrap our options in the list function. There are two control parameters for the xpdf engine: info and text.

How do I create a PDF in markdown?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.


1 Answers

If you are just trying to insert an image that has been exported from, for example, some R analysis into a pdf image, you can also use the standard image options from the knitr engine.

With something like:

```{r, out.width="0.3\\linewidth", include=TRUE, fig.align="center", fig.cap=c("your caption"), echo=FALSE} knitr::include_graphics("./images/imagename.pdf") ``` 

Unfortunately you can't specify the initial dimensions of your image output (fig.width and fig.height), which you would need to pre-define in your initial output, but you can specify the ultimate size of the image in your document (out.width). As noted below, however, this is limited to scaling down.

You could also of course leave out the initial directory specification if your files are in the same working directory. Just be aware of operating system differences in specifying the path to the image.

An alternative method is to use Markdown syntax noted by @hermestrismegistus on this post:

![Image Title](./path/to/image.pdf){width=65%} 

This can also be collected for multiple images side-by side:

![Image Title](./path/to/image.pdf){width=33%}![Image2 Title](./path/to/image2.pdf){width=33%}![Image3 Title](./path/to/image3.pdf){width=33%} 

Edit:

After working more extensively with in-text referencing, I have found that using r chunks and the include_graphics option to be most useful. Also because of the flexibility in terms of image alignment (justification).

As an example:

```{r image-ref-for-in-text, echo = FALSE, message=FALSE, fig.align='center', fig.cap='Some cool caption', out.width='0.75\\linewidth', fig.pos='H'} knitr::include_graphics("./folder/folder/plot_file_name.pdf") ``` 

The reference can later be used in-text, for example, Figure \@ref(fig:image-ref-for-in-text) illustrates blah blah.

Some important things to note using this format:

  1. You can only expand PDF images via a code chunk up to the out.width and out.height conditions set in the original .pdf file. So I would recommend setting them slightly on the larger side in your original image (just note that any chart text will scale accordingly).

  2. The in-text reference code (in this case image-ref-for-in-text) CANNOT contain any underscores (_) but can contain dashes (-). You will know if you get this wrong by an error message stating ! Package caption Error: \caption outside float.

  3. To stop your plots drifting to the wrong sections of your document, but in a way that unfortunately will generate some white space, the above example includes fig.pos='H'. Where H refers to "hold" position. The same can be achieved for the former Markdown option by placing a full-stop (period .) immediately after the last curly bracket.

Example:

![Image Title](./path/to/image.pdf){width=75%}. 

Unfortunately, this latter option results in some unsightly full-stops. Another reason I prefer the include_graphics option.

like image 181
Rob Smith Avatar answered Sep 26 '22 17:09

Rob Smith