quesadagranja
quesadagranja

Reputation: 171

Is it possible to debug an R Markdown chunk using "print()" to see certain variables on console?

In R Markdown I'm getting the following error:

  |......................................................................| 100%
label: heatmap_placement (with options)
List of 2
 $ echo   : logi TRUE
 $ results: chr "asis"

Quitting from lines 176-230 (summary_report_v03.Rmd)
Error in readChar(con, 5L, useBytes = TRUE) : cannot open the connection
Calls: hmp2rep ... withCallingHandlers -> withVisible -> eval -> eval -> load -> readChar

It looks like I'm getting this error because it cannot open some file inside a chunk of R code (it's a large piece of code with some nested for-loops).

To debug I use to place some "print()" functions to check some variables and see what's wrong on screen. But, since the bug is inside a chunk, I can't see anything.

Is it possible to debug Rmd chunks using "print()" functions to see what's happening inside? Otherwise, what can I do to guess where the bug is?

Upvotes: 1

Views: 831

Answers (1)

user2554330
user2554330

Reputation: 44887

You can do it with message() if you set the chunk option not to include messages in the document. For example,

```{r message=FALSE}
x <- runif(1)
message(x)
```

Another possibility is to set an unused chunk option to a string, e.g.

```{r chunkmsg=paste("x = ", x)}
y <- 1
```

Using both of these prints this to the console:

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

  |...................................                                   |  50%
label: unnamed-chunk-1 (with options) 


processing file: Untitled.Rmd
List of 1
 $ message: logi FALSE

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

  |......................................................................| 100%
label: unnamed-chunk-2 (with options) 
List of 1
 $ chunkmsg: chr "x =  0.287577520124614"


0.287577520124614
output file: Untitled.knit.md

/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS Untitled.knit.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output Untitled.html --lua-filter /Library/Frameworks/R.framework/Versions/4.1/Resources/library/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /Library/Frameworks/R.framework/Versions/4.1/Resources/library/rmarkdown/rmarkdown/lua/latex-div.lua --self-contained --variable bs3=TRUE --standalone --section-divs --template /Library/Frameworks/R.framework/Versions/4.1/Resources/library/rmarkdown/rmd/h/default.html --no-highlight --variable highlightjs=1 --variable theme=bootstrap --include-in-header /var/folders/d6/s97fjjxd3_9353x_lwb692100000gn/T//Rtmp4tTiD4/rmarkdown-str44473d4f8885.html --mathjax --variable 'mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' 

Output created: Untitled.html

Notice that the chunkmsg string appears in the list of chunks, while the message() string appears after all of them. In some cases the message() string will appear earlier; I think it depends on what is in the following chunks.

Upvotes: 1

Related Questions