Overleaf provides an integration with knitr, which allows you to add R code to your LaTeX document to generate a dynamic output.Documents that contain R code must be saved with the extension .Rtex or .Rnw, otherwise the code won’t work. Let’s see an example:
\documentclass{article}\begin{document}You can type R commands in your \LaTeX{} document which will be processed and their output included in the document:<<>>=# Create a sequence of numbersX = 2:10# Display basic statistical measuressummary(X)@\end{document}
Open this knitr example on OverleafAs you can see, the text in between the characters <<>>= and @ is R code. This code and its output is printed in a listing-like format. This chunk of code can take some extra parameters to customize the dynamic output, as described in the next section.
A code block like the one presented in the previous section is usually called a chunk. You can set some extra options in knitr chunks. See the example below:
\documentclass{article}\begin{document}You can type R commands in your \LaTeX{} document which will be processed and their output included in the document:<<echo=FALSE, cache=TRUE>>=# Create a sequence of numbersX = 2:10# Display basic statistical measuressummary(X)@\end{document}
It is possible to access objects generated in a chunk and print them in-line.
\documentclass{article}\begin{document}You can type R commands in your \LaTeX{} document which will be processed and their output included in the document:<<echo=FALSE, cache=TRUE>>=# Create a sequence of numbersX = 2:10# Display basic statistical measuressummary(X)@So, the mean of the data is $\Sexpr{mean(X)}$\end{document}
Open this knitr example on OverleafThe command \Sexpr{mean(X)} prints the output returned by the R code mean(X). Inside the braces, any R command can be passed.
Open this knitr example on OverleafThis histogram uses data stored in “data.txt”, saved in the current working directory. A few figure-related options are passed to the chunk.plot1This is the label used to reference the plot. The prefix “fig:” is mandatory. You can see in the example that the figure is referenced with \ref{fig:plot1}.fig.pos="t" positioning parameter. This is the same used in the figure environment.fig.height=4, fig.width=4Figure width and height.fig.cap="First plot"Caption for the figure.
You can import parts of an external R script into a knitr document. This is very helpful since it is fairly common to write and debug the script in an external program prior to including it in your document.Suppose we have the following R code in a file called mycode.R which we include in our LaTeX document:
## ---- myrcode1# Create a sequence of numbers X = 2:10## ---- myrcode2# Display basic statistical measuressummary(X)
Notice the lines:
## ---- myrcode1
and
## ---- myrcode2
These mark the beginning of a chunk of code and are mandatory if you want to use this script in our document, as shown in the following code fragment:
The chunk below will not be printed<<echo=FALSE, cache=FALSE>>=read_chunk("mycode.R")@The code must show up here<<myrcode2>>=@
The first chunk is not printed and is only used to import the script with the command read_chunk("mycode.R") . That’s why the option echo=FALSE is set. Also, scripts must not be cached. Once the script is imported, you can print a chunk using the label you set after ## ----. In this case it’s myrcode2.We have put all the article code fragments into a project that you can Open on Overleaf .