Skip to Content

lobstr segfault leads me to develop a way to debug `rmarkdown::render()`

1 cause

My copy of adv-r seems to have some problem in chapter cross-reference, so I decide to build the source code to figure out the cause.

Then the build failed 1.

2 Development

Naturally, I want to find where the error occur, but the unnamed-chunk-*** in rmarkdown::render() console output is so annoying! Who can ever know which chunk is the ***st chunk ?!

As I expected, knitr doesn’t provide the API to get nth chunk (I should have tried this several times).

3 Climax

Then I start to think about set something in knitr, so that every chunk’s code would be printed in the console output. I read the knitr book, tried many times, and finally find a way 2 when I am going to give up:

```{r}
old_chunk_hook <- knitr::knit_hooks$get('chunk')
new_chunk_hook <- function(x, options) {
    writeLines(options$code)
    old_chunk_hook(x, options)
}
knitr::knit_hooks$set(chunk = new_chunk_hook)
knitr::opts_chunk$set(mutable_var = Sys.time()) # disable cache
```

Now I can search the code causing error, which seems really wired: calling lobstr::obj_size() with function (such as mean) would cause segfault. 3

4 Epilogue

Finally, eval = F that chunk, build the book, and find the reason: the cross-reference failure seems to result from split_by: section.

5 Afterword

Recall that I happily discard microbenchmark after I find bench is awesome to use in the mooning, now I should say it’s so fortunate that we have pryr in hand when lobstr fails oddly.


  1. A very serious error, corrupt R session and force you exit.↩︎

  2. get up-to-data version here ↩︎

  3. As usual, I run it yestreday!↩︎