Making knitr optional

knitr, latex, r

Solution

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}

Problem

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)?

Original source