5 Introduction to RMarkdown

The ultimate guide to RMarkdown can be found here https://bookdown.org/yihui/rmarkdown/. But if you just want to get started quickly, read on.

5.1 Quick Start-Up Guide

R markdown is a (relatively) simple way to combine text, R code, and results from your analyses (including tables, graphs, and charts) into a professional looking report. This book, for example, was written with R markdown. If you are familiar with LaTeX, you will find many similarities when it comes to formatting and including equations. You will be using R markdown to write and submit your assignments and projects. To open a RMD file, click on File, New File, R Markdown and then just follow the prompts. R will create a “starter file” that you can then modify as needed.

The starter RMD file
The starter RMD file

5.2 Knitting

Turning your file into a pretty document is called knitting. Theoretically, you have three options for your output: html, pdf, or word. Using a pdf output directly requires that you have a working version of LaTeX installed and follow LaTeX syntax. This approach is sometimes a little tricky, but lets you use all the LaTeX formatting such as colors, complex tables, etc.

An easier way might be to knit to html, open your file in a browser, and then save as pdf. You may want to play around a little and see which output looks and works better for you.

RMD is sensitive to indentations, blank lines, and the number of spaces you insert. It takes a while to get used to what the output looks like, and to make it look the way you want. For starters, it might be easiest to pick one format and get used to that, rather than switching. For me, it worked best when I started with one file and changed things to see what happens. Most of all, be patient while you learn, it’s a steep learning curve. In case you are wondering, this book was knitted to HTML.

5.3 Sample RMD

Below you find the code and the output for a sample RMD. The best way to learn would be to first compare the output and the code, and then start changing and trying out things. The files needed are also stored in the Chapter 5 sub-folder.

The code

The Output

5.4 Assignment

Generate a variable x with 101 elements ranging from 0 to 10 in steps of 0.1. Next, create a variable error as error <- rnorm(101, 0, 0.3). Use an appropriate graph to plot error. Add error to x. Compute y as $x^2+3x-1$. Graph x vs y.

To hand in: A PDF file with your RMarkdown, including your code and your graphs.