Sample Rtex document
Author
Jeremy Foote
Last Updated
5 years ago
License
Creative Commons CC BY 4.0
Abstract
Very basic example of including R code in a LaTeX document.
Very basic example of including R code in a LaTeX document.
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{wrapfig}
\usepackage{float}
\usepackage[breaklinks]{hyperref}
\hypersetup{colorlinks=true, linkcolor=Black, citecolor=Black, filecolor=Blue,
urlcolor=Blue, unicode=true}
<<init, echo=FALSE>>=
knit_hooks$set(document = function(x) {
sub('\\usepackage[]{color}',
'\\usepackage[usenames,dvipsnames]{color}', x, fixed = TRUE)
})
opts_chunk$set(fig.path="figures/knitr-")
opts_chunk$set(fig.pos = 'H')
@
\begin{document}
\title{Sample Rtex document}
\author{Statistics and Statistical Programming\\
(MTS 525)\vspace{1em}\\
Your Name\\
\href{mailto:youremail@u.northwestern.edu}{youremail@u.northwestern.edu}
}
\date{Spring, 2019}
\maketitle
\begin{abstract}
There are many great ways to do reproducible research. One tool is \href{https://yihui.name/knitr/}{knitr};
it helps you source data and execute R code directly in the (\LaTeX) report you are writing. This document gives a few examples of how it is used.
\end{abstract}
\section{Overview}
Knitr supports the integration of R and \LaTeX. You might think of it as an analogous tool to RMarkdown in this respect: whereas RMarkdown enables you to create notebooks that integrate R code and Markdown, knitr allows you to create documents that combine R code and \LaTeX code. This means that you can use knitr to create papers and reports that are fully reproducible in the sense that you can generate all the analysis and visualizations each time you create a new version of the pdf. Update your dataset? Collect new data? Discover a bug in the analysis code? If you work on your project in knitr (or RMarkdown), you can quickly regenerate the analysis, tables, and figures with a single click.
We will discuss some of the advantages and tradeoffs of this approach, as well as some more general thoughts about reproducible research workflows in class. The rest of this document illustrates how you might go about creating a knitr document that presents analysis of some data. We have created it in Overleaf, a web-based \LaTeX collaboration and editing tool that our research group uses frequently. Keep in mind, this document is just a regular \LaTeX document with the knitr features (and some other odds and ends) enabled within it. That means you can still do all the other things \LaTeX can do on top of the knitr features (e.g, manage styling and references).
\section{Tables}
You can do lots of things within a document. For example, here I create a dataframe and a table of the first few rows. Also, notice that in the caption I use the \texttt{\textbackslash Sexpr\{\}} syntax, which lets you access R variables inline within a document.
\begin{table}[H]
\centering
<<example_table, echo=FALSE>>=
df = data.frame(x.1 = rnorm(100),
x.2 = rnorm(100),
x.3 = sample(c(T,F), 100, replace=T))
df$y = 2 * df$x.1 + 3 * df$x.2 + df$x.3 + rnorm(100)
kable(head(df), format="latex")
@
\caption{This is a test table. The mean value of y is \Sexpr{mean(df$y)}.}
\label{tab:test}
\end{table}
\section{Code}
You can also display code, by changing `echo' to TRUE. In this example, I get the mean of $y$ for each $x.3$ group.
<<example_code, echo=T>>=
# Get the mean for each group
aggregate(y ~ x.3, data=df, mean)
@
\section{Regression Tables}
You may also want to display regression tables. This simple table has two models: one of just $x.1$ as a predictor for $y$. The second model includes the other predictors.
<<example_regression, echo = F, results='asis', message=F>>=
library(xtable)
m1 <- lm(y ~ x.1, data = df)
m2 <- lm(y ~ x.1 + x.2 + x.3, data = df)
xtable(m1)
xtable(m2)
@
\section{Plots}
You can also dispaly plots. You just put the ggplot code in a code chunk, include a fig.height or fig.width to change the dimensions. Calling it within a "figure" environment (as I do here) allows additional control.
\begin{figure}[H]
<<example_boxplot, fig.height=5, echo=F>>=
library(ggplot2)
ggplot(df) +
geom_boxplot(aes(y = y, x = x.3))
@
\caption{Boxplot of the values of y for each x.3 group}
\end{figure}
\begin{figure}[H]
<<example_plot, fig.height=5, echo=F>>=
library(ggplot2)
ggplot(df,aes(x=x.1, y = y, color = x.3)) +
geom_point() +
geom_smooth(method='lm')
@
\caption{Scatterplot of y by x.1, grouped by x.3. Lines give the linear regression of y on x.1 for each group.}
\end{figure}
\end{document}