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
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.
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
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.
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.
Bold or Italics
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:
For our assignments, you are going to choose Show code and output.
2 + 4## [1] 6
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 dataThis code will produce a graph with a figure caption. Notice that the figure caption text is in quotes.
hist(iris$Sepal.Length) # histogramFigure: Histogram of Sepal Length from the iris data set.
Once you have a finished product, you can create a free online HTML document with R pubs.
To do this: