RMarkdown Set Up
Goal
This document will walk through the last piece of set up you need to get ready to code. In this course, we will be using a type of file called an RMarkdown file when we work with R. This type of file allows us to combine essentially a Word document with a space to do code. It is very convenient, as it will allow you to answer all your lab questions easily without needing to copy/paste any output or save any codes.
Getting Set Up with RMarkdown
Typically, when you open up RStudio, you will see something like
this:
There are three windows: one large one on the left and two smaller on the right. We actually want to start with 4 windows, so we need to take one more step.
Go to File
at the top of your RStudio Screen. Find
New File
and then choose RMarkdown
.
When you do that, a prompt box will pop up.
Type in STA 111 Lab 1 as your title and put your full name in the
author box. Once you have done that, hit okay. Once you have done so,
you should see something like this:
What you are looking at is a template for an
RMarkdown document. This is a special type of file that
allow us to combine a Microsoft Word document with code. To get ready to
use it, we want to delete some of the things that are just for
demonstration. Go ahead and delete everything after the first gray box
in the file.
At this point, we are ready to start!
Starting your RMarkdown File
There are two main types of things you can do in an RMarkdown file.
- You can type things like you would in a Microsoft Word or Google Doc file.
- You can run R code.
Let’s try each of these.
To start off with, let’s try typing. Go to line 12 on your file and
type: Welcome to RMarkdown!
Now, look at the top of this window and find Knit.
This will be next to a blue ball of yarn with knitting needles. Click
Knit to HTML. What will
pop up is the HTML file your Knit command created. This
is the file you would submit when you were done with your lab!
Right now, this file isn’t very interesting. Where is the code?? Well, let’s add some! We have already seen that if we type in the white space in a Markdown file, the file acts like a Word document. To tell the computer we want to give it code, we need to insert a Code Chunk into our Markdown file.
To insert a Code Chunk, look at the top of your
window and find Code and then choose Insert
Chunk. When you do this, a little gray box (a code chunk) will
appear in your Markdown file. Inside that code chunk, you can type R
code. Let’s try that by typing 1 +1 + (2*3)
.
When you have done that, hit Knit again. You will
see that your Knit file shows your code( 1 + 1 + (2*3)
) and
it also shows the result of running that code (which is 8). This is the
beauty of a Markdown file! You can add both words and code in the same
document.
Formatting for Labs
Now that you have the basics, you are almost ready to start on Lab 1. We only need one more thing, which is a little bit of formatting.
When you work on your labs, some questions will involve only words, some will involve only code, and some will involve both. We want to make it clear for the person who is grading where one Question stops and another Question starts. To do this, we add section headers to our RMarkdown file.
To try this out, make your Markdown file match the following:
When you have done so, click Knit again.
Do you see that we know have Question 1 in bold? The
##
we typed in our Markdown file did that! You will also
see that the actual text of the is question in italics. The
*
in the Markdown file did that. We then have our answer,
and our code, right after.
This is how you will set up your labs! Each time you have a new
question, just start a new line and put two ##
with the
question number.
Starting the Lab
At this point, you are ready to start Lab 1! You can refer back to this document anytime you need to refresh. We will use these same steps all semester long.