Important Dates/Information

Due date: Friday, February 16, 2018 11:59PM . No late submission allowed! Please submit to blackboard.

Update to submission guidelines: Submit both RMD, TEX, and the respective PDFs. Please submit all files in such format: _lab#_mmdd.extension ********

Introduction

Overview

Learning Goals

  1. Use RMarkdown to create intuitive, literave programmin examples
  2. Complie RMD files to html, pdf, or word
  3. Create basic LaTEX documents with Overleaf

In the first lab assignment, I made reference to the fact that these lab assignments (the actual documents) are created in R. That was slightly a lie. The lab assignments are typeset using RMarkdown. Markdown is a plaintext formatting language which allows for the creation of easy lists, tables, headers, italics etc… Markdown is a writing format, ie it contains commands which are interpreted as formatting cues and will alter the final document. R Markdown (often called RMD) is a nifty composition of R and the Markdown syntax. It allows for you to write text and R code seamlessly in the same document. When you codeblocks like in previous labs, it’s an indication that you are looking at an RMD file. The core of RMarkdown is text, and code chunks and we will subsequently discuss both. The purpose for letting you create documents in this way is to allow you to accurately describe your thought process when doing analysis. You can show someone (me, your boss, research collaborators) what code you ran, what it produced, and then describe in words how you interprete it and then move on to other analysis. I highly recommend this read as an intro to what is referred to as Literate Programming. The idea is to develop programs which are “Demanded by the logic and flow of their thoughts”.

Text

RMD files are written in plaintext and formatted with Markdown. The first page of this PDF is what I have printed out on my desk for a cheat sheet. Additionally if you’re in RStudio, the help menu (Help ->Markdown Quick Reference) is a great guide also and sits lovingly in the Rstudio help pane. As an example, what follows is a list of my most-used Markdown syntax, and the resulting creation.

### Header 3
1. List *item* 1. Can also be italicised with _item 1_
2. **Bold** requires multiple astericks or __Underlines__
  * Sub list items are available for ordered or unordered lists
