RMarkdown PDF add non-spacing linebreak inside paste0() - r

I want to use RMarkdown to combine several sentences in the paste0-function, and insert a non-spacing line break in between.
In pure Markdown, a simple backslash \ is sufficient to generate a non-spacing line break. However, this does not work in the character string of the paste0-function.
I have already inserted \n\n between the sentences. However, there is a space between the lines, which I want to avoid.
Can anyone help me with my problem?
The programmatic scheme with the code chunk and the subsequent inline code should be the same.
Here is my example code:
---
title: "Untitled"
output: pdf_document
date: "2023-01-17"
---
```{r echo=FALSE}
result_1 <- 100
result_2 <- 200
```
```{r echo=FALSE}
final_text <- paste0("My first result is ", result_1, "My second result is ", result_2)
```
`r final_text`
The first and second sentences are the result of the \n\n-separator.
The third and fourth sentences are my desired outcome (created with a \ in plain Markdown)

The result of putting `r final_text` into your text will be the same as if the characters in the final_text variable were inserted there. So you just need to insert the characters that resulted in the output you want.
If those characters are
My first result is 100\
My second result is 200
then you can get that output using this R code:
---
title: "Untitled"
output: pdf_document
date: "2023-01-17"
---
```{r echo=FALSE}
result_1 <- 100
result_2 <- 200
```
```{r echo=FALSE}
final_text <- paste0("My first result is ", result_1, "\\\nMy second result is ", result_2)
```
`r final_text`
The explanation for the string \\\n at the start of the second line is as follows: Use \\ to get a single backslash, then use \n to get a line break.

Related

How to generate rmarkdown chunk within a chunk

I'm trying to generate a rmarkdown chunk using code. I've read similar questions and their solutions, such as using pander or using cat. I just can't seem to generate it. I also tried knitting the output manually. Here's an example of a Rmd file:
---
title: "test"
output: pdf_document
---
## R Markdown
```{r, results='asis',echo=FALSE}
txt <- paste("```{r}",
"2+2",
"```")
pander::pander(txt)
```
When I knit this, I get a verbatim {r} 2+2. I would like to see the number four instead. In my real example, I'm using bookdown and trying to generate a block2 chunk.
Any ideas how to generate this chunk that gets evaluated as R code?
Does this do what you want?
## R Markdown
```{r, results='asis',echo=FALSE}
txt <- paste("```{r}",
2+2,
"```")
pander::pander(txt)
```
This evalutates to
```{r} 4 ```
in your markdown document.
You using a string literal "2+2" as opposed to the expression 2+2. This is the first issue, I guess.
If you want it correctly parsed you need to add an sep = "\n" argument to paste or manually add the newline breaks.
I.e.
## R Markdown
```{r, results='asis',echo=FALSE}
txt <- paste("```{r}\n",
2+2,
"\n```", sep = "")
pander::pander(txt)
```
This evalutates to
```{r}
4
```
which is interpreted as R code in the markdown document.

Formula's or symbols in footnotes using knitr and kableExtra

Does anyone know how to place a formula, a (weird) character, or words in italic within a sentence of a footnote of a table?
I'm creating a pdf file with Rmarkdown and kableExtra. But stuff like $Y_{t-1}$ or $p < .001$ (since I want the p to be italic) does not work. Or should I really learn xtable?
The trick is 1. to escape latex code and special characters four times, e.g. \\\\frac, 2. to set option escape=FALSE in footnote().
---
title: "Untitled"
output: pdf_document
---
```{r tab}
library(knitr)
library(kableExtra)
df <- data.frame(v1=rnorm(6), v2=runif(6), v3=rbinom(6, 1, .33),
row.names=LETTERS[1:6])
kable(df, "latex", align="c", booktabs=TRUE) %>%
footnote(general=c("$a^2+b^2=c^2,$",
"$\\\\sigma^2=\\\\frac{1}{n-1}\\\\sum_{i=1}^n(x_i-\\\\bar{x})^2;$",
"1,000 \\\\$;", "100\\\\%."),
number=c("Hello\ there! \\\\textit{Hello\ there!}"),
footnote_as_chunk=TRUE,
escape=FALSE)
```
Yields:

How to generate headers functionally?

How can I generate a narrative functionally in RMarkdown? For instance, say I want to generate 3 headers like
##First
##Second
#Third
from a vector c('First', 'Second', 'Third'), then knit to pdf
If you're trying to knit to pdf, this solution calls on LaTeX within an R code chunk to create the headers as you requested. Just add this into an .Rmd file:
---
title: "Untitled"
author: "Ryan Runge"
date: "8/30/2017"
output: pdf_document
---
```{r eval=TRUE, results='asis' }
headers <- c('First', 'Second', 'Third')
cat(paste("\\section{",headers,"}"))
```
The output is:
And if you don't want the R code chunk to show in the pdf, just supply echo=FALSE in the code chunk options.
You can reference a pre-defined list of headers in RMarkdown as follows:
```{r}
list_of_headers <- c("Title 1", "Title 2", "Title 3")
```
This is an example.
### `r list_of_headers[1]`
Stuff under the first header
### `r list_of_headers[2]`
Etc
### `r list_of_headers[3]`

Removing exponents from RMD output

I am trying to output a large number that is stored in a variable in a RMD file. I would like the number to print something like
Large number: 4123125.2
however when I knit the rmd it always comes out as
Large number: 4.123125210^{6}
I would like to do away with the exponent notation to make it easier to read.
---
output: html_document
---
```{r}
large.number <- 4.1231252*10^6
```
Large number: `r large.number`.
```{r}
large.number <- 4123125.2
```
Large number: `r large.number`.
use this syntax:
sprintf("%f", large.number)
you can then define how many figures you want to be printed:
sprintf("%.2f", large.number)

R inline markdown

I’m using R Markdown in RStudio to create a report that mixes Markdown and R output. I know how to use inline R expressions in the Markdown, but I’m wondering how to do the converse, i.e., use Markdown within the R code. I want to loop through a series of calculations and get Markdown headings for each one. I want headings for formatting purposes (e.g., bold titles etc) and also to be able to specify (sub)sections in the resulting PDF (which is a nice feature of the way RMarkdown handles # and ## etc).
I know I can do the following:
---
title: "test"
output: pdf_document
---
#Section 1
```{r, echo=FALSE}
print(1+1)
```
#Section 2
```{r, echo=FALSE}
print(2+2)
```
#Section 3
```{r, echo=FALSE}
print(3+3)
```
Which gives something looking (roughly) like this:
Section 1
## [1] 2
Section 2
## [1] 4
Section 3
## [1] 6
Is it possible to achieve the same output using something along the lines of this:
---
title: "test2"
output: pdf_document
---
```{r, echo=FALSE}
for (i in 1:3)
{
print(paste("#Section",i))
print(i+i)
}
```
As #scoa pointed out, you have to set the chunk option results='asis'. You should also place two \n both before and after your header.
---
title: "test"
output: pdf_document
---
```{r, echo=FALSE, results='asis'}
for (i in 1:3) {
cat(paste0("\n\n# Section", i, "\n\n"))
print(i+i)
cat("\n\n\\newpage")
}
```
As a more general answer, it could be useful to a look at the markdownreports package that parses markdown code (and output) from your R variables.

Resources