# execute R code in latex file imported from separate R file

My personal objective in this Rnotebook revolution where typesetting and number crunching cohabitate is to keep R files separate from latex and as little polluted by markdown as possible and only in the form of R comments, like with javadoc for example or even perlpod. And in that I have succeeded so far by producing readable R-reports with minimal markdown safely confined within comment fences. Then from an R cli session one can do: library(rmarkdown); render('afile.R').

Now I am trying to typeset with latex and crunch with R.

But again: try to keep R and latex files separate. So instead of following what many naive examples on the net do (and end up in hell when files grow):

\documentclass[12pt]{article}
\begin{document}

Hello I am latex
<<echo=F>>=
cat('and I am R and 1+1 is ', 1+1)

x1 <- runif(1000,1,2)
hist(x1,breaks=10)
@
\end{document}


Ideally, I would like to do this:

\documentclass[12pt]{article}
\begin{document}

Hello I am latex and following is my R script:

\input{crunch.R}

\end{document}


where crunch.R is:

cat('and I am R and 1+1 is ', 1+1)

x1 <- runif(1000,1,2)
hist(x1,breaks=10)


Unfortunately this does not work because the input is not enclosed in <<>>= and @.

But neither does this crunch.R work (additionally the file can not be processed by R correctly):

<<>>=
cat('and I am R and 1+1 is ', 1+1)

x1 <- runif(1000,1,2)
hist(x1,breaks=10)
@


Nor this latex file:

\documentclass[12pt]{article}
\begin{document}

Hello I am latex and following is my R script:

<<>>=
\input{crunch.R}
@

\end{document}


for obvious reasons.

What I need is a special \inputR{filename.R} latex command which will take care of inserting the tags <<>>= and @.

Any idea where to find this command or how to write it?

• What if the .Rnw file has hundreds of lines beyond the two chunks you want to use? Mine does and I get an error message when I compile: Error in parse(text = code, keep.source = FALSE) : <text>:12:1: unexpected '@' #1: # ggsave(filename = "surveysperyear.PNG", path = "~/R/Projects/LAWYER Hornbooks/LFSurveys/figure") # 12: @ – lawyeR Sep 10 '18 at 20:59
• i don't think this is because of the hundreds of lines in the .Rnw file. Check that line and if you think there is nothing wrong with it just comment it out or better remove it. – bliako Sep 11 '18 at 23:00
• But I want to pick and choose about 12 plots from the .Rnw file. They are all over the place. And I can't comment out hundreds of lines just to preserve the lines with 12 plots. I appreciate your taking the time to respond. the question seems to be, how do you mark off the end of what begins with ## ---- chunk ----? – lawyeR Sep 11 '18 at 23:56

You can load external chunks using read_chunk and then execute them later. This simplifies the source document and allows you to keep code external. See Code Externalization.

So assume your R code is called Rcode.R This has an R comment line with the label of the code to be used in your .Rnw file, in this example the label is external-code. It looks like this:

# ---- external-code ----
cat('and I am R and 1+1 is ', 1+1)

x1 <- runif(1000,1,2)
hist(x1,breaks=10)


Now we have the following .Rnw file. Notice that this first reads the code, and then executes it separately. The code is referred to using the label defined in the Rcode.R file.

\documentclass[12pt]{article}
\begin{document}

Hello I am latex

<<external-code, cache=FALSE,echo=F>>=
@

<<external-code,echo=F>>=
@

\end{document}


You get the following output:

• Thanks, that works fine. It also worked with multiple labels so that I can interject latex comments in-between R chunks from the same R file. However, I still need more flexibility on filtering out comments in R script and especially Rmarkdown comments like #' the following plots ... which I have inserted for rendering using rmarkdown::render(). – bliako Mar 10 '18 at 20:33
• @bliako I'm not quite sure what you have in mind, but it sounds like a separate question. – Alan Munn Mar 10 '18 at 20:41
• sure, do you think this is an appropriate place or is there any other knitr-related forum? – bliako Mar 10 '18 at 20:42
• @bliako It's probably on the borderline here. There are plenty of knitr questions on Stackoverflow: stackoverflow.com/search?q=knitr so that might be a better place since the question isn't directly about LaTeX integration. – Alan Munn Mar 10 '18 at 20:56