3

I'm trying to take an Rmarkdown file written by someone else and run it from the command line (not RStudio) using

Rscript -e 'library(rmarkdown); rmarkdown::render("input.Rmd")'

Execution continues along printing things like this to my console as it processes each chunk:

label: readDataFiles (with options) 
List of 1
 $ error: logi TRUE

  |..............                                                   |  22%
  ordinary text without R code

However, a chunk is failing and execution stops. I believe it is because a path passed to a function is not valid. So I tried to add a new chunk that displays the value of this path to my console:

``` {r thePath, eval=True}
path
```

However I just see the output

label: thePath (with options) 
List of 1
 $ eval: logi TRUE

The value of the path variable is not visible. And I do not get out an HTML file to look at because the overall knitting process fails. How can I debug this in my console?

I've tried this answer with no success.

rgov
  • 3,516
  • 1
  • 31
  • 51

1 Answers1

4

You can use stop(path) to kill the compile with an error message showing you the path. For example, put this in your .Rmd file:

```{r}
path <- "c:/wrong/path"
stop("path=",path)
```

This is what I see when I run this file on the command line:

label: unnamed-chunk-1
Quitting from lines 19-21 (untitled.Rmd) 
Error in eval(expr, envir, enclos) : path=c:/wrong/path
Calls: <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval -> eval

Execution halted

As long as you put this in the file ahead of the actual error that is killing your compile, you'll see it.

Edited to add: you can also use warning() or message() (as @sindri_baldur mentioned) for this, but for those you need to change the chunk default to warning=FALSE or message=FALSE respectively so the message goes to stderr and shows up in the console.

user2554330
  • 37,248
  • 4
  • 43
  • 90