3. ![This is a link to an arbitrary file ](../bensPlot.png)
4. ![Whereas this is a link to an image using the same syntax](http://i0.kym-cdn.com/entries/icons/original/000/016/212/manning.png) 
5. Web hyperlinks are done automatically
  * Like this http://www.google.com
  * Or you can have link text like [this link to Ben's homepage](http://web.ics.purdue.edu/~barachun)

Header 3

  1. List item 1. Can also be italicised with item 1
  2. Bold requires multiple astericks or Underlines
  • Sub list items are available for ordered or unordered lists
  1. This is a link to an arbitrary file
  2. Whereas this is a link to an image using the link syntax
  3. Web hyperlinks are done automatically

The only additional note is that you can change the header by using more or less #s before the header. One hashtag is a bigger header, more hashtags is smaller.

Chunks

The other important piece of the puzzle is actually adding your code to the document. If you create an RMD file (File, New File, R Markdown) everything you write will be text unless you add a chunk. Chunks look like the following but you can add them either by typing out the syntax or by using the RStudio Insert button in the top right. As you’ve seen so far, we create code and the resulting output is also included in the file.

plot(runif(100))

In the RMD file this code looks like the following. Just note that the dash symbols are the ones which go with the ~ key not the single quote mark key.

'''{r}
plot(runif(100))
'''

You can specify options in the {r} block which will change the execution of the code. THis can be suppressing warnings, suppressing output, changing the ouptut format etc… A good list of those is available in the R Markdown Reference Guide. (Help -> Cheat Sheats -> R Markdown Reference Guide).

Knitting/Compiling

When you have written your document, marked it up, and added the code blocks the final step is to Knit the document. Knitting first runs the code, then formats the output along with the document formatting to create the final file. You have the option to create an HTML, PDF, or Word file and this can be done by clicking the knit button in Rstudio. The document as you see it here is the result of such compilation. The source code is also available here. I’ll add the RMD source to all future labs.

\(\LaTeX\)

\(\LaTeX\) (Pronounced Lah-Tech or Lay-Tech emphasize on the tech) is a typesetting language similar in spirit to Markdown. You use a programming syntax to specify how a document will look. Funny enough, LaTeX was created by the same person who coined the idea of Literate Programmin (Donald Knuth) so we actually can consider the two spiritual siblings.

At it’s heart, LaTeX lets you format documents with absolute precision. Unlike Word where a small change will ripple through, the outputs of LaTeX documents have the same consistantly as deterministic code. The presentation is also -with default configurations- very professional looking. In this course, your final project should be typeset in LaTeX so here’s an early intro.

Text

LaTeX text is formatted using tag pairs similar to HTML. You begin a document by saying \begin{document} and end the document with \end{document}. Sections are delimited with \section{Section Header} etc… The basic structure of a document is below (Note this is a commonly reproduced internet LaTeX example) which I’ve included for completeness.

\documentclass[a4paper]{article}

%% Language and font encodings
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}

%% Sets page size and margins
\usepackage[a4paper,top=3cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}

%% Useful packages
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage[colorlinks=true, allcolors=blue]{hyperref}

\title{Your Paper}
\author{You}

\begin{document}
\maketitle

\begin{abstract}
Your abstract.
\end{abstract}

\section{Introduction}

Your introduction goes here! Some examples of commonly used commands and features are listed below, to help you get started. If you have a question, please use the help menu (``?'') on the top bar to search for help or ask us a question. 

\section{Some examples to get started}

\subsection{How to include Figures}

First you have to upload the image file from your computer using the upload link the project menu. Then use the includegraphics command to include it in your document. Use the figure environment and the caption command to add a number and a caption to your figure. See the code for Figure \ref{fig:frog} in this section for an example.

\begin{figure}
\centering
\includegraphics[width=0.3\textwidth]{frog.jpg}
\caption{\label{fig:frog}This frog was uploaded via the project menu.}
\end{figure}

\subsection{How to add Comments}

Comments can be added to your project by clicking on the comment icon in the toolbar above. % * <john.hammersley@gmail.com> 2016-07-03T09:54:16.211Z:
%
% Here's an example comment!
%
To reply to a comment, simply click the reply button in the lower right corner of the comment, and you can close them when you're done.

Comments can also be added to the margins of the compiled PDF using the todo command\todo{Here's a comment in the margin!}, as shown in the example on the right. You can also add inline comments:

\todo[inline, color=green!40]{This is an inline comment.}

\subsection{How to add Tables}

Use the table and tabular commands for basic tables --- see Table~\ref{tab:widgets}, for example. 

\begin{table}
\centering
\begin{tabular}{l|r}
Item & Quantity \\\hline
Widgets & 42 \\
Gadgets & 13
\end{tabular}
\caption{\label{tab:widgets}An example table.}
\end{table}

\subsection{How to write Mathematics}

\LaTeX{} is great at typesetting mathematics. Let $X_1, X_2, \ldots, X_n$ be a sequence of independent and identically distributed random variables with $\text{E}[X_i] = \mu$ and $\text{Var}[X_i] = \sigma^2 < \infty$, and let
\[S_n = \frac{X_1 + X_2 + \cdots + X_n}{n}
      = \frac{1}{n}\sum_{i}^{n} X_i\]
denote their mean. Then as $n$ approaches infinity, the random variables $\sqrt{n}(S_n - \mu)$ converge in distribution to a normal $\mathcal{N}(0, \sigma^2)$.


\subsection{How to create Sections and Subsections}

Use section and subsections to organize your document. Simply use the section and subsection buttons in the toolbar to create them, and we'll handle all the formatting and numbering automatically.

\subsection{How to add Lists}

You can make lists with automatic numbering \dots

\begin{enumerate}
\item Like this,
\item and like this.
\end{enumerate}
\dots or bullet points \dots
\begin{itemize}
\item Like this,
\item and like this.
n_\end{itemize}

\subsection{How to add Citations and a References List}

You can upload a \verb|.bib| file containing your BibTeX entries, created with JabRef; or import your \href{https://www.overleaf.com/blog/184}{Mendeley}, CiteULike or Zotero library as a \verb|.bib| file. You can then cite entries from it, like this: \cite{greenwade93}. Just remember to specify a bibliography style, as well as the filename of the \verb|.bib|.

You can find a \href{https://www.overleaf.com/help/97-how-to-include-a-bibliography-using-bibtex}{video tutorial here} to learn more about BibTeX.

We hope you find Overleaf useful, and please let us know if you have any feedback using the help menu above --- or use the contact form at \url{https://www.overleaf.com/contact}!

\bibliographystyle{alpha}
\bibliography{sample}

\end{document}

Overleaf

Similar to RMarkdown, LaTeX requries writing code and then compiling it to create the document. Ordinarily this requires installing the TeX compiler and usually an editor on your computer. However thankfully we have a much simpler example. Purdue contracts access to an online LaTeX compiler called Overleaf. Overleaf lets you view the immediate results of your TeX document and have it compiled server-side meaning you have nothing to set up on your computer. Register with your Purdue email address (It’s free) and you’ll have access to a web-drive where you can collaborate and create documents. Similar to how we learned how to use R, I am only going to provide references for the syntax of LaTeX as trying to cover the bases is too exhaustive for a semester let alone one assignment.

Questions

These questions will be about turning your previous work into LaTeX documents and RMD files. All of these should be submitted as individual files. Please do not submit anything in Word or Zip format as it exponentially increases the time to grade.

  1. Take your first lab assignment and re-create it as an RMarkdown File, then compile it to PDF. The content will not be graded only the formatting. Try to make sure it is readible, interesting, and importantly there aren’t pages of output. Submit both the RMD and compiled PDF file. If you’ve already done the first lab as an RMD, make sure it’s compiiled as PDF and you can submit that.

  2. Take your project proposal and reformat it as a LaTeX file. Submit the TEX and PDF file. If you previously wrote your proposal as a LaTex file, reformat it in Overelaf. Note I will also not be grading for content, just for formatting. You may have to google how to get the PDF and tex files from the web-based Overleaf. Do not copy and paste text from Overleaf into your local files.