I'm just here to share an experience that took some head scratching to fix and on googling it, it appears that there is sufficient occurrence of it it to warrant a quick topic discussion.
The goal is to use
library(rmarkdown) to render a PDF.
The problem I encountered is that I was able to knit together the PDF manually, but when I tried to do it via an R script (
render()) I was met with:
Error in xml_children(x)[[search]] : subscript out of bounds.
I was able to trace this back to a
kableExtra::row_spec() call but upon removing that, I was then met with
error: Functions that produce HTML output found in document targeting latex output..
One of the more interesting findings, however, was that this only occurred on a development branch, and only when I sourced the development branch first. That's to say, if when I opened R, and sourced the production branch and then switched to the development branch, everything would run fine (including rendering the PDF in question). This would persist even if I then soured all of the new scripts from the development branch.
The converse was not true, however. If I initially sourced the development branch, and then switched to my production branch, things were broken. Obviously concerning.
Some searching led me to Book will not compile to PDF · Issue #296 · hadley/r4ds · GitHub which discussed a
webshot package - but since I didn't use this package I didn't think anything of it. Until -
On a hunch, I did two
sessionInfo()'s, one from when I initialized the program from the production branch first, and one from when I initialized the development branch first. Strangely, two packages were present in the development branch that were not present in the production branch
I set out then to manually source the new scripts and found the offending script that was bringing in these two packages. It turns out, that I had explicitly called another
library(kableExtra) in one of the new scripts, and as such, it was bringing in these two new packages. This is in spite of the fact that
kableExtra is already called in the initiation scripts I have written that bring in all required packages (although I am now moving towards explicit package calling).
Removing the additional
library(kableExtra) both stopped the
webshot packages from being loaded and also removed all errors with knitting the PDF from the script.
That's all I got for you guys.