knitr::include_graphics runs, but image cannot be displayed - r

I wanted to try using knitr::include graphics (link) in my R-markdown document. I am using it as suggested by this link (using the full path):
---
title: "Sample Document"
output:
word_document
---
```{r pressure, echo=FALSE, fig.cap="A caption", out.width = '100%'}
knitr::include_graphics("Z:/../SEM/semflow.png")
```
This is the picture. I tried installing the png package and also tried simply using:
```{r}
knitr::include_graphics("Z:/../SEM/semflow.png")
```
The code is accepted by knitr, which means that it found the file, but it shows up in the Word document like this:
Does anyone have an idea how this could happen?
Related question
A related question (without an answer) can be found here.
Help file: ?knitr::include_graphics

Related

How to insert an image from a url into R markdown html vignette?

Similar questions have been asked before about inserting an image from a url (for example here and here ). However, I can't seem to get those solutions to work for me.
I am trying to create an R package vignette and a github.io vignette page. Initially I was saving my images to my machine and when building the vignette I was calling them locally via:
knitr::include_graphics("file/path/image.png")
However, when I use devtools::build_site() to build the github.io site, the images are understandably missing from the vignette. I have uploaded all the images to my GitHub repo and I was wondering if there is any way to display to these images when building the GitHub.io site?
I have tried the following but neither have worked. I just get a blank space where the image should be.
<center><src="https://github.com/tidyverse/ggplot2/blob/main/icons/coord_cartesian.png"></center>
```{r out.width="100%"}
url <- "https://github.com/tidyverse/ggplot2/blob/main/icons/coord_cartesian.png"
knitr::include_graphics(url)
```
My YAML looks like this:
---
title: "myPackage"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{vivid}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: console
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "vig/"
)
options(rmarkdown.html_vignette.check_title = FALSE)
```
I figured out the solution to my problem from this. I needed to use the "raw" absolute link from the image. So in the example above, I should have used:
```{r out.width="100%"}
url <- "https://raw.githubusercontent.com/tidyverse/ggplot2/main/icons/coord_cartesian.png"
knitr::include_graphics(url)
```

Change font and color for figure captions in Rmarkdown in PDF output

I'm wondering how to change font and color specifically for figure captions in Rmarkdown for a PDF output. I know how to do this for word output (creating a separate document), but often figures are submitted to journals as PDFs.
Note that this is not whether you can change the color of text in a PDF which is answered here: Changing the font size of figure captions in RMarkdown pdf output. If I try this within the fig.cap = "fig text here.\\label{...}" I get the following error:
! Undefined control sequence.
<recently read> \cellcolor
This probably comes closest: Changing the font size of figure captions in RMarkdown pdf output, but I went to CTAN and downloaded the package and am not sure whether I have to find my R libraries and drag it to there, or what. Perhaps this is the only/easiest solution, if so, can you advise on how I should proceed with the downloaded package. Haven't done a lot in latex, so it would probably be finding how colors are defined in latex as well?
Thanks much!
James
Here is my header:
output:
pdf_document:
keep_tex: true
html_document: default
header-includes:
\usepackage{placeins}
---
You could try this:
---
title: "Untitled"
author: "bttomio"
date: "6/13/2021"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r pressure, echo=FALSE, fig.cap="\\textcolor{red}{This is red} and \\textcolor{blue}{this is blue}"}
plot(pressure)
```
-output

Company logo in PDF output only on the first page

I was wondering if there is a way to include the company logo to a PDF document created by R Markdown only on the first page of the document. I have searched for an answer and the closest I could find is this. There are multiple solutions presented in the answers, however, neither of them works for me because they either:
include the logo at every page of the document (not just first); or
include the chapter title in the header (and a horizontal line below it) along with the logo.
I am looking for a way to include just the plain logo, with no chapter titles only on the first page of a PDF R Markdown document and I've ran out of all the resources I could find online. Is there someone who could aid me?
Before anyone asks: yes, it strictly has to be a PDF document, not HTML.
The \includegraphics code line is insert after the R SETUP chunk in markdown.
Here is an example of where I put the line \includegraphics :
---
geometry: "margin=1.5cm"
classoption: landscape
output:
pdf_document: # Export to pdf
number_sections: yes
includes:
in_header: ltx.sty
---
```{r SETUP, include=FALSE, message=FALSE, warning=FALSE}
knitr::opts_chunk$set(echo = FALSE,
warning = FALSE,
message = FALSE,
results = "asis")
library(knitr)
library(kableExtra)
library(some_packages)
options(scipen = 999,
digits = 2,
width = 110)
```
\definecolor{astral}{RGB}{87,146,204}
\allsectionsfont{\color{astral}}
\setcounter{tocdepth}{5}
<!-- Title page -->
\includegraphics[width=7cm]{logo.jpg}
\begin{center}\begin{Large}
Project 1
\end{Large}\end{center}
\vfill
# Some R chunk
```{r results='asis'}
# Table, code, graphics.
```
So the line is insert between the R SETUP chunk and the next R chunk.

