How to remove metadata from rmarkdown rendered documents? - r

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

Related

rmarkdown read code from file and display with highlight

I have two RMarkdown files. main.Rmd which is the main file which is rendered as well as example.Rmd which holds a longer example and is used elsewhere (hence it lives in its own document).
I want to include example.Rmd in the main.Rmd file with code highlighting of its RMarkdown code but the code of example.Rmd does not need to be executed, as if I set eval=FALSE and copied all code into the chunk by hand.
An example MWE is
main.Rmd
---
title: This is main.rmd
output: html_document
---
```{r}
# attempt that doesnt work
cat(readLines("example.Rmd"), sep = "\n")
```
and in example.Rmd
---
title: This is example.rmd
output: html_document
---
```{r}
# code that is not executed but shown in main.Rmd
data <- ...
```
Set eval=FALSE in the example.Rmd file and then include it in main.Rmd using child chunk option.
example.Rmd
---
title: This is example.Rmd
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(eval = FALSE)
```
```{r}
# This is from example.Rmd
x <- rnorm(10)
y <- rnorm(10)
lm(y ~ x)
```
```{r}
# some comments
print("This is from example.Rmd")
```
main.Rmd
---
title: This is main.Rmd
output:
html_document:
highlight: haddock
---
```{r example, child="example.Rmd"}
```
Edit
To show full source-code of the Rmarkdown file, one possible option could be reading that Rmd file and then cat it with chunk option comment="".
Now about the syntax highlighting; there's a chunk option class.output with which it is possible to specify a language name for which pandoc supports syntax highlighting.
You can get the list of language names for which pandoc has syntax highlighting support by running the following,
pandoc --list-highlight-languages
(Note, if you don't have pandoc installed separately, you can also use the pandoc installed with Rstudio itself. Run rmarkdown::pandoc_exec() to get the pandoc executable path)
Now, the file we are trying to include actually contains not just R code, but also markdown and yaml syntaxes. So it's a kind of mixed thing and pandoc has no syntax highlighting support out of the box for this. Still I have chosen c as syntax highlighting language just to show the possibility. (Also tried r, but syntax-highlighting is not so distinctive)
---
title: This is main.Rmd
output:
html_document:
highlight: tango
---
## Rmarkdown
```{r example, echo=FALSE, class.output="c", comment=""}
cat(readLines("example.Rmd"), sep = "\n")
```
But still if you want a specific syntax-highlighting for Rmarkdown, you can actually create one. See here from the pandoc documentation itself and also this answer on SO about this.

How to include a multipage PDF in RMarkdown with dynamic paths?

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`}

How to not run a chunk that will have an error

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
```

`fontawesome` icons ignored when knitting Rmarkdown to PDF

Unfortunately, I fail to knit an *.Rmd file in RStudio to PDF that includes fontawesome-icons. However, when knitting to output: html_document in renders perfectly.
Info:
I am using R version 4.0.3 with RStudio 1.3.1056
I have installed the latest TeX Live distribution
The following example contains elements taken from RStudio's Github:
Icon renders in HTML:
---
title: "FontAwesome in R Markdown"
output: html_document
---
```{r load_packages, message=FALSE, warning=FALSE, include=FALSE}
library(fontawesome)
```
This is an R icon: `r fa("r-project", fill = "steelblue")`.
Icon is ignored for PDF output:
---
title: "FontAwesome in R Markdown"
output: pdf_document
---
```{r load_packages, message=FALSE, warning=FALSE, include=FALSE}
library(fontawesome)
```
This is an R icon: `r fa("r-project", fill = "steelblue")`.
Neither R nor Rmarkdown shows any errors when rendering to PDF. Does anyone have an idea why it does not work?
Thank you very much.
Cheers,
Christian
You can workaround the problem by using the fontawesome5 latex package:
---
title: "FontAwesome in R Markdown"
output:
pdf_document:
keep_tex: true
header-includes:
- \usepackage{fontawesome5}
---
This is an R icon: \faRProject

R Markdown : \#ref() not working

I'm having a lot of trouble getting basic references to work in R Markdown. To reduce complexity from my original project, I've decided to use the bookdown example code, but I'm experiencing the same problem. Here's a link to the intro exmample code: https://github.com/rstudio/bookdown-demo/blob/master/01-intro.Rmd
When I use Knitr to HTML or PDF the file is being generated fine but the references are not working, instead the file will just containt "#ref(example)". Here is an image to show better the output (my emphasis added in red):
Direct link to image: https://i.imgur.com/2yxB5h3.png
Here is a minimal example:
---
title: "Minimal"
output:
pdf_document:
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a reference to the plot below \#ref(fig:minGraph)
```{r minGraph, echo=FALSE, fig.cap="\\label{fig:minGraph}test"}
plot(x=1)
```
With the output appearing as such:
https://i.imgur.com/J3UECqn.png
If you want make use of the bookdown extensions in a normal rmarkdown document you can use bookdown::html_document2 and bookdown::pdf_document2 instead of rmarkdown::html_document and rmarkdown::pdf_document. Example:
---
title: "Minimal"
output:
bookdown::html_document2:
fig_caption: yes
bookdown::pdf_document2:
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a reference to the plot below \#ref(fig:minGraph)
```{r minGraph, echo=FALSE, fig.cap="test"}
plot(x=1)
```
Looks like I was getting my syntax confused by reading the bookdown guide while using just R markdown. Thanks to Ralf for pointing me in the this direction. The correct minimal code would be like so:
---
title: "Minimal"
output:
pdf_document:
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a reference to the plot below \ref{fig:minGraph}
```{r minGraph, echo=FALSE, fig.cap="\\label{fig:minGraph}test"}
plot(x=1)
```

Resources