Using accented letters in R package documentation using bibtex and roxygen2 - r

I am developing an R package in RStudio (R version 3.6.1; RStudio version 1.2.1335) using roxygen2 (version 6.1.1) and am using the \insertCite{} command together with a bibtex file in order to cite references in the documentation for individual functions. I am following the instructions Inserting references in Rd and roxygen2 documentation. Everything works fine, except when I try to include a reference with accented characters. So my REFERENCES.bib file contains the following entry:
#ARTICLE{Cabcdef15,
author={John {\c C}abcdef},
title={A title},
journal={Journal of Applied Stuff},
year={2015},
volume={81},
number={1},
pages={100--200},
}
The {\c C} is the LaTeX command for a C-cedilla (Ç). (I also tried \c{C} and pasting Ç directly and neither resolved the issue.)
I cited this reference in the roxygen2 preamble for my R function myfunction using \insertCite{Cabcdef15}{mypackage}. However, in the documentation output (after running devtools::document() and devtools::build(), installing the package and running library(mypackage) and ?myfunction) the citation appears in my browser (Google Chrome) as (Çabcdef 2015) rather than as (Çabcdef 2015).
Presumably this is an encoding issue. However, from what I read in the aforementioned instructions (under 4.4 Encoding of file REFERENCES.bib) this should be working, provided that I have the line Encoding: UTF-8 in the DESCRIPTION file for my R package, which I do. Hence I am stumped.

I have a strong suspicion you are using a Microsoft operating system.
I have code in a roxygen2 examples block which outputs accented French characters: works fine with non-French locales on MacOS and Linux: Windows makes a mess of it. I have UTF-8 in package DESCRIPTION. For me, the obvious work-around is not to use Windows for documenting the package. UTF-8 everywhere seems to work well for me, except on Windows. The R documentation links are helpful, and, in a related post, the mighty Yihui Xie writes about this issue.
This WONTFIX R issue also hints at the root cause: Windows.
A more palatable and Windows-compatible workaround is discussed in platform specific sections in Writing R Extensions.

Related

Use R/exams exams2pdf() to produce a PDF document

