Making knitr optional - r

When writing a paper I generally use knitr to embed tables and plots that I generate in R. All of this works exceptionally well for me. However, some of my coauthors are not as enthusiastic about this workflow and would much rather just leave interactions with knitr to me and concentrate on writing their sections without having to bother with the R code. They would also much rather not have to install R, RStudio and various packages.
So, is there any way of typesetting LaTeX documents with embedded knitr chunks without having to run them through R first? Is there a way, in other words, to simply ignore the chunks during the typesetting process (or perhaps to replace them with dummy tables/plots)?

Update: revised description here
This does not answer the exact question, but maybe the use case. I had a similar challenge recently: I wanted to combine the writing and analysis in one .rnw file, but my collaborators didn't want to use R/RStudio/GitHub/LaTeX.
So I decided to share a subfolder of my git repo with them via Dropbox. This folder contains three .docx files: introduction.docx, methods.docx, and discussion.docx (I write the results section within the .rnw file). The only catch is that they have to use some very basic LaTeX when writing, e.g., \subsection{heading} for headings, \cite{key} for references, ``quotes'', escaping \%, \$, and \&.
Back in the .rnw file, I convert the .docx files to .txt:
system("textutil -convert txt introduction.docx")
and then rename the file extension from .txt to .tex:
file.rename("introduction.txt", "introduction.tex")
Then outside of the R code chunks, I call in the .tex files with:
\input{introduction}
I posted a small example to GitHub.
\documentclass{article}
\makeatletter
\renewcommand{\#biblabel}[1]{\quad#1.}
\makeatother
\date{}
\bibliographystyle{plain}
\begin{document}
\begin{flushleft}
{\Large
\textbf{My Title}
}
\end{flushleft}
\section{Introduction}
% do not write in this section...let collaborators write in introduction.docx
<<intro, include=FALSE>>=
# assumes wd set to root folder collaborate
# convert docx to txt
system("textutil -convert txt introduction.docx")
# rename txt to tex
file.rename("introduction.txt", "introduction.tex")
#
% pull in introduction.tex
\input{introduction}
\section{Methods}
<<methods, include=FALSE>>=
system("textutil -convert txt methods.docx")
file.rename("methods.txt", "methods.tex")
#
\input{methods}
\section{Results}
<<results>>=
dat <- data.frame(x=runif(30, 0, 30))
mean <- mean(dat$x, na.rm=TRUE)
#
The mean is \Sexpr{round(mean, 1)}.
\section{Discussion}
<<discussion, include=FALSE>>=
system("textutil -convert txt discussion.docx")
file.rename("discussion.txt", "discussion.tex")
#
\input{discussion}
\bibliography{example.bib}
\end{document}

Related

Include RNW file

Code
\input{./A1_sample/sample.Rnw}
How to include Rnw file so that the R code in the file is executed? Using \input does not work.
The command is \SweaveInput.
Usage:
\begin{document}
\SweaveInput{PATH_TO_RNW_FILE/RNW_FILE.Rnw}
\end{document}
For further information see: https://support.rstudio.com/hc/en-us/articles/200486298-Working-with-Multiple-Rnw-Files
The patchDVI package provides various project management facilities for handling multiple file documents. It requires a bit of setup, but works well in TeXShop or TeXworks. Unfortunately I don't think RStudio can be easily customized to use it.
Your document main.Rnw would look something like this:
\documentclass{article}
<<echo=FALSE,results="asis">>=
# .SweaveMake <- 2 # For complete make, uncomment this
.SweaveFiles <- c("file1.Rnw", "file2.Rnw", "file3.Rnw")
.TexRoot <- "main.tex"
#
\begin{document}
\input{file1}
\input{file2}
\input{file3}
\end{document}
Each of the file*.Rnw files would have a similar header, listing main.Rnw and other dependent files as .SweaveFiles. Then the compiler can be told to run Sweave on all changed files and LaTeX on main.tex when it processes the file.

Put figure directly into Knitr document (without saving file of it in folder) Part 2

I am extending a question I recently posted here (Put figure directly into Knitr document (without saving file of it in folder)).
I am writing an R package that generates a .pdf file for users that outputs summarizations of data. I have a .Rnw script in the package (here, my MWE of it is called test.Rnw). The user can do:
1) knit("test.Rnw") to create a test.tex file
2) "pdflatex test.tex" to create the test.pdf summary output file.
The .Rnw file generates many images. Originally, these all got saved in the current directory. These images being saved to the directory (or maybe the .aux or .log files that get created upon calling pdflatex on the .tex file) just does not seem as tidy as it could be (since users must remember to delete these image files). Secondarily, I also worry that this untidiness may cause issues when scripts are run multiple time.
So, in my previous post, we improved the .Rnw file by saving the images to a temporary folder. I have been told the files in the temporary folder get deleted each time a new R session is opened. However, I still worry about certain things:
1) I feel I may need to insert a line, like the one on line 19:
system(sprintf("%s", paste0("rm -r ", temppath, "/*")))
to automatically delete the files in the temporary folder each time the .Rnw file is run (so that the images do not only get deleted each time R gets restarted). This will keep the current directory clean of the images, and the user will not have to remember to manually delete the images. However, I do not know if this "solution" will pass CRAN standards to have a line to delete files in the temporary folder. The reason is that it deletes files in the user's system, which could cause problems if other programs are writing files to the temporary folder. I feel I have read about CRAN not allowing files to be written/deleted from the user's computer for obvious reasons. How strict would CRAN be about such a practice? Is there a safe way to go about it?
2) If writing and deleting the image files in a temporary file will not work, what is another way to accomplish the same effect (run the script without having cumbersome image files created in the folder)? Is it possible to instead have the images directly embedded in the output file (not needing to be saved to any directory)? I am pretty sure this is not possible. However, I have been told it is possible to do so with .Rmd, and that I could convert my .Rnw to .Rmd. This may be difficult because the .Rnw file must follow certain formats (text and margins) for the correct output, and it is very long. Is it possible to make use of the .Rmd capability (of inserting images directly into the output) only for the chunks that generate images, without rewriting the entire .Rnw file?
Below is my MWE:
\documentclass[nohyper]{tufte-handout}
\usepackage{tabularx}
\usepackage{longtable}
\setcaptionfont{% changes caption font characteristics
\normalfont\footnotesize
\color{black}% <-- set color here
}
\begin{document}
<<setup, echo=FALSE>>=
library(knitr)
library(xtable)
library(ggplot2)
# Specify directory for figure output in a temporary directory
temppath <- tempdir()
# Erase all files in this temp directory first?
#system(sprintf("%s", paste0("rm -r ", temppath, "/*")))
opts_chunk$set(fig.path = temppath)
#
<<diamondData, echo=FALSE, fig.env = "marginfigure", out.width="0.95\\linewidth", fig.cap = "The diamond dataset has varibles depth and price.",fig.lp="mar:">>=
print(qplot(depth,price,data=diamonds))
#
<<echo=FALSE,results='asis'>>=
myDF <- data.frame(a = rnorm(1:10), b = letters[1:10])
print(xtable(myDF, caption= 'This data frame shows ten random variables from the distribution and a corresponding letter', label='tab:dataFrame'), floating = FALSE, tabular.environment = "longtable", include.rownames=FALSE)
#
Figure \ref{mar:diamondData} shows the diamonds data set, with the
variables price and depth.Table \ref{tab:dataFrame} shows letters a through j
corresponding to a random variable from a normal distribution.
\end{document}

