This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. This is a wonder approach to force organization in R and also create easy to read reports. R markdown is similar an R script but is much more dynamic!

For more details on using R Markdown see http://rmarkdown.rstudio.com or http://www.stat.cmu.edu/~cshalizi/rmarkdown/#overview

So, let’s begin …

Creating a new R Markdown document

To create a new document, open R Studio. With R studio open go to:

File > New File > R Markdown…

At this point, you can edit the Title and Author. You want to chose HTML as the default output.

Copy and paste the fancy document instructions

When you open a new R markdown file, you will see a lot of basic information about the code. Your instructor (Brian Ohsowski) created instruction code at the begining of this document to make your output more readable. You can simply copy and paste everything above:

Copy / paste everything above this line for a new document

YOU SHOULD EDIT THE TITLE and AUTHOR INFO ONLY

Visualizing your document

When you click the Knit button, a document will be generated that includes both content as well as the output of any embedded R code chunks. This fancy output is visualized in the Viewer pane in R Studio. Also, everytime you Knit your file, you work is saved.

Typing out your text

To create document text, all you need to do is type into your R Markdown file. When you click Knit, you will be able to see your work in the Viewer pane. Format text easily with asterisks. This is bold and this is italics.

You can also create header levels with the hash # sign. Increasing the number of # symbols will add a new level by one.

Header Size 1

Header Size 2

Header Size 3

Header Size 4

Bold or Italics

Chunks: Including code is the beauty of R Markdown!

The real point of R Markdown is that it lets you include your code into a readable format. The code will run automatically when your document Kint together.

There is an extremely simple way to create and run code in R markdown.

At the top of the screen, you will see a green box with insert next to it. Click this box and choose R.

You will this output below…

Once you have the R format inserted, you can type in all of the R code in the homework. An example of a simple calculation is below. Knit the document and look at the Viewer. You will see the formula and the output in the HTML file.

You can also view your answers by clicking the green play symbol to the right.

2 + 4
## [1] 6

We can control what data is visualized in the HTML. To do this, create another R chunk. Then, click the setting gear on the right.

Here, you can:

  1. Name the chunk.
  2. Choose Output Settings
  3. Toggle a warning display

For our assignments, you are going to choose Show code and output.

2 + 4
## [1] 6

Graphs

You can also easily visualize graphs in the HTML. To do this, I will show you a very basic example with a figure caption.

data("iris") # Import iris data

This code will produce a graph with a figure caption. Notice that the figure caption text is in quotes.

hist(iris$Sepal.Length) # histogram
Figure: Histogram of Sepal Length from the iris data set.

Figure: Histogram of Sepal Length from the iris data set.

Turning in your work.

Once you have a finished product, you can create a free online HTML document with R pubs.

To do this:

  1. First register for a free R Pubs account at http://rpubs.com
  2. click the blue eye icon at the top of the window.
  3. Click Publish.
    4