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

Sorry, I found that there is a similar post before: Add pdf file in Rmarkdown file

Basically, I can use something like below works well for the html output:
<img src="myFirstAlignment2.pdf" alt="some text" width="4200" height="4200">

And something like below works well for the pdf output: (1)possible solution \begin{center} <br> \includegraphics[width=8in]{myFirstAlignment2.pdf} <br> \end{center} (2)possible solution ![Alt](myFirstAlignment2.pdf)

The myFirstAlignment2.pdf should be replaced with path\myFirstAlignment2.pdf if the pdf file is not in your working directory.


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.

Tags:

R