4

I have modularized my vignette Rmd file using child chunks to be able to reuse the child Rmd files in other Rmd documents.

The package build fails (in RStudio and with R CMD build .) with this error message:

** installing vignettes
   ‘Vignette.Rmd’ using ‘UTF-8’ 
Warning in readLines(if (is.character(input2)) { :
  cannot open file 'child_doc.Rmd': No such file or directory
Quitting from lines 10-11 (child_doc.Rmd) 
Error in readLines(if (is.character(input2)) { : 
  cannot open the connection
ERROR: installing vignettes failed

How can I build my package (make R find my child Rmd files)?

Example Rmd files:

Vignette.Rmd

---
title: "title"
author: "me"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{title}
  %\VignetteEngine{rmarkdown::render}
  %\VignetteEncoding{UTF-8}

main doc

```{r child = "child_doc.Rmd"}
```

child_doc.Rmd:

# This is from the child doc
lorem ipsum

Update 1:

https://stackoverflow.com/a/49463061/4468078 indicates that RStudio builds the vignettes with the package folder as root (which could explain why the files are not found).

Update 2:

If have created a minimal reproducible example package together with a summary of the findings at github:

https://github.com/aryoda/R_pkg_knitr_child_vignette_issue

Update 3:

I have opened an issue at knitr (https://github.com/yihui/knitr/issues/1540) but @user2554330 has identified the tools namespace as one reason of problems...

Update 4:

See the bugzilla bug entry opened by Duncan Murdoch: https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=17416

R Yoda
  • 8,358
  • 2
  • 50
  • 87

1 Answers1

1

This looks like a bug (or maybe more than one). I'd probably call it a bug in R, but it might be a bug in knitr. When you build the tarball, R copies the main file into inst/doc, but not the child file. knitr then looks at it and since it doesn't see the child, it quits.

To get the package to build, you just need an empty file in inst/doc with the same name as the child file. But this isn't enough to pass checks.

When checking the package, R will see that child file sitting in inst/doc, and get upset because it's not a proper vignette. So you need to fool R into thinking it is one.

As far as I can see, there's an easy (though ugly) workaround. Just put a file named child_doc.Rmd into the inst/doc directory. To make R think it is a vignette, copy the lines

  %\VignetteIndexEntry{title}
  %\VignetteEngine{rmarkdown::render}
  %\VignetteEncoding{UTF-8}

from the main file. Otherwise, the content appears to be irrelevant, so I wouldn't put anything else there.

Put the real child_doc.Rmd file into the vignettes directory. I think if you do this, your package will build and check without errors.

This is probably worth a bug report, but I'm not sure what the fix should be. Maybe knitr should be more tolerant in its check, or maybe R should copy the file sooner.

Too bad the workaround is so ugly, and will probably cause other problems once the bug is fixed.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Great analysis! I would classify this as bug too. What happens if you (without applying your described workarounds) call `devtools::build_vignettes()`? After that I do not get any error or warning anymore during build or check, but `inst/doc` does still **not** contain the child Rmd file. – R Yoda Apr 29 '18 at 06:19
  • That passes the build but fails the check for me. – user2554330 Apr 29 '18 at 07:57
  • I am using R3.4.4 too (on Linux), I will try to build using Windows and another R version tomorrow. Thanks for your help, I am debugging into the code now and try to identify R or knitr as the source of the problems to be able to open an issue. Currently it looks like `tools::buildVignettes` is not the problem if called with the right args (R CMD build + check call this function according the help). I will accept your answer since you have provided a work-around. THX! – R Yoda Apr 29 '18 at 09:00
  • The problem seems to be in `tools::.install_package_vignettes2`, which calls `tools::pkgVignettes` on the `doc` directory without copying all the files. I'll submit a bug report. – user2554330 Apr 29 '18 at 15:38
  • Wow, deep diving in the code :-) Good to hear that. I have already opened an issue at github for knitr, perhaps you could link your ticket to enable a big picture: https://github.com/yihui/knitr/issues/1540 – R Yoda Apr 29 '18 at 17:41
  • Duncan Murdoch has already filed a bug report for R, see: https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=17416 – R Yoda Apr 30 '18 at 22:56