Any way of attaching .csl, .bib files by using `knitcitations` package? - r

I have tried to attach bibliography.bib, nature.csl files by using knitcitations packages on myPkg_vignette.Rmd, but when I execute the .Rmd file, I got following error:
pandoc-citeproc.exe: Could not find bibliography.bib
pandoc.exe: Error running filter pandoc-citeproc
Filter returned error status 1
Error: pandoc document conversion failed with error
I looked into knitcitations manual and how to attach .csl, .bib file by handy, but I got warning and can't attach these files on myPkg_vignette.Rmd.
Edit :
This is context of bibliography.bib:
#article{Vahid_Jalili_Musera_2015,
title = "MuSERA: Multiple Sample Enriched Region Assessment",
author = {Vahid Jalili, Matteo Matteucci, Marco Masseroli, Marco J. Morelli},
journal = "Briefings in Bioinformatics",
year = "2016",
pages = "1-15",
url = {http://bib.oxfordjournals.org/content/early/2016/03/23/bib.bbw029.abstract?keytype=ref&ijkey=8IlROGziM9XA7NS},
doi = "10.1093/bib/bbw029 ",
}
How can I attach bibliography.bib, nature.csl on package's vignette easily ? I read some post in SO and I confused about the given solution. I am quite new with using knitcitations packages, and I may get wrong with attaching .bib, .csl files in right way. Can any one help me out how to address this issues in Rstudio ? Any idea ? Thanks a lot :)
NEW EDIT :
I've read this post and followed the solution :
setwd('C:/Users/me/Documents/myPkg')
Sys.setenv(TEXINPUTS=getwd(),
BIBINPUTS=getwd(),
BSTINPUTS=getwd())
but bibliography.bib is not printed in .Rmd file. I changed the global option for weaving Rnw file to knitr. How can I print out bibliography on .Rmd file ? Any further help please ?

As with Dirk, it just works for me. Maybe you didn't put the YAML together properly, or maybe you never cited anything. Here's a sample document that works with your bib file:
---
title: "Biblio Example"
output: html_document
bibliography: bibliography.bib
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Here's Some Text
This text cites the paper: [#Vahid_Jalili_Musera_2015].
## References
The bibliography will appear by magic at the end of the document, just
below this text.
See http://rmarkdown.rstudio.com/authoring_bibliographies_and_citations.html for more details.

For me the problem was that my .bib file was listed in .Rbuildignore. Removing the entry solved the problem.
According to the r-pkg-dev mailing list answer from Duncan Murdoch, .Rbuildignore is "case-insensitively against the file and directory names relative to the top-level package source directory".
Therefore the entry xxx.bib ignored all .bib files recursively in my package directory and the error was thrown.

Related

Knitting .Rmd file in Rstudio hangs when using a custom .csl file for bibliography

I am writing a manuscript using R Markdown and am trying to knit the document with a custom csl that I got from the Zotero Style Repository (for the journal Environmental Entomology). When I try to knit, the R Markdown pane shows that it progresses through all the code chunks adequately, then it gets to the point where it says "output file: Mytitle.knit.md" but it hangs there and never finishes. If I take out the custom csl portion of the YAML header, the document knits no problem. It produces the .md file and I tried opening that and hitting the "Preview" button, but that hangs as well.
I've tried a few things to reproduce the problem but I can't nail down what's causing it. I went into another manuscript I wrote using the same custom csl. That document knit with the custom csl fine. I tried creating a new document with just the template text and the custom csl, that worked fine. I thought it might have something to do with my .bib file, so I copied the .bib file from the current manuscript to a different one using the same .csl and that knit fine.
Here is what my YAML looks like:
title: ""
output:
bookdown::word_document2:
reference_docx: "template.docx"
bibliography: references.bib
csl: "environmental_entomology.csl"
link-citations: true
I'm using R v4.0.3 and RStudio 1.4.999 on a Windows 10 Home machine.
I found out the issue was related to a single bad entry in the .bib file. I'm not sure what about it caused the hang, but once I removed it the file knit fine.
This is the culprit:
#book{lowtemp2009,
title = {Low Temperature Biology of Insects},
editor = {{Denlinger}, {David L.} and {Lee}, {Richard E. Jr}},
year = {2009},
date = {2009},
publisher = {Cambridge University Press},
doi = {10.1017/cbo9780511675997},
url = {http://dx.doi.org/10.1017/CBO9780511675997}
}
I'm not super familiar with bibtex entries so I'm not sure what caused the problem. Changing it to the below entry caused the file to knit successfully.
#book{denlinger2010low,
title = {Low temperature biology of insects},
author = {Denlinger, David L and Lee Jr, Richard E},
year = {2010},
publisher = {Cambridge University Press}
}

Package build fails because vignette does not find child Rmd files

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.

Using pandoc-crossref with R bookdown

