11

I'm trying to write a document that discusses using errors to communicate problems with the arguments to the user. Unfortunately, I can't seem to get the .Rmd file to knit. A short example:

Intro text

```{r}
some_function <- function(x, y)
{
  if (x < 0) stop("x must be greater than 0")
  x + y
}

some_function(3, 2)
```

```{r}
some_function(-3, 2)
```

When I try to knit this to any format, I get the error

Quitting from lines 14-15 (test.Rmd) 
Error in some_function(-3, 2) : x < 0
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> some_function

Execution halted

Everything I've read so far points to this being a problem with either a) not loading a package, or b) an incorrectly set path in the Rmd file.

Since I'm only using base functions here and am not referencing any files (that I'm aware of, anyway), I don't think either of those are my problem (but I'll be happy to be wrong).

Any tips on what I need to do to get the document to knit?

Solution

Add the following to the top of the .Rmd

```{r, echo=FALSE}
knitr::opts_chunk$set(error = TRUE)
```

Short explanation, RMarkdown v1 used error = TRUE by default, but RMarkdown v2 uses error = FALSE. See the link in Josh's comment below.

Benjamin
  • 16,897
  • 6
  • 45
  • 65
  • Yes, it will stop knitting on an error (clearly occurs at the some_function(-3, 2) call. What is your desired result? It prints the error or something? – Josh W. May 29 '15 at 01:43
  • That's correct. I want it to display the error. – Benjamin May 29 '15 at 01:46

2 Answers2

8

Don't compile with the button in rstudio. Try:

library("knitr")
knit2html("file")
Josh W.
  • 1,123
  • 1
  • 10
  • 17
  • That's interesting. This renders the file as I want it. The next question is whether I can apply this so that I can write this up as a vignette in a package. – Benjamin May 29 '15 at 01:53
  • 2
    see here: http://rmarkdown.rstudio.com/authoring_migrating_from_v1.html . It's just a different default chunk setting to knitr. – Josh W. May 29 '15 at 01:55
  • Well now I feel silly. I thought I had tried that already. Which can only mean I've been awake too long. Thanks, Josh. – Benjamin May 29 '15 at 02:01
  • 6
    If you're reading this on 2017, then `rmarkdown::render('file.rmd', output_format = 'html_document')` – Manos Parzakonis Jul 31 '17 at 11:21
  • 3
    if you're reading this in 2019 then the comment above for 2017 still works – user300 May 26 '19 at 19:20
  • If you're arriving here because of `withCallingHandlers -> withVisible -> eval -> eval`, be sure to also make sure you are using `\`\`\`{r}` and not `\`\`\`r` – Theo Belaire Jul 02 '20 at 23:23
  • If you're reading this in 2020, you will get an error asking you to call rmarkdown::render() probably because your R Markdown is v2 – Kay Sep 09 '20 at 11:17
  • For me the problem was because I was loading packages in a chunk that was being cached. For some reason these packages are not visible to later chunks. Either disable the cache or move the loading to a different chunk. – Chechy Levas Jan 19 '21 at 12:54
  • hi @ChechyLevas what do u mean by that? Thanks – Zawir Amin Feb 16 '21 at 07:14
0

In my case (June 2021, R 4.06, latest RStudio), replacing

```{r eval=T, message=F, warning=F, error=F}

with

```{r message=FALSE, warning=FALSE, error=FALSE}

fixed this problem:

Error in options$error && options$include : invalid 'x' type in 'x && y' Calls: ... call_block -> block_exec -> in_dir -> evaluate -> `

Very odd....

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
Pete
  • 1