I am working to knit an html file with images that will be updated weekly. I am using an Rmarkdown file to specify file path to images as so:
---
title: "test"
output:
html_document:
toc: true
toc_depth: 2
toc_float: true
theme: flatly
params:
user1: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts.png"
user2: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts5.png"
---
I will be generating these report weekly so one week we might have an image for user 2 and one week we might not have an image for user 2. So I know that I can knit and get an html running with setting error = TRUE. But that still displays the error message in the html output where the image is missing. Is there a way to set up a condition where if a file is not present, that chunk will not run where I read in the file? Im using magick package to read in each image from it path specified in the params.
you can pass R statements to knitr options as well, as in the following.
---
title: "test conditional chunks"
output: html_document
params:
user1: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts.png"
user2: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts5.png"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r condition 1, echo=FALSE, eval=file.exists(params$user1)}
# plot image
```
```{r condition 1, echo=FALSE, eval=file.exists(params$user2)}
# plot image
```
Related
I was able to figure out how to include a multi-page PDF document in my RMarkdown output using "\includepdf", but I need to be able to change the path to different PDFs based on conditions in the data. I'd like to be able to call the PDF file path based on an object I set in a chunk beforehand (e.g. "test"). Any ideas?
Thanks!
---
title: "Personnel Reports"
output:
pdf_document
header-includes:
- \usepackage{pdfpages}
---
This works:
\includepdf[pages=-,pagecommand={}]{Person1_report.pdf}
```{r global_options, include=FALSE}
test <- "Person2_report.pdf"
```'
This doesn't work:
\includepdf[pages=-,pagecommand={}]{test}
---
title: "Personnel Reports"
output:
pdf_document:
keep_tex: true
header-includes:
- \usepackage{pdfpages}
---
```{r global_options, include=FALSE}
test <- "example-image-duck.pdf"
```
\includepdf[pages=-,pagecommand={}]{`r test`}
I'm using R markdown to make a beamer presentation.
When I try to include an error message output from R on the slide it goes off the page.
Here is my code:
---
title: "Untitled"
author: "John Smith"
date: '2022-04-29'
output: beamer_presentation
---
This error message goes off the page
```{r cars, echo = TRUE, error=TRUE}
summary(carrrrrrrrrrrrrrrrs)
```
And, this is what the slides look like:
How do I keep the code on the page, either by putting it on multiple lines or reducing font size?
You can use the same technique as in Change representation (background-color & frame) of chunks in RMarkdown (with Beamer-Presentations) to redefine the verbatim environment in such a way that it uses the listings package. The listings package then allows you to both add line breaks and adjust the font size to your liking:
---
title: "Untitled"
author: "John Smith"
date: '2022-04-29'
output:
beamer_presentation:
keep_tex: true
header-includes:
- \let\verbatim\undefined
- \let\verbatimend\undefined
- \usepackage{listings}
- \lstnewenvironment{verbatim}{\lstset{breaklines=true,basicstyle=\ttfamily\footnotesize}}{}
---
This error message goes off the page
```{r cars, echo = TRUE, error=TRUE}
summary(carrrrrrrrrrrrrrrrs)
```
I have a RMarkdown document that I'm rendering as a word document using a template as reference.
This is the code:
---
output:
word_document:
pandoc_args: [
"--reference-doc=template_report.docx"
]
always_allow_html: no
---
#Report starts here
```{r setup, include=FALSE, warning=FALSE}
library(pander)
library(rmarkdown)
library(webshot)
#Some code
```
The issue is that the word document has the same metadata printed in the report which I don't want.
output:
word_document:
pandoc_args: [
“–reference-doc=template_exec_report.docx”
]
always_allow_html: yes
Report starts here
Is there a way to remove this?
Could this because of change in R-versions. We recently moved to RStudio cloud environment and there might be a change in R-version.
Edit: This is occurring this in R 4.0.5. It is working fine in R 3.5.0
Can anyone help me understand how to write my header so that the figure caption and cross reference works?
I am practicing making captions and cross references to a simple plot in my Rmd file. I understand that to do so, I should add to my header: "output: bookend::pdf_document2" and "fig_caption = yes". Then, to a chunk called myfigure, I should add "fig.cap = "\label{fig:myfigure} My caption". To cross reference this figure I should write in the text "#ref(fig:myfigure)". My code is below. It won't knit because the formatting of the header is wrong.
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: bookdown::pdf_document2
toc: true
fig_caption: yes
---
```{r myfigure, fig.cap = "\\label{fig:myfigure} My caption"}
plot(pressure)
```
My plot is called \#ref(fig:myfigure).
Then, I tried deleting the whitespace before toc and fig_caption, and it knit, but no caption appeared, and the text literally printed "#ref(fig:myfigure)" instead of a cross reference. The header I tried is here:
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: bookdown::pdf_document2
toc: true
fig_caption: yes
---
I also tried adding "pdf_document:" to the header, but the same issue of no caption and the cross reference being literally "#ref(fig:myfigure)". This header I tried is here:
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output: bookdown::pdf_document2
pdf_document:
toc: true
fig_caption: yes
---
Can anyone help me understand how to write my header so that it works?
use \ref{fig:myfigure} instead of \\#ref(fig:myfigure)
See RStudio Community Post
You have a wrong YAML header and some wrong understanding of referencing. I used this RMD file:
---
title: "knit"
author: "Chase Hommeyer"
date: "4/1/2019"
output:
bookdown::pdf_document2:
toc: true
fig_caption: yes
---
```{r myfigure, fig.cap = "My caption"}
plot(pressure)
```
My plot is called Figure \#ref(fig:myfigure).
First, break the line after output in the header. The whitespaces are very important in the YAML header!
Then, read the bookdown manual:
The label of a figure environment is generated from the label of the code chunk, e.g., if the chunk label is foo, the figure label will be fig:foo (the prefix fig: is added before foo). To reference a figure, use the syntax, where label is the figure label, e.g., fig:foo.
To reference your plot with the chunk name "myfigure", just write \#ref(fig:myfigure). The caption of the figure can be set via fig.cap in the chunk options.
I have an RMarkdown file used for creating a QC report on other sets of data, and the report is relatively long. My table of contents essentially contains the name of all the checks I run on the data, and I'd like to have the table of contents itself exported into a new document, for outside users to quickly see what checks are run.
Is there any way to export just the TOC to word/pdf/html, or otherwise 'roll up' and export the different sections of an RMarkdown file?
I'm not entirely clear on what you're trying to achieve.
Perhaps you're after a "floating" TOC, like this?
---
title: "Title"
output:
html_document:
toc: true
toc_float: true
---
# Section 1
```{r}
stringi::stri_rand_lipsum(4, start_lipsum = TRUE)
```
# Section 2
```{r}
stringi::stri_rand_lipsum(4, start_lipsum = TRUE)
```
For a html_document output format, you can set the body Pandoc variable to a void string:
---
title: "Title"
output:
html_document:
toc: true
pandoc_args: ['-V', 'body=""']
---
This solution will not work for a pdf_document or a word_document.