Is it possible to use pandoc-crossref in bookdown?
I tried to change the yaml header to:
output:
bookdown::tufte_book2:
toc: yes
highlight: tango
pandoc_args: -F /usr/local/bin/pandoc-crossref
Which should pass the filter to pandoc, but I get the error:
pandoc: Error running filter pandoc-crossref:
Could not find executable ' pandoc-crossref'.
The above error does not make sense, as I entered the correct path. What kind of env is bookdown using, which is precluding the access to the filter file?
Here is an example
---
output: bookdown::html_document2
---
# Section name {#id}
```{r pressure, echo=FALSE, fig.cap='test plot'}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. To cross-reference the figure, use `\#ref(fig:pressure)` to produce Figure \#ref(fig:pressure). All this is found within the section \#ref(id).
Which produces...
See https://bookdown.org/yihui/bookdown/figures.html for the official documentation.
I had a similar issue while I was trying to put numbers to equations in Word files (SO question). I got the same error Could not find executable 'pandoc-crossref'.
My RStudio installation (on Windows) did not come with pandoc-crossref. Here is what I did:
Downloaded pandoc-crossref from here.
Find the path where RStudio saved the file pandoc.exe:
rmarkdown::find_pandoc()
Put the pandoc-crossref.exe in the folder I got in (2).

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

Rstudio pdf knit fails with "Environment Shaded undefined" error

When trying to knit a PDF using a template from package rticles output: rticles::acm_article I get the following error:
! LaTeX Error: Environment Shaded undefined.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.76 \begin{Shaded}
This appears to only happen when I include code chunks within the output document.
Reproducible example:
You will need to start a new R Markdown document using the New Document -> From Template -> Association for Computing Machinery. Here is the R Markdown file:
---
title: Short Paper
author:
- name: I Am Me
email: me#email.com
affiliation: Fictional University
abstract: |
This is the abstract.
It consists of two paragraphs.
output:
rticles::acm_article:
keep_tex: true
---
## Simple test
Code chuck follows:
```{r}
plot(rnorm(10))
```
This above example, however, works if I set echo=FALSE in the header. You won't get code in the output, but for an academic paper you probably don't need it anyway, an if you do you can display it in a different manner.
Notes:
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit),
running Ubuntu 16:10
I made sure I have the texlive-latex-base, texlive-latex-recommended, and texlive-latex-extra package installed, but still no luck
I also tried generating a pdf from the intermediate .tex file, as suggested here, but I get the same error.
I considered this approach, but it didn't work, I still got an error (maybe I need to tweak to work in my context, but not sure how)
So, the issue here is the rticles templates sometimes omit a means for pandoc to inject code highlighting environments, e.g. Shaded. To get around this, you would have to insert into the template.tex preamble:
$if(highlighting-macros)$
$highlighting-macros$
$endif$
I struggled with fixing the error and actually raised another question which I was able to answer after quite some experimentation and searching for other related questions/answers.
In short the solution presented above by coatless works perfect given you know the location of the template.tex file. Obviously, I played around with the wrong files before I came across a related question on rticles and how to embed (missing) LATEX.
The longer answer can be found in my "another question". For reference here are the key steps:
locate the template.tex file in your R package rticles library. If you do not know where you have your package library use .libPaths() in your RStudio console. Then work your way to the resources subfolder of your rticle template. In my case:
R-3.5.0/library/rticles/rmarkdown/templates/ieee_article/resources.
add the fix proposed by coatless above in the preamble of the template.tex. The preamble is anything before the line \begin{document}:
$if(highlighting-macros)$
$highlighting-macros$
$endif$
save template.tex and go back to R/RStudio and hit the knit button. R code chunks are now nicely displayed, if you do not suppress their printing with echo = FALSE.
I know the post is old, but I'll put this here for reference for others who have the same problem.
The reason is that in the latex template the environment Shaded is trying to be redefined. However the environment only exists if you are using R chunks in your pdf. So if you don't have any r chunks in the pdf, it tries to redefine something which was never defined - causing the error.
The solution is to modify the \renewenvironment{Shaded} command in the latex template.
https://github.com/yihui/bookdown-chinese/commit/a3e392593b464ba31a7eceb0cd60f7e0bd112798
I found a work around. If I hide the code with chunk option echo = FALSE, a PDF is generated with no issues.
Reproducible example 1:
title: Short Paper
author:
- name: I Am Me
email: me#email.com
affiliation: Fictional University
abstract: |
This is the abstract.
It consists of two paragraphs.
bibliography: sigproc.bib
output:
rticles::acm_article:
keep_tex: true
---
## Simple test
Code chuck follows:
```{r}
plot(rnorm(10))
```
Knit to PDF fails with ! LaTeX Error: Environment Shaded undefined.
Example 2:
---
title: Short Paper
author:
- name: I Am Me
email: me#email.com
affiliation: Fictional University
abstract: |
This is the abstract.
It consists of two paragraphs.
bibliography: sigproc.bib
output:
rticles::acm_article:
keep_tex: true
---
## Simple test
Code chuck follows:
```{r echo=FALSE}
plot(rnorm(10))
```
Knit to PDF works!.
The only difference between the two examples is adding echo=FALSE to the code chunk header. You won't get code in the output, but for an academic paper you probably don't need it anyway, an if you do you can display it in a different manner.
The rticles templates typically do not directly allow shading of code. As a result,
the required $highlighting-macros$ which is used to control the shading is not included.
If for any reason, you cannot (or do not) want to directly edit the template.tex file as suggested by #coatless, you can alternatively consider include the code directly in the acm_proc_article-sp.cls file:
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
You may also wish to see my answer to a similar question, whereby the same code is put into a header.tex file in the same directory as your project.

Resources