简体   繁体   中英

Debugging an RMarkdown file from the command line

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM