Package build fails because vignette does not find child Rmd files - r

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

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.

Related

R markdown beamer presentation returning undefined control sequence

Whenever I tried to knit R markdown in beamer presentation format, it keeps on giving me undefined control sequence.
It is fraustrating because it worked fine until yesterday and I have no idea what I changed that causes this error.
To show that my code is not the source of the problem, I simply tried to knit an empty page.
code:
---
title: "untitled"
output: beamer_presentation
---
Output:
output file: a.knit.md
! Undefined control sequence.
<argument> ...{subsubsection}\addtobeamertemplate
{theorem begin}{\expandaft...
l.322 }{}
My original file returns the exaclty same error message.
I tried:
reinstall LaTeX
reinstall RStudio
upgrade all packages in R
Any idea how to fix this?
This was just fixed by #UlrikeFischer. #JosephWright is preparing a new beamer release as we speak, it should be available as beamer v3.67 on ctan tomorrow.
As a temporary workaround, place these two files in the folder of your .rmd file:
https://raw.githubusercontent.com/josephwright/beamer/main/base/beamer.cls
https://raw.githubusercontent.com/josephwright/beamer/main/base/beamerbasesection.sty
(don't forget to delete these files again once you have the new beamer version, otherwise they might interfere with future updates)

Adding a new bibliography style with R Markdown and TinyTex

I'm writing a paper using R Markdown and TinyTex, using Biblatex for referencing. It works fine with default referencing styles, but I need to add a custom bibliography and citation style for the journal I'm writing for.
I need to follow the Unified Stylesheet for Linguistics, for which there is a Biblatex implementation available on Github here, containing a .bbx and .cbx file.
I've tried adding those .bbx and .cbx files to my local copy of TinyTex, inside Library/TinyTex/texmf-local/tex/latex/biblatex. My YAML header includes:
output:
pdf_document:
citation_package: biblatex
biblatexoptions: [bibstyle=biblatex-sp-unified, citestyle=sp-authoryear-comp]
When I knit the document, I get the following error:
tlmgr search --file --global '/biblatex-dm.cfg'
! Package keyval Error: bibstyle undefined.
I don't have a biblatex-dm.cfg file (nor do I really understand what that would be). I would have thought the .bbx and .cbx files would be sufficient, based on the regular installation instructions in the style's Github repo.
Where should I put .bbx and .cbx files, so that tlmgr can find them? And/or what additional steps do I need to take to use this style with my paper?
====================================================================
UPDATE: The problem seems to be coming from the Pandoc LaTeX template that R Markdown uses.
Setting aside R Markdown, I created a smaller minimal LaTeX example:
main.tex
references.bib
Where main.tex is:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[bibstyle=biblatex-sp-unified,citestyle=sp-authoryear-comp]{biblatex}
\addbibresource{references.bib}
\begin{document}
Something something \citep{darwin_origin_1859}.
\printbibliography
\end{document}
And references.bib is:
#book{darwin_origin_1859,
location = {London},
title = {On the Origin of Species by Means of Natural Selection},
publisher = {J. Murray},
author = {Darwin, Charles},
date = {1859}
}
I had success compiling this example using the sequence of commands pdflatex, biber, pdflatex, pdflatex. Thus it seems my local TeX installation knows about the biblatex-sp-unified.bbx and sp-authoryear-comp.cbx files I added and can use them just fine.
Subsequently, I created an equivalent minimal R Markdown document with the YAML header:
title: "Untitled"
output:
pdf_document:
citation_package: biblatex
bibliography: references.bib
biblatexoptions: [bibstyle=biblatex-sp-unified, citestyle=sp-authoryear-comp]
and body:
Something something [#darwin_origin_1859].
This time, I got the same old error message from before:
tlmgr search --file --global '/biblatex-dm.cfg'
! Package keyval Error: bibstyle undefined.
This would seem to suggest that the problem is caused by something in Pandoc's LaTeX template, but I don't know what.
Just to confirm that it's definitely the Pandoc template and not my own installation/setup, I took the .tex file that gets produced when I knit the minimal R Markdown example above, and tried to compile it in Overleaf (with biblatex-sp-unified.bbx and sp-authoryear-comp.cbx files added). I reproduced the same error.
Although I think I've localised the problem, I'd still very much like to understand what and where the problem is in the Pandoc template. I'd also be keen to hear if anyone has any fixes (other than just using a different template or writing my own).
UPDATE: This seems to be an issue with using an out-of-date version of R Markdown and/or Pandoc.
I was using rmarkdown package v.1. At time of writing, the most up-to-date version is 2.1.
I updated all my packages and updated Rstudio (which currently ships with Pandoc v2.3.1) and no longer experience problems. I also upgraded R (from 3.5.something to 3.6.2) and did a fresh re-install of tinytex while I was at it, but I'm not sure whether those things had an effect for this particular problem.
Now, when I put biblatexoptions: [bibstyle=biblatex-sp-unified, citestyle=sp-authoryear-comp] in my YAML header, it's correctly converted into the LaTeX command \usepackage[bibstyle=biblatex-sp-unified,citestyle=sp-authoryear-comp]{biblatex}, rather than the \ExecuteBibliographyOptions command as described below.
Ralf Stubner initially suggested I check my R Markdown/Pandoc versions in the comments. Please give his comments an upvote if you them useful as well.
Problem recap:
I'm writing a document in R Markdown and I have a particular referencing style that I'd like to use with biblatex. I have a .bbx and .cbx file defining the style, available on Github (linked above). The problem is that the document fails to compile, saying biblio/citation styles are undefined (even when the style files are in the project folder itself).
I've found that the problem was caused by the way I was passing options to biblatex. In my YAML Header, the line:
biblatexoptions: [bibstyle=biblatex-sp-unified, citestyle=sp-authoryear-comp]
gets converted to the latex command:
\ExecuteBibliographyOptions{bibstyle=biblatex-sp-unified,citestyle=sp-authoryear-comp}
I'm not sure why, but when this command is included, it produces the errors I was observing.
Installing new Biblatex style:
I'm finding that TeX doesn't know about the .bbx and .cbx files when they're in my ~/Library/TinyTex/texmf-local/tex/latex/biblatex directory (which is where I expected to put them based on the Github installation instructions).
To get the referencing style recognised by the system, I placed .bbx and .cbx files inside ~/Library/TinyTex/texmf-dist/tex/latex/biblatex/bbx and ~/Library/TinyTex/texmf-dist/tex/latex/biblatex/cbx respectively. Then, in the terminal, I ran sudo mktexlsr.
(Alternatively, for use only with a particular document, the .bbx and .cbx files could simply be kept in the project directory with the R Markdown file)
Original hacky answer (but see update above):
Instead of using biblatexoptions in the YAML header of the R Markdown document, I simply knitted it with citation_package: biblatex (and no extra options). I also added keep_tex: yes. Then, I opened the resulting tex file, found the \usepackage{bibtex} command and added the desired options, so it read \usepackage[bibstyle=biblatex-sp-unified,citestyle=sp-authoryear-comp]{biblatex}.
Finally, I ran pdflatex and biber on the tex file in the terminal. Clearly far from ideal, but it will technically produce the desired output.

Error in browseVignettes: no vignettes found

I'm trying to see the vignette that I've created after rebuilding a package on my local computer. Note that this has nothing to do with suggestions on this post, since I'm not cloning from Github.
The vignettes portion of my vignette file looks like this:
vignette: >
%\VignetteIndexEntry{pack_name}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
The process is:
Ctr + Shift + K to knit the vignette
Ctr + Shift + B to rebuild the package
Enter browseVignettes("package name")
Get error:
No vignettes found by browseVignettes
I also tried changing knitr::markdown to knitr::knitr, but it didn't help.
Entire vignette top-portion:
---
title: "Random title"
author: "Author"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{vignette_name}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
You don't give all the details to say what went wrong, but here is what you need to do:
Vignettes need to be in the package vignettes directory.
If they are anything but Sweave vignettes (and yours is knitr::rmarkdown, according to the header lines you showed us), then you need to tell R about it in the DESCRIPTION file. For your example, you'd need
VignetteBuilder: knitr
in that file, and also have knitr listed in the Suggests, Depends, or Imports lines.
You need to follow the naming convention for your vignette engine. For knitr::rmarkdown, name the file something.Rmd.
You don't need to knit the file yourself. R will do that when it builds the source package. (You can configure RStudio to do this for "Install and Restart"; by default it doesn't.) Even if you haven't knitted the vignette, browseVignettes should find it if you've followed the other steps properly, it just won't display the HTML or PDF output, only the source and extracted R code.
Edited to add: When building and reinstalling the package, it appears that sometimes vignettes aren't included unless the install is done from a .tar.gz file. Use the "Build Source Package" option in the RStudio build pane to get one of those.

