rmarkdown render not recognizing parameters - r

In my R file I have:
library(rmarkdown)
rmarkdown::render("C://Users//me//Desktop//test_param.Rmd",
params = list(region = "west"))
and in the rmd file:
---
title: "test"
output: pdf_document
params:
name: "test"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
`r params$name`
that rmd works when I run in separately.
BUT when I run the .r file I get this error
Error in knit_params_get(input_lines, params) :
render params not declared in YAML: region
what's the reason for this error?

knitr's override overrides existing params. You need to have a front matter like:
---
title: "test"
output: pdf_document
params:
name: "test"
region: ""
---

Related

using params from YAML header with a shiny runtime Rmd

I would like to use params to pass settings for a shiny app. Consider the following document, which just prints a message when the checkbox is changed. I would like the message to be configuable using params in the yaml header.
However, it seems this fails as params seems to be only available when the document is rendered (notice that it correctly prints the message in the document itself). Is this correct? Is there any way of reading params at runtime? Is this documented anywhere?
Document is below:
---
title: "Test"
output: html_document
runtime: shiny
params:
message: "hi"
---
Can see this: `r params$message`
```{r eruptions, echo=FALSE}
shiny::checkboxInput("cb","Checkbox")
```
```{r, echo=FALSE}
observeEvent(input$cb, {
# This works
message("clicked")
# This fails:
# Error in message(params$message) : object 'params' not found
try(message(params$message))
})
```
This works by setting params to a non-local variable:
---
title: "Test"
output: html_document
runtime: shiny
params:
message: "hi"
---
```{r}
params <<- params
```
......

R Markdown code chunk does not show math equations

Basically i want to build a random multiple choice question generator with R Markdown. For this task there need to be equations in the code chunks of the markdown.
The following works like a charm and gives the equation "greekbeta = 1"
---
title: "test"
author: "me"
output:
word_document: default
---
```{r eval=TRUE, echo=FALSE,results = "asis"}
"$\beta = 1$"
```
In contrast, this will not work when some other math symbol is used, for example:
---
title: "test"
author: "me"
output:
word_document: default
---
```{r eval=TRUE, echo=FALSE,results = "asis"}
"$\sum_{n=1}^{\infty}$"
```
After pressing knit, an error occurs (unfortunately the error message is in german, basically this: "'\s' is an unknown escape-sequence within the string starting with "$/s"").
I am very puzzled by this, especially because for example \frac{1}{2} works, but \hat{x} does not. Equations in the "normal" markdown text are no problem at all. But for my task, the equations have to be in the code chunk sections.
Does someone has a workaround for this problem? I tried using "$\hat{x}$" or even "$$\hat{x}$", but the error message is still the same.
I am using pandoc 2.11.4, R 4.1.0 and knitr 1.33
Use cat() and escape the escapes.
---
title: "test"
author: "me"
output:
word_document: default
---
```{r eval=TRUE, echo=FALSE,results = "asis"}
cat("$\\beta = 1$", '\n\n')
cat("$a^2+b^2 = c^2$", '\n\n')
cat("$\\sum_{n=1}^{\\infty}x_i$", '\n\n')
```

r - rmarkdown - yaml - Is it possible to include inline, internal document hyperlink in title?

is it possible to include an inline (internal document) hyperlink in the YAML title block of a RMarkdown document? Or is there a R package that allows this?
Below is what I am trying to do:
---
title: blah {#one1} and so on {#two2}
...
---
# Bibliography
1. Reference note to [explain](#one1) in title
2. Reference note to [explain](#two2) in title
Thank you.
The title is parsed as markdown, so you can use regular markdown link syntax in order to get the behavior you want.
---
title: 'There is a link in this title...[LINK](#anchor)'
author: "Jason Punyon"
date: "6/16/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Bibliography
<p id='anchor'>Here's where I went!</p>
You could also just put the link in manually...
---
title: 'There is a link in this title...LINK'
author: "Jason Punyon"
date: "6/16/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Bibliography
<p id='anchor'>Here's where I went!</p>

R Markdown keywords don't appear in the document

I want to knit a pdf-document but the keywords don't appear in the final document. Can someone say me what I'm doing wrong?
---
title: "title"
subtitle: "subtitle"
author: "author"
date: "09 04 2019"
output:
pdf_document:
keywords: "word1, word2, word3"
footerdate: yes
abstract: 'Insert abstract here'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Introduction
When knitting to PDF, keywords are sent to the file metadata but are not actually visible in the file. Source
You can customize the template for PDF generation or render Rmd files with base_format: rticles::elsevier_article.
However, there is another way to show keywords on the PDF just by adding few codes to the Rmd file:
Add a LaTeX code to provide the \keyword command (\providecommand{\keywords}[1]{\textbf{\textit{Keywords---}} #1}) into YAML's header-includes key;
Add keywords in the text section using the LaTeX command
---
title: "title"
subtitle: "subtitle"
author: "author"
date: "09 04 2019"
output:
pdf_document:
# keywords: "word1, word2, word3" # <- You don't have to add your keywords here since they only appear as the 'invisible' metadata of PDF
footerdate: yes
abstract: 'Insert abstract here'
header-includes:
- |
```{=latex}
\providecommand{\keywords}[1]{\textbf{\textit{Keywords---}} #1}
```
---
```{=latex}
\keywords{word1, word2, word3}
```
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Introduction

Can you call/render a parameterized rmd report within another parameterized rmd report - rmarkdown

I'm wondering if it is actually possible to call/render a parameterized report from within another parameterized report?
I found [this][1] but it doesn't seem to come up with a solution.
Below is a minimal example where main-report.rmd tries to call/render sub-report-1.rmd . Both reports have the same params in the YAML header.
library(here)
sub-report-1.rmd
---
title: "Secondary report to run"
output: html_document
params:
country: "Canada"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
paste0("Hello ", params$country)
```
main-report.rmd
---
title: "Main report"
output: html_document
params:
country: "France"
---
```{r run1, include=FALSE}
rmarkdown::render(here::here("rmd", "sub-report-1.rmd"),
output_format = "html_document",
output_file="report1.html",
params = list(country=params$country))
```
I get the following error:
Error: params object already exists in the knit environment so can't
be overwritten by rend param. Execution halted.
The solution is to use another parameter in the render function: envir = new.env(). The problem is that object params is already used.
rmarkdown::render(here::here("rmd", "sub-report-1.rmd"),
output_format = "html_document",
output_file="report1.html",
params = list(country=params$country),
envir = new.env())

Resources