Get single latex file instead of PDF for documentation of R package - r

I would like to get the Latex source for the PDF that is built when executing
R CMD Rd2pdf /path/to/mypackage
for building the R package documentation.
It would be very nice if I could get the documentation as a latex document such that I could easily include it in another latex document as an appendix and have a continuous page numbering.
I have only seen the possibility to compile the files one by one from .Rd to .tex:
How to create R documentation file (.Rd) in latex?, which points to the function tools::Rd2latex and the question How do I convert Rd files to pdf for a package that I am creating in R? which points to the command
R CMD Rdconv -o x.tex --type=latex man/x.Rd
Both commands only work on single .Rd files. The created files lack the header/definitions. So I would have to sort and include each file manually in the final latex file. It would be nice if there is some way to get the latex document in a simpler way.

You can do:
R CMD Rd2pdf path/to/package/man --no-clean
In addition to the PDF file, this creates a folder containing the auxiliary files, including the tex source file.

Related

Long documents on R Sweave include files

I'm writing a book in R Sweave, as long as the document is becoming larger I use the \include LaTeX command to include files. When I PDF-Compile the main file the files with R chunks are not included. What extension they must have .tex or .Rnw?
From the Sweave User Manual, p. 7
Latex files can include others via \input{} commands. These can
also be used in Sweave files, but the included files will be included by
Latex and are not processed by Sweave. The equivalent if you want
the included files to be processed is the \SweaveInput{} command.
Included files should use the same Sweave syntax (see below) and
encoding as the main file.

How to get a Sweave pdf into a different directory

