Introduction to R and R Studio
Overview
Teaching: 20 min
Exercises: 10 minQuestions
How do you find your way around RStudio?
How do you interact with R?
Objectives
Describe the purpose and use of each pane in the RStudio IDE
Locate buttons and options in the RStudio IDE
Manage a workspace in an interactive R session
Manage packages
Introduction to RStudio
We’ll be using RStudio: a free, open source R integrated development environment. It provides a built in editor, works on all platforms (including on servers) and provides many advantages such as integration with version control and project management.
Basic layout
When you first open RStudio, you will be greeted by three panels:
- The interactive R console/terminal (entire left)
- Environment/History (tabbed in upper right)
- Files/Plots/Packages/Help/Viewer (tabbed in lower right)
Once you open files, such as R scripts, an editor panel will also open in the top left.
You can move the panels around in RStudio so that their arrangement suits you.
Challenge: filesystems revised
In the last episode, you used windows to work out the location of a file on your computer. Using your hand-drawn diagram, find that same file from the
Files
panel in RStudio.
Projects
R Studio provides in-built support for keeping all files associated with a project together. This includes the input data, R Scripts, analytical results and figures.
A good project layout will ultimately make your life easier:
- It will help ensure the integrity of your data;
- It makes it simpler to share your code with someone else (a lab-mate, collaborator, or supervisor);
- It allows you to easily upload your code with your manuscript submission;
- It makes it easier to pick the project back up after a break.
Challenge: Creating a self-contained project
We’re going to create a new project in RStudio:
- Click the “File” menu button, then “New Project”.
- Click “New Directory”.
- Click “New Project”.
- Type in the name of the directory to store your project, e.g. “intro-git”.
- Select “Create a git repository”
- Click the “Create Project” button.
Now when we start R in this project directory, or open this project with RStudio, all of our work on this project will be entirely self-contained in this directory.
The two main changes after creating and opening a project are the progect menu and the Git information pane. Both highlighted here:
Key Points
Use RStudio to write and run R programs.
Set up an RStudio project for each analysis you are performing.