Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making knitr optional

Tags:

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

like image 701
RoyalTS Avatar asked Jan 13 '14 15:01

RoyalTS


People also ask

How do I not show my RMD code?

Chunk Optionsinclude = FALSE prevents code and results from appearing in the finished file. R Markdown still runs the code in the chunk, and the results can be used by other chunks. echo = FALSE prevents code, but not the results from appearing in the finished file. This is a useful way to embed figures.

How do you make a R chunk?

You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).

What is knitr used for?

knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.

What is a knitr document?

knitr is an R package that integrates computing and reporting. By incorporating code into text documents, the analysis, results and discussion are all in one place.


1 Answers

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} 
like image 91
Eric Green Avatar answered Nov 22 '22 15:11

Eric Green