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)
```
Related
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
I´m new at R. I was trying to knit a Rmd file in RStudio into a pdf, but it presents the following error:
Quitting from lines 17-20 (projeto_final_20220429.Rmd)
Error in .External2(C_dataviewer, x, title) : unable to start data viewer
Calls: ... withCallingHandlers -> withVisible -> eval -> eval -> View
Execution halted
The code written in those lines was this:
library(readr)
despesas_municipios_saude_educacao <- read_csv("despesas_municipios_saude_educacao.csv")
View (despesas_municipios_saude_educacao)
Is there anyone who can help me?
On MacOS, knitr attempts to load the X11 library in order to view the data frame, which fails because knitr isn't designed to interact with an end user. Regardless of the underlying operating system, since utils::View() is meant to work in a windowing environment (e.g. RStudio, RGui, R Tools for Visual Studio, etc.), it can't be used to print a data frame in an R Markdown document.
---
title: "Error with View()"
output: html_document
date: "`r Sys.Date()`"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## A header
Here is a reproducible version of the stackoverflow question [Execution Halted when Knitting](https://stackoverflow.com/questions/72068679/execution-halted-when-knitting-in-rstudio).
```{r mtcars}
data(mtcars)
View(mtcars)
```
...generates the following error:
To display a subset of data in a markdown file, one can use head() instead of View().
---
title: "Error with View()"
output: html_document
date: "`r Sys.Date()`"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## A header
Here is a reproducible version of the stackoverflow question [Execution Halted when Knitting](https://stackoverflow.com/questions/72068679/execution-halted-when-knitting-in-rstudio).
```{r mtcars}
data(mtcars)
head(mtcars)
```
...and the output in HTML:
We can improve the look of the output by turning off echo on the R chunk used to print the table, and use knitr:kable() to format the data frame.
We use the following R Markdown code, including the "pipe" argument to appropriately space the columns in the output.
We can make the output look nicer with `knitr::kable()`.
```{r kableVersion,echo=FALSE}
library(kableExtra)
kable(head(mtcars),"pipe")
```
...and the output renders like this in HTML:
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
I am able to knit the following code to PDF. However, it always fails when trying to knit the output to Word (win 10, Rstudio 1.2.5033, R 3.6.2, papaja 0.1.0.9942). I had to delete the papaja header for posting (too much code).
```{r setup, include = FALSE}
library("papaja")
```
# Methods
```{r figure}
plot(cars)
```
The error message reads
Error running filter D:/Boelte/R_library/papaja/rmd/docx_fixes.lua:
[string "--[[..."]:227: Constructor for Emph failed: [string
"--[[..."]:258: attempt to index a nil value (local 'x')
stack traceback: [C]: in function 'error'
..."]:227: in field 'Emph' D:/Boelte/R_library/papaja/rmd/docx_fixes.lua:14: in function 'Image'
Fehler: pandoc document conversion failed with error 83
Is there any way to correct this error? It is a papaja or a pandoc error?
This is a papaja error related to post-processing of the document (in this case styling figure captions) via the docx_fixes.lua-filter. I will try to fix this as soon as possible. For the time being, you should be able to resolve this problem by specifying a figure caption in the chunk options.
```{r setup, include = FALSE}
library("papaja")
```
# Methods
(ref:fig-cap) This is the figure caption.
```{r figure, fig.cap = "(ref:fig-cap)"}
plot(cars)
```
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: