2. Creating R Projects
Video Tutorial
Opening R Studio
When you load RStudio, the screen will be split in three. This is some important vocabulary to direct yourself around RStudio and get help when you need it.
The CONSOLE is where all your code is run. Here you will see the output from the code you run. The COMMAND LINE is where you can type in code and execute it to the console. Code you run in the console is not saved. Try typing 1+1
into the console, and you’ll see that R spits back 2.
The ENVIRONMENT is where all your data will be stored. R is an object oriented programming language. Think of it like having a bunch of spreadsheets open at once. The environment shows you all the data you have loaded, and what each dataset, list, or other object is called.
The DIRECTORY is in the bottom right. This links to all the files in your current folder, called your working directory. If you are ever in the wrong working directory, you can set it by running the setwd()
function or going to “Session” -\> “Set Working Directory.” We will keep files organized by using an R Project.
Creating a Project
An R Project is basically a folder that will hold all your files together in one place - including your code, raw data, and any output you may produce.
Create your first R Project by clicking on the projects icon in the top right. You can create a project from a new or existing directory.
When you return to RStudio to work on a saved project, open the project again by using the Project menu in RStudio, or double clicking the .Rproj
file in the project directory.
Creating Your First Script
Go to File -> New Script and save it to your project folder
Use the assignment operator <-
to save values, dataframes, and other objects to the environment for future use.
Use command+enter
(Mac) or ctrl + enter
(Windows) to run your code. Or select all and then run the shortcut to run the whole script at once.