First, we’ll start by making a new folder for your course material like we just discussed. Make a new folder somewhere in your Documents folder called something like “Gov50” or “gov_50” (remember – no spaces!):
"Computer" -> "Documents" -> Right click -> "New" -> "Folder"
"Finder" -> "Documents" -> Right click -> "New Folder"
Now that’s done, we will create a file to store your code in. No code in the console will be saved the next time you open RStudio. If you want to save your work, you need to store it in a file. There are files that will save only code, but it is convenient to save your writing, code, and outputs like plots and figures all in one place.
This is what RMarkdown is for. An RMarkdown file is a document just like Microsoft Word or Google Docs. If you want to save your work, you need to make a document and save.
Let’s make an RMarkdown file by going to File --> New File --> R Markdown
like in the picture below:
A window like this should appear - please give it any title you like, make sure HTML is selected, and press OK.
Now go to File --> Save
(or press CMD + S on Mac / Ctrl + S on PC), give your file a name (something like day1.Rmd
is fine!), and save it. It would be a good idea to create a new folder on your computer for this course and save it there.
There is some setup code there that you don’t need. Go ahead and delete everything after the ---
(shown below).
You can delete all of the text and code in the red box – don’t remove the dashes up top!
Then, click on the green Insert button, select R, and you will see a chunk. A chunk is a place where you can write code and R will evaluate it when you press the green arrow on the side. You can write regular text outside of the chunk and it will not be evaluated as code. Write something simple like 2 + 2:
To run your code, you can click on the Knit
button with the blue yarn to run your code. You should see a new window appear like this with your finished code. RStudio may ask you to install some additional packages, to which you can answer “yes.”