R check warning: Files in the 'vignettes' directory but no files in 'inst/doc'

Lately I get a warning for my vignette on Win R Development Version when doing a cran check.
Files in the 'vignettes' directory but no files in 'inst/doc'
This warning only appears with the Win Dev version.
For Mac, AppVeyor and Travis no warning appears.
The problem is, I don't know what the warning wants to tell me.
As far as I know I do not have to put files in inst/doc.
Here is the complete warning message:
Files in the 'vignettes' directory but no files in 'inst/doc':
'Figures.d/Rlogo.png', 'Figures.d/distribution.pdf',
'Figures.d/distributionbar.pdf', 'Figures.d/gapsize.pdf',
'Figures.d/imputations.pdf', 'Figures.d/imputations2.pdf',
'Figures.d/sponsorlogo.jpg', 'Figures.d/statsna.png',
'Figures.d/tsairgap.png', 'Introduction.pdf', 'Introduction.tex',
'RJournal.sty'
In my case, I was getting this issue because I had specified my YAML as per #Claudia's answer, but I had specified rmarkdown as the VignetteBuilder in the DESCRIPTION file. Using VignetteBuilder: knitr in my DESCRIPTION file fixed the problem.
I had a similar issue with an Rmd vignette. I fixed it by changing the YAML header of the vignette to something like this:
---
author: "Name Surname"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteEngine{knitr::knitr}
%\VignetteIndexEntry{Title of your vignette}
%\usepackage[UTF-8]{inputenc}
---
I fixed the problem with adding the .pdf output of my Vignette to inst/doc
Although I am not sure, if this is the supposed solution, it made the warning disappear.
I was getting the error on more than just the Win R Development version, but for me the fix was adding markdown and knitr in the Suggests field in the DESCRIPTION file. Then, I guess it was clear to the package compiler that the vignette was designated as a markdown/HTML doc.
These lines are in my markdown YAML header:
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Example for estimating the case fatality ratio}
%\VignetteEngine{knitr::knitr}
\usepackage[utf8]{inputenc}
During the compiling of your R package, those files are generated and cause your issue.
You should install the package gitignore, then in the main folder you have to create a text file named ".gitignore" (no extension) and add the address of the files you want to ignore. For example, vignettes/*.pdf

knit HTML does not save html in vignettes/

So I have a vignette, vignettes/test-vignette3.Rmd:
---
title: "Sample Document"
output:
html_document:
highlight: kate
theme: spacelab
toc: yes
pdf_document:
toc: yes
---
Header
=========
When I hit the knit HTML button, I get the following:
processing file: test-vignette3.Rmd
output file: test-vignette3.knit.md
Output created: /tmp/RtmpKVpegL/preview-5ef42271c0d5.dir/test-vignette3.html
However, if I copy this file to inst/doc and hit the knit HTML button, I get:
processing file: test-vignette3.Rmd
output file: test-vignette3.knit.md
Output created: test-vignette3.html
My questions are:
How do I get RStudio to save the output from knit HTML on vignettes/test-vignette3.Rmw to the vignettes directory?
How do I get RStudio to not delete test-vignette3.knit.md during the knit HTML procedure? (I'd like to have the .md so people can read it on my github repo.)
I'm running RStudio version 0.98.836, rmarkdown version 0.1.98 and knitr version 1.5.
Actually you should not keep the .html output under vignettes/, because the vignette output is supposed to be generated by R CMD build. R may fail to recompile your vignettes if the HTML output files have already been there when you build the source package, which means you are likely to see old (and possibly wrong) results because the HTML file was not generated from the latest version of the .Rmd file. Therefore RStudio intentionally avoid writing the HTML files in the vignetttes directory.
If you choose to ignore the warning above, you can certainly run rmarkdown::render('your-vignette.Rmd') in the R console.
For the second question, I do not recommend you to do that, either, because Github renders the markdown to HTML differently (compared to the Pandoc conversion done through the rmarkdown package). Normally the package vignettes are shown on CRAN, see, for example, the knitr page on CRAN. However, because the rmarkdown package is not on CRAN yet, you cannot use the vignette engine knitr::rmarkdown at the moment (I guess we are not too far away from the CRAN release now). You can consider pushing the HTML files to Github pages, though.

Resources