Using include_graphics in R Markdown does not reproduce the image in HTML file

I am attempting to use R Markdown Notebooks (.Rmd files) in R Studio to capture notes and excercises while learning R programming. I find that any plots generated through the code chunks are being replicated in the corresponding html file correctly, however I am unable to get images to be replicated in the html.
Sample code below -
The image is a .PNG file in the working directory path.
```{r}
library(knitr)
knitr::include_graphics("MyImage.PNG")
```
This replicates the image in the R Markdown Notebook correctly, but not in the html file.
I am able to replicate the image in the html file by directly using html syntax -
<img src="MyImage.PNG" alt="MyImage">
I have looked through other questions around this topic, but could not resolve this issue through any of the solutions provided. I would be grateful if any of you can help resolve this.
Thanks!
I think this might be a bug to do with adding shiny.
I just did a quick test and it works for a normal document:
---
title: "Test"
output: html_document
---
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE)
```
```{r, echo=FALSE, out.width="50%"}
include_graphics("../images/RMarkdownOutputFormats.png")
```
but when I add shiny it doesn't work anymore:
---
title: "Test"
output: html_document
runtime: shiny
---
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE)
```
```{r, echo=FALSE, out.width="50%"}
include_graphics("../images/RMarkdownOutputFormats.png")
```
I had the same problem when using shiny_prerendered with a learnr tutorial... This from Yan Holtz worked for me:
library(png)
library(grid)
img <- readPNG("photos/header_stats.png")
grid.raster(img)

Changing page size within Rmarkdown document

I have a very large phylogenetic tree that I'd quite like to insert into a supplementary material I'm writing using Rmarkdown and knitr. I dislike splitting trees across pages and I doubt anybody would print this out anyway so I thought I'd just have a large page in the middle of the pdf I'm generating.
The question is how do I change page size for one page in an otherwise A4 document? I'm pretty new to knitr and I've found global paper size options but I'm struggling to find ways of setting up what would be the equivalent of sections in Word.
(Update) Hi does anybody else have a suggestion? I tried the pdfpages package but this seems to result in an equally small figure on a page the size of the pdf that is being pasted in i.e. if I make a 20in by 20in pdf figure then paste the page in using \includepdf then I get a 20in by 20in page with a much smaller figure on it (the same as the \eject example above). It seems like knitr or Latex is forcing the graphics to have a specific size regardless of page size. Any ideas? Here's a reproducible example:
---
title: "Test"
output:
pdf_document:
latex_engine: xelatex
header-includes:
- \usepackage{pdfpages}
---
```{r setup, include=FALSE}
require(knitr)
knitr::opts_chunk$set(echo = FALSE,
warning=FALSE,
message=FALSE,
dev = 'pdf',
fig.align='center')
```
```{r mtcars, echo=FALSE}
library(ggplot2)
pdf("myplot.pdf", width=20, height=20)
ggplot(mtcars, aes(mpg, wt)) + geom_point()
dev.off()
```
#Here's some text on a normal page, the following page is bigger but has a tiny figure.
\newpage
\includepdf[fitpaper=true]{myplot.pdf}
You should be able to use \ejectpage like this:
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
\eject \pdfpagewidth=20in \pdfpageheight=20in
```{r mtcars}
library(ggplot2)
ggplot(mtcars, aes(mpg, wt)) + geom_point()
```
\eject \pdfpagewidth=210mm \pdfpageheight=297mm
Back
(I can only remember the A4 height in mm for some reason)
I have just faced the same struggle when dealing with a large phylogenetic tree.
Based on hrbrmstr's answer, I came up with the snippet below.
The trick is to make knitr generate the figure but not include it in the intermediate .tex right away (hence fig.show="hide"). Then, because figure paths are predictable, you can insert it with some latex after the code chunk.
However, the new page height is not taken into account when positioning page numbers, so they tend to be printed over your image. I have tried to overcome this behavior multiple times, but in the end I simply turned them off with \thispagestyle{empty}.
---
output: pdf_document
---
```{r fig_name, fig.show="hide", fig.height=30, fig.width=7}
plot(1)
```
\clearpage
\thispagestyle{empty}
\pdfpageheight=33in
\begin{figure}[p]
\caption{This is your caption.}\label{fig:fig_name}
{\centering \includegraphics[height=30in, keepaspectratio]{main_files/figure-latex/fig_name-1} }
\end{figure}
\clearpage
\pdfpageheight=11in

Resources