In Ubuntu, suppose in /home/folder1 I have test.Rnw. To generate a pdf I will go (assuming this directory is my getwd) within my R console:
Sweave(test.Rnw)
texi2pdf(test.tex)
However, my objective is to have the pdf save in another existing folder called /home/folder2. How do I compile the .Rnw (which is to remain in /home/folder1) and the resulting .tex (don't care which folder this gets into) such that the pdf ends up in /home/folder2?
It seems that I want to interfere with either the arguments in Sweave(...) or texi2pdf(...) but haven't found a parameter I can toggle to set export directory.
It is simpler to use knitr package:
knit2pdf(input=/home/folder1/test.Rnw,
output =/home/folder2/test.pdf)
Another option is to use pandoc :
pandoc -f latex /home/folder1/test.tex -s -o /home/folder2/test.pdf

How to automatically create BibTex citations for R packages in knitr file?

I am not sure whether this an R, LaTeX, or BibTex problem.
I am trying to automatically generate a .bib file containing citations for R packages and then list them at the end. I am able to generate the BibTex file and I don't see anything wrong with the BibTex file, but the entries don't appear when I compile the PDF.
I'm not sure if R is not producing a BibTex file correctly, if some LaTeX syntax is wrong, or if the BibTex file needs to be pre-compiled or whatever. I noticed that \bibliography{NOT A REAL FILENAME} will produce a References section without complaining, but I don't think that is the problem.
Minimal working example:
\documentclass[10pt]{amsart}
\usepackage[margin=1in, headheight=20pt, footskip=20pt]{geometry}
\begin{document}
<<label='Create References'>>=
require(knitr) # Needed for write_bib()
# Load some packages to the session:
require(xtable)
require(ggplot2)
# Select packages to cite:
citPkgs <- names(sessionInfo()$otherPkgs)
# Write the bibtex file:
write_bib(citPkgs, file="R-Pckgs.bib")
#
\nocite{*}
\bibliographystyle{plain}
\bibliography{R-Pckgs.bib}
\end{document}
Any help or suggestions would be appreciated.
Just replace \bibliography{R-Pckgs.bib} with \bibliography{R-Pckgs}, and it should work fine.
On Windows 7, with an up-to-date MikTeX installation and current R and R packages, the following worked:
Put your reproducible example in a file named "eg.Rnw" and edit to remove the extraneous ".bib"
Launch R and navigate to the directory in which "eg.Rnw" is located.
Do library(knitr); knit2pdf("eg.Rnw")
Note: There are obviously many workflows for going from *.Rnw to *.pdf, but if you want to use knit2pdf() (at least), make sure that you run it from the directory containing the *.Rnw to be processed.
Add a \nocite{dummycite} to your document - to create a citation so that the bibliography is printed.
Note that this is obsolete if you already have other citation in your document.

build R package using report generation with knitr

I am doing report generation in R with knitr.
So basically I have a dataset, do some preprocessing and then call knitr to output an html report.
This means the entire workflow consists of several R code files and some .Rhtml templates which are needed later on for report generation.
I would like to wrap all of this into a R package.
Having just .r files I would just run package.skeleton() and have a start..
But, how do I deal with the .Rhtml files. What is the proper way to deal with these when building a R package?
Thanks,
Ben Bolkers answer:
put them in a directory within an inst directory and use system.file() to retrieve them.

How to convert R Markdown to HTML? I.e., What does "Knit HTML" do in Rstudio 0.96?

What commands are run when pressing "Knit HTML" on an R Markdown file in Rstudio 0.96?
My motivation is that I might want to run the same command when I'm in another text editing environment or I might want to combine the command in a larger makefile.
Basic Script
So now that the R markdown package has been released, here is some code to replicate the features of Knit to Html.
require(knitr) # required for knitting from rmd to md
require(markdown) # required for md to html
knit('test.rmd', 'test.md') # creates md file
markdownToHTML('test.md', 'test.html') # creates html file
browseURL(paste('file://', file.path(getwd(),'test.html'), sep='')) # open file in browser
where test.rmd is the name of your R markdown file.
Note that I'm not 100% confident about the browseURL line (hence my question here about opening files in a web browser).
markdownToHTML Options
The good thing about markdownToHTML is that there are heaps of options in how the HTML is created (see ?markdownHTMLOptions). So for example, if you want just a code fragment without all the header information, you could write:
markdownToHTML('test.md', 'test.html', options='fragment_only')
or if you don't like hard wrapping (i.e., inserting line breaks when there are single manual line breaks in the markdown source), you can omit the 'hard_wrap' option.
# The default options are 'hard_wrap', 'use_xhtml',
# 'smartypants', and 'base64_images'.
markdownToHTML('test.md', 'test.html',
options=c('use_xhtml', 'base64_images'))
Makefile
This could also all be added to a makefile perhaps using Rscript -e (e.g., something like this). Here's a basic example makefile I put together, where test indicates that the rmd file is called test.rmd.
RMDFILE=test
html :
Rscript -e "require(knitr); require(markdown); knit('$(RMDFILE).rmd', '$(RMDFILE).md'); markdownToHTML('$(RMDFILE).md', '$(RMDFILE).html', options=c('use_xhtml', 'base64_images')); browseURL(paste('file://', file.path(getwd(),'$(RMDFILE).html'), sep=''))"
The makefile uses my preferred markdown options: i.e., options=c('use_xhtml', 'base64_images')
Put Sys.sleep(30) in a chunk and you will see clearly what commands are called by RStudio. Basically they are
library(knitr); knit() to get the markdown file;
RStudio has internal functions to convert markdown to HTML;
The second step will be more transparent in the next version of the markdown package. Currently you can use knitr::knit2html('your_file.Rmd') to get a similar HTML file as RStudio gives you.
Update on 2019/09/17: The above answer applies to RStudio v0.96 (in the year 2012). Now R Markdown is compiled through rmarkdown::render(), which uses Pandoc instead of the retired R package markdown. See the post Relationship between R Markdown, Knitr, Pandoc, and Bookdown for more details.
Very easy command line method from knitr in a knutshell:
R -e "rmarkdown::render('knitr_example.Rmd')"
This requires rmarkdown to be installed with install.packages(rmarkdown) and that pandoc is installed (apparently it comes with Rstudio, see knitr in a knutshell for more details).
So far when I've used this it nicely puts all the plots in the HTML file rather than as images in a figure directory and cleans up any intermediate files, if any; just like compilation in RStudio does.
It seems you should call rmarkdown::render() instead of knitr::knit2html() because a.rmd appears to be an R Markdown v2 document.

Resources