This notebooks is really all one big exercise: by viewing the HTML version of this (which is what you’re doing, right?), try to recreate the key components of this file on your own! Open the *.Rmd version of this file to see how things were made.

Remember to use the cheat sheet to help!

Also remember: really the best way to learn is to see what the range of things you can do is, and then just try using it for your own projects. You can look up the details and the syntax for specific features when you want to use them. Just get proficient with the basics to start.

Step 1: Header

Set up the header. Set a value for the title, and your name. I like the date to be re-generated each time I knit the file, so I use a snippet of R code instead of writing in a date:

date: “`r Sys.Date()`”

Yes, the back-tick denoted code section goes inside normal quotes. Also note: creating the above literal snippet of a code block is difficult and not a normal thing to do unless you’re writing workshop materials. They way I’ve done it above is a hack. I wouldn’t try to replicate it as an exercise (I still haven’t figured out a good way to do it consistently).

Also, turn on a floating table of contents.

Step 2: Sections

Make some sections in your document. Use different levels.

This is a subsection

This is a subsubsection

How far can we go?

This level isn’t in the table of contents anymore.

Level 5!

Step 4: Code Chunks

Finally, let’s write some code!

2+2
## [1] 4
hist(rnorm(100))

We can change figure sizes

hist(rnorm(100))

Chunks that don’t return anything won’t have output

x <- 4

When there are multiple lines with output, by default, your chunk will get split:

mean(rnorm(100))
## [1] -0.02423002
sd(rnorm(100))
## [1] 0.9974543

Step 5: Chunk Options

We can control whether chunks are evaluated, whether the code and output is shown, and how output is formatted.

First, a normal chunk

x<-10

Don’t evaluate this next one:

x<-5
x

Above wasn’t evaluated, so x will still be 10

x
## [1] 10

You can also just get the output (don’t include the code):

Step 6: Formatting Output

We want our tables to look pretty. There are multiple options. I like datatable from the DT package:

install.packages("DT")
library(DT)
mtcars %>% datatable()

For static tables, try kable, or stargazer for regression output.

What else?

A few parting thoughts.

Something that isn’t in this example file, but can be found in other *.Rmd exercise files for Research Computing workshops, is a paramter up top that is used to either include or exclude exercise answers. This is useful if you’re writing homework assignments, workshops, or if you want a version of your files with the code, and a different one with only the output.

You can knit RMarkdown documents by calling the rmarkdown::render function. (You don’t have to just hit the button.) See the knitall.r files in the Intro to R workshop repo.

It’s often useful to put a chunk with your system information at the end of a file, so at least you know what version of everything was used, so that if it breaks in the future with updates, you have a chance to recover old versions.

sessionInfo()
## R version 3.3.2 (2016-10-31)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: macOS Sierra 10.12.6
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] DT_0.2
## 
## loaded via a namespace (and not attached):
##  [1] htmlwidgets_0.8 backports_1.1.0 magrittr_1.5    rprojroot_1.2  
##  [5] tools_3.3.2     htmltools_0.3.5 yaml_2.1.14     Rcpp_0.12.13   
##  [9] stringi_1.1.2   rmarkdown_1.6   knitr_1.16      jsonlite_1.3   
## [13] stringr_1.2.0   digest_0.6.12   evaluate_0.10