Using knit spin or stitch to interleave R into a template

This is probably documented somewhere, but I cannot find it ...
I have an .R file which can be used as a read_chunk() file and call from an .Rnw latex template or in a .Rmd file for primary review.
This design worked well for the first part of our project, but since the .R file will change, it's not 'reproducible research'.
Since I already have a 'template' with the named chunks referring to chunks in the read_chunk() file, is there any way to interleave the R into the .Rnw for posterity?
You want the reverse operation of purl(), I believe. I don't know any such function, but technically it doesn't seem too hard; all the pieces are there:
chunks <- knitr:::knit_code$get()
for(k in names(chunks))
cat(c(sprintf("<<%s>>=", k), chunks[[k]], "#\n"), sep = "\n")
would return all the chunks in a format suitable for inclusion in the Rnw file.

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.

How do I Sweave a multiple-file project?

I am writing my thesis in LaTeX and because things got a bit long for my taste, I had split it into several files. Let's call them thesis.tex, intro.tex, mat_n_met.tex, rslts.tex and discsn.tex. I have linked intro.tex, mat_n_met.tex, rslts.tex and discsn.tex through thesis.tex with \include{intro} (and so on...).
I have also created a separate file called r_crunching.Rnw (that I run through Sweave) that holds a chunk that runs the R script with data analysis and chunks that produce pdf outputs of graphs that I embed via \includegraphics (in e.g., rslts.tex). Still following?
If I run a Rnw (i.e. I renamed rslts.tex to rslts.Rnw) without "a link" to the chunk with the R script, you will get a Sweave() error saying the reference in \Sexpr{} doesn't exist. Is there a way, without merging all the files into a single .Rnw, to call \Sexpr{} in say rslts.Rnw?
Other methods how to accomplish this are welcome.
I recommend using RStudio (http://www.rstudio.com/ide/). Sweave is nicely integrated into that IDE and it supports multi-file documents. Even Synctex and TeX error log navigation still work when working with multi-file documents.
From the master file you can include child files using
\SweaveInput{Child.Rnw}
You can link a child file back to the master file by including the directive
% !Rnw root = Master.Rnw
in the child file. That way when working on a child file and typesetting it, RStudio know to typeset the master file.
The details are explained in the RStudio documentation at http://www.rstudio.com/ide/docs/authoring/multiple_rnw_files
Forget for a second that you are dealing with Sweave and just think of the latex problem -- for which \include and \includeonly offer solutions. Try that with a few simple test files.
Once you have that figured out, fold Sweave back into the mix and it just work as Sweave is after 'merely' a pre-processing step, albeit a very clever one.
To expand Dirk's and mjm's answer, I would suggest using \include's and Makefiles.
Suppose you have a master file: master.tex. In that file, you include some .tex and .Rnw files, i.e.
\include chapter1
\include chapter2
\include chapter3
....
Now the following Makefile provides functions for creating the .tex, .R and .pdf files:
.SUFFIXES: .tex .pdf .Rnw .R
MAIN = master
##List your your .Rnw includes
RNWINCLUDES = chapter1 chapter2 chapter3
TEX = $(RNWINCLUDES:=.tex)
RFILES = $(RNWINCLUDES:=.R)
RNWFILES = $(INCLUDES:=.Rnw)
all: $(MAIN).pdf
$(MAIN).pdf: $(TEX) $(MAIN).tex
R: $(RFILES)
.Rnw.R:
R CMD Stangle $<
.Rnw.tex:
R CMD Sweave $<
.tex.pdf:
pdflatex $<
bibtex $*
pdflatex $<
pdflatex $<
Essentially, the .SUFFIXES provide a set of rules for convert from one file format to another. For example, to convert from .Rnw to .R, we use the command
`R CMD Stangle $<`
one fairly obvious answer is to use a makefile, possibly using package cachesweave, to process the relevant files in the right order.
My solution to multi-file projects in Sweave (under Rstudio) is the following:
1) Create a master file, say master.Rnw, in which you have the calls to the subfiles intro.Rnw, matmet.Rnw, etc:
\documentclass[11pt]{book}
% \usepackage{blah, blah} as you wish
\graphicspath{ {./figs/}
\begin{document}
\SweaveOpts{concordance=TRUE}
\include{intro} % a call to 'intro.Rnw'
\include{matmet} % a call to 'matmet.Rnw'
\include{results} % a call to 'results.Rnw'
\include{discuss} % a call to 'discuss.Rnw'
\end{document}
2) Create the subfiles. I'm giving here only the first one, intro.Rnw. Please note that in the subfiles you do not use preambular commands such as \documentclass or \begin{document}
\chapter{Introduction}\label{ch:intro}
\section{This is section 01}
In section 01 we are concerned about whether \texttt{Sexpr} could possibly work. The chunk below creates a variable \em{a} which will be referred to by this command later on.
<<>>=
a <- 1+2
#
Ok, if it is working, we shall see number 3 right here: \Sexpr{a}.
3) After saving modifications in 'intro.Rnw', simply go to 'master.Rnw' and compile it using Ctrl+Shift+K and... voilá:
Screenshot of the file created by the above command.

Resources