R Markdown is a way to turn your R code and output into a document. It has the ability to go from text, to code, to graphics, and back with ease. You will be required to turn in homework assignments as R Markdown files (Don’t worry, I will give you templates and cheat sheets).
What does Markdown mean?
To answer that, you need to know the meaning of markup language. It is a way to format and annotate digital documents. Markup language is the ML in HTML (Hyper Text is the HT). It lets the person or machine reading it know all the behind the scenes information about a document, like font, color, margins, etc. Go to your favorite website and press F12… this will give you an idea of what it takes to make that page work.
Markdown is (thankfully) a much simpler version of markup using plain language.
R Markdown has three basic pieces to it:
The very beginning of any R Markdown document is the YAML. This seriously stands for “YAML Ain’t Markup Language”. But all you need to know is that it is where some of the file characteristics are defined. Mostly we will use it for creating a title, author, and type of file (Word, PDF, or HTML). Some more advanced options of the YAML include font, style, and many other customization options.
One beautiful thing about R Markdown is that when you want to add or write text to the file, you can do it just by typing normally. It will look very basic, and if you are used to typing documents in Word, you will find that it is definitely different. There is no tab, or highlighting words to make then bold or italic. The document you write in R Markdown needs to be “Knit” together in order to make it look like you want. Which means, you need to tell R which words are bold or italic. For example, to get those two words to look like that, I had to type **bold** and *italic*.
Surrounding words by * will make them italic, and surrounding by ** will make them bold. Another common feature is headers. Headers are denoted by #, the largest sized header (Like “What is R Markdown?” above) has one #. Each additional # will make the header smaller (### 2. Text).
You can make lists, tables, block quotes, write mathematical equations, and so much more in the text portion of an R Markdown document. We will start simple and not deal with most of this yet. Visit this link for a downloadable cheat sheet of common text formatting. (By the way, you can include working links in R Markdown documents!!)
The last type of code you can have in an R Markdown document is a chunk of R code. These chunks will always start the same way.
Whatever you put between the first line of ticks and the last line of ticks will run exactly like R. And any code that produces output will also show in your document.
Here is an example of some of the R Intro lecture. Take a look at the .Rmd file for this lesson to see how I wrote the chunk to get the following:
6 + 5 * 4
## [1] 26
(6 + 5) * 4
## [1] 44
Chunks can have different settings as well. For instance, sometimes you will not want to actually run the code you type, just show it in the document. To do this, you can specify {r, eval = FALSE} at the beginning of the chunk, instead of just {r}.
6 + 5 * 4
(6 + 5) * 4
Other times, you will want the output shown, but not the code you wrote to get it. You will need to say {r, echo = FALSE}.
## [1] 26
## [1] 44
These are the two most common statements you will make at the beginning of your R chunk, but there are many more with different results.
Plots and other output are also shown in the document.
x <- 1:10
y <- 10:1
plot(x, y)
So anything you do in R can be done in an R Markdown file.
Your markdown file looks like code, which is not what you want to show to clients or teachers. You want the nice, formatted final product. To do this, you can click the Knit button at the top of the window. This will compile your document and give you a nice final product.
There are three types of files you can knit a document into. This document has been knit into all three types, so feel free to compare them.
A basic webpage format that you can use to view your progress or publish online. There is a website called R Pubs https://rpubs.com/ that will host any document you would like for free. You can click the Publish button at the top right of the viewer screen. Just set up an account, but be careful, anything on R Pubs is publicly accessible. So no sensitive information or data.
Hint: Use the HTML format to check your progress. Knit often, that way if there are errors, you can pin point them easily. Click the Settings wheel (right of knit) and select “Preview in Viewer Pane” to avoid pop-ups.
For Windows systems: In order to create a PDF you will need to download and install a software called MiKTex. The link is here https://miktex.org/download. Note, you will need to download the Net Installer.
For Mac systems: You can download and install MacTeX. The link is here http://tug.org/mactex/.
Once the appropriate software is installed, you can Knit to PDF. This format is better for sharing, emailing, and looks better than HTML or Word.
You can knit to a word document if you have word installed on your machine (maybe even if you don’t). I find that the word documents from R Markdown look less organized. But it has the benefit of being able to edit the file in word. Sometimes when you need to copy a quick graph or some code, word can be the best option.