I am new to the R/exams package and I try to produce a pdf document from one of the templates provided by the developers. (http://www.R-exams.org/templates/confint3/)
I am able to compile a Rnw file into a HTML document using the commands
library("exams")
exams2html("confint3.Rnw")
When calling the function
exams2pdf("confint3.Rnw")
it gives the error message
! LaTeX Error: File `Sweave.sty' not found.
I have Latex installed and there are no problems using it in general. I do not understand:
Do I need to tell exams2pdf() the location of the latex installation?
Do I need to define a template (as plain.tex) first? How should it look like?
What is it that I do not understand?
I looked at the documentation of the exams package, I also tried exams2pdf() after installing and calling library("tinytex").
Any help where to look at or what to do is highly appreciated. Thank you!
Minimal example:
install.packages("exams")
install.packages("tth")
library("exams")
set.seed(1090)
exams2html("confint3.Rnw")
set.seed(1090)
exams2pdf("confint3.Rnw")
It is hard to diagnose what exactly goes wrong with the information provided. In any case, when running pdfLaTeX either through utils::texi2dvi() (the default when the R package tinytex is not installed) or through tinytex::latexmk() (the default when the R package tinytex is installed) does not find the Sweave.sty file provided by the R base system. What is not clear to me which LaTeX engine is running in the background: MikTeX on Windows?
There are several strategies that could resolve this issue:
Tell your LaTeX installation about the texmf directory provided by the R base system so that it is found no matter where on your system you call pdfLaTeX.
Use a different LaTeX installation, e.g., by installing TinyTeX (the LaTeX distribution) through tinytex (the R package): tinytex::install_tinytex(). This might be particularly attractive if you are not actually a LaTeX user and just need it to compile PDF exams.
Avoid using the Sweave.sty file in a custom template file, say myplain.tex. A suggestion for such a file is included at the end of this post.
Further details are discussed in this thread: https://tex.stackexchange.com/questions/153193/latex-error-sweave-sty-not-found
As for your three questions:
As already explained above: exams2pdf() leverages either utils::texi2dvi() or tinytex::latexmk(). So these need to know about the LaTeX installation - but this seems to be the case. They just don't find the texmf provided by base R.
You should not have to do this but it is certainly an option that you can use. As a starting point, run exams_skeleton(markup = "latex", writer = "exams2pdf"). Among other things this creates a templates folder where you could put the myplain.tex template below.
As I said above, it's hard to answer that with the information provided. Hopefully, one of the clues provided here gets you a couple of steps forward.
Content of myplain.tex:
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{a4wide,graphicx,color,verbatim,url,fancyvrb,ae,amsmath,amssymb,booktabs,longtable,eurosym}
\newenvironment{question}{\item \textbf{Problem}\newline}{}
\newenvironment{solution}{\textbf{Solution}\newline}{}
\newenvironment{answerlist}{\renewcommand{\labelenumi}{(\alph{enumi})}\begin{enumerate}}{\end{enumerate}}
\providecommand{\tightlist}{\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setkeys{Gin}{keepaspectratio}
\DefineVerbatimEnvironment{Sinput}{Verbatim}{fontshape=sl}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{}
\DefineVerbatimEnvironment{Scode}{Verbatim}{fontshape=sl}
\newenvironment{Schunk}{}{}
\begin{document}
\begin{enumerate}
%% \exinput{exercises}
\end{enumerate}
\end{document}

R bookdown: list of abbreviations with LaTeX nomenclature package

Generating an index in R bookdown works perfectly well.
Adding a list of abbreviations with the nomenclature package is more difficult:
I added to preamble.tex
\usepackage{nomenclature}
\makenomenclature
And then I include a file calling the \printnomenclature macro via the after_body include option in the yaml header.
Actually, I already have a pretty good idea, what the problem is:
Usually, one calls makeindex when generating the nomenclature list with LaTeX. This magically works for the index, but for the nomenclature package, there seems to be a need for some manual work.
Since RStudio does not know about special makeindex calls, you could install a tool like latexmk and configure RStudio to use that by putting Sys.setenv(RSTUDIO_PDFLATEX = "latexmk") somewhere in your main Rmd file, c.f.
Is there a way to add chapter bibliographies using bookdown?
[Not tested since you do not provide a minimal example.]

Lost references with “Compile PDF” for a Sweave vignette in RStudio

“Compile PDF” in RStudio 0.99.902 with R 3.3.0 cannot find references included in the “sos” Sweave vignette that's been part of the “sos” package on CRAN since 2009. It produces “sos.pdf”, which reads mostly OK, except that the inline references are replace by something like “?”.
For example, an entry “\citet[pp. 282-283]{JC09}” that previously translated into “Chambers (2009, pp. 282-283)” now displays as “?, pp. 282-283”. The “\bibitem[Chambers(2009)]{JC09}” entry is still at the end of the file and prints with the Bibliography, but “Compile PDF” in RStudio cannot find it make the proper substitution for “\citet[pp. 282-283]{JC09}”.
Any suggestion on how to fix this?
Thanks,

R documentation, how to force Rcmd Rd2pdf to automatically break an overfull line in examples R-like section?

My package passed Rcmd check successfully. but in constructed PDF format of R documentation, within the examples section, half of the line (R code) is out of the paper. I also found another CRAN submitted package, ftsa , that suffer from overfulling in a line too, see ftsa Reference manual.
I guess this
problem roots in the behavior of the verbatim environment in Latex. However there are some Latex packages to deal with this, https://tex.stackexchange.com/questions/14342/verbatim-environment-that-can-break-too-long-lines, but I do not know how to use them with Rcmd.
In dealing with these cases, why Rcmd check does not show any error, warnings or note as Latex does?
How can I force line breaking in examples section?
Thank you
R CMD check does not analyze / forward all LaTeX warnings. Yes, in an ideal world it would.
It has been the case, always that you should format the \examples{ ... } section
well, yourself,
notably including sensible line breaks (and leading spaces) for nice alignment of multi -line examples.
Use the sources of R itself or recommended packages (or those authored by me, as I do pay attention to this quite a bit), to see good examples in their *.Rd files.
Remember: The current development sources of R are always web accessible at
R (devel) source tree # svn.r-project.org, i.e. R's standard packages at R sources src/library.

R -- Vignettes that are not made by Sweave possible?

Can I include some PDF in the pkg/doc folder so that the vignette function works, but no corresponding Rnw, Rtex, etc exists?
I am thinking of slides or documents containing markdown text weaved with R chunks, which have a different build process and hence different file extensions.
The writing R extensions guide suggests that it should be possible to include documents which can not be build at installation time, but the vignette function seems to look for files with special extensions (Rnw, Rtex, etc) and also for a file called vignette.rds.
Any hints are appreciated.
I asked about this several years ago, and while Fritz Leisch is amenable to the idea, he hasn't had the time to implement it.
(Cross-posted from a response I just left on R-help:)
As a workaround, you could include your own xvignette function in your package: see below.
It won't show you indices, but it will pick up any appropriately named file that you include in the inst/doc directory of your
package ...
xvignette <- function(vname,pkg,ext="pdf") {
vname <- paste(vname,ext,sep=".")
fn <- system.file("doc",vname,package=pkg)
if (nchar(fn)==0) stop("file not found")
utils:::print.vignette(list(pdf=fn))
invisible(fn)
}
You'll have to somehow alert your package users to the fact that this alternative documentation exists -- perhaps in the help file for the package itself.
You might fill in the default value of pkg above with your package name to make it easier on the user: I thought about using some variant of getPackageName(environment(xvignette)) to do it automatically, but that seems too complicated ...
Brian Ripley also mentioned in his response to the question that:
At present vignette() means Sweave documents, as only they have
metadata like titles. This is planned to be changed soon.
... but I don't know what "soon" means (it will be about 6 months until 2.14.0 comes out, I think)
edit: http://article.gmane.org/gmane.comp.lang.r.devel/28449 details another workaround (creating a dummy vignette that incorporates the existing PDF file)
edit 2: And
here's what Yihui Xie has to say about including knitr-based vignettes in packages (essentially another "dummy vignette" strategy)
vignette about non-Sweave vignettes from the R.rsp package
This is supported natively as of R 3.0.0, see http://yihui.name/knitr/demo/vignette/.
Instructions to use knitr as vignette engine boil down to:
add %\VignetteEngine{knitr::knitr} to the Rnw source document (note you still need %\VignetteIndexEntry{} as before)
specify VignetteBuilder: knitr in the package DESCRIPTION file
add Suggests: knitr in DESCRIPTION if knitr is needed only for vignettes
See also the official R documentation on that topic.

Resources