Why R Markdown Caption cannot take "&" - r

I am gradually building an R Markdown (.RMD) file, learning by doing. I was able to insert a couple of tables, but I had a problem with one of them. The initial setup is:
---
title: "Untitled"
author: "Me"
date: "5/10/2021"
output: bookdown::pdf_book
---
```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = FALSE,
fig_align = "left",
fig_width = 7,
fig_height = 7,
dev = "png",
cache = FALSE)
```
The original code that generated an error was
```{r sphistperf}
kable(stock_index_stats,
format="latex",
caption="S&P Historical Performance Statistics")
```
The error message is:
output file: TestCenter.knit.md
! Misplaced alignment tab character &.
<argument> ...}{\relax }}\label {tab:sphistperf}S&
P Historical Performance S...
l.202 ...rf}S&P Historical Performance Statistics}
Error: LaTeX failed to compile TestCenter.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See TestCenter.log for more info.
Error during wrapup:
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
The problem is fixed if I remove the "&" from the caption, which becomes
caption="SP Historical Performance Statistics"
Still, I want the "&" in my caption. Is there a way to keep it? I tried putting an escape character "" before it and that did not work. Any suggestions on how to keep the "&"?

According to wiki, there are some characters that needs escaping
Here, is a tested version of the markdown code
---
title: "testing"
author: "akrun"
date: "10/05/2021"
output: bookdown::pdf_book
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r cars}
library(kableExtra)
kable(summary(cars), format = 'latex', caption="Dummy S\\&P Performance")
```
-output

Related

R Markdown producing error: Input must be a vector, not an environment?

I have to update some reports I created a while ago using Markdown and when I even try to just create a title page I get an error. Running the code below
---
title: "Report"
author: "Author"
date: "12/16/2022"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
produces this error message:
processing file: Testing.Rmd
Error in stop_vctrs():
! Input must be a vector, not an environment.
Backtrace:
rmarkdown::render(...)
vctrs:::stop_scalar_type(<fn>(<env>), "")
vctrs:::stop_vctrs(msg, "vctrs_error_scalar_type", actual = x)
Execution halted
Any idea what's going on? Been searching online for an hour and can't find anything

How to cache intermediate results when rendering with parameters?

With RMarkdown, I try to render a parametrized report for different values of a parameter. The Rmd file use caching.
The caching works as intended if I knit in RStudio, with the knit button : cache built at first, then used at each successive knitting, even if I change the parameter value in the YAML header.
But when looping with my parameters values and using rmarkdown::render() the cache is rebuilt at each iteration.
The test.Rmd file
---
title: "Untitled"
author: "Author"
params:
id: 0
date: "23/10/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Test `r params$id`
```{r cars, cache=TRUE}
## open and work on large file (simulate)
test <- mtcars
Sys.sleep(10)
```
And the rendering script : render.R
library(rmarkdown)
library(tidyverse)
1:5 %>%
walk(function(x) render("test.Rmd",
params = list(id = x),
output_file = paste0("file", x, ".html")))
The script takes 5 * 10 seconds to run instead of about 10 seconds.
What did I do wrong? How to use the cache?
It has nothing to do with parameters, which can be shown by the minimized reprex below (test.Rmd) by taking out the parameters (and the irrelevant tidyverse):
---
title: "Untitled"
---
```{r, cache=TRUE}
Sys.sleep(10)
```
Then run
for (i in 1:5) rmarkdown::render(
"test.Rmd", output_file = paste0("file", i, ".html")
)
The problem comes from output_file, which changes in each iteration. For R Markdown documents, the output filename determines the knitr chunk option fig.path. For example, when output_file = "file1.html", fig.path is set to file1_files/html/.
When any chunk option of a code chunk changes, knitr will invalidate its cache. In your case, fig.path invalidated the cache each time. To avoid that, you have to stabilize this option, e.g.,
---
title: "Untitled"
---
```{r, cache=TRUE, fig.path='test_files/html/'}
Sys.sleep(2)
```

`knitr::include_url` ignored in output

After updating R to version 3.6.0, and re-installing the required packages, knitting Rmd documents now seems to ignore the knitr::include_url function.
The following is a minimal example, in a file named test.Rmd -
---
title: "test"
author: "Michael Dorman"
date: "May 1, 2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r}
knitr::include_url("https://www.wikipedia.org/")
```
```{r}
knitr::include_graphics("https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png")
```
And here is a screenshot of the output, after pressing Knit in Rstudio -
The knitr::include_url function is ignored. No <iframe> element was produced. The knitr::include_graphics function still works, so the image does appear in the output.
Thanks for reading, I appreciate any help!

Include error messages from testthat in R markdown document

I want to include error messages in an R markdown pdf report. This works well:
---
output: pdf_document
---
This will be knitted and show the error message in the pdf.
```{r, error = TRUE}
stopifnot(2 == 3)
```
However, if I try the same approach with an error that comes from testthat, my document does not knit anymore.
---
output: pdf_document
---
This will not knit
```{r, error = TRUE}
library(testthat)
expect_equal(2, 3)
```
Why is that? And what can I do to include error messages from testthat's expect_something functions without wrapping them up in a test?
I think this must be possible since Hadley Wickham includes many error messages in his book R packages that come directly from expect_something-functions.
This is related, but not answered in Include errors in R markdown package vignette and How to skip error checking at Rmarkdown compiling?
Create a test:
```{r, error = TRUE}
library(testthat)
test_that("Test A", {
expect_equal(2, 3)
})
```
I don't understand the reason for the behavior (good question!), but this could be a workaround:
---
output: pdf_document
---
This will knit
```{r, error = TRUE}
library(testthat)
# expect_equal(2, 3)
# skip_if_not(2, 3)
assertthat::assert_that(2 == 3)
```

Can Rmarkdown having a non working code, knit html output showing the errors and warnings

Lets say I have code in R which is not working, i.e. I run that code and get some errors and warnings, and I want to share the code and output showing errors and warnings, with the third person through R markdown.
Is it possible to knit R markdown if I have errors in r code chunks?, If yes, then is it going to show me waht errors and warnings occurs in the html output? Goal is to share the html output showing errors and everything with the non working code.
Any help on this is highly appreciated. Thanks.
Yes, use knitr::opts_chunk$set(error = TRUE)
Here is the full markdown:
---
title: "Untitled"
date: "September 21, 2016"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, error = TRUE)
```
## R Markdown
Here is my error
```{r}
1 + 1
1 + "a"
```
Output:

Resources