Here is a short introduction to the basics of the RStudio layout. Now you should have a general understanding of what is R and RStudio, R links to other languages, and potential extension of functionality via user-contributed packages of functions. The next section will teach you how to install and set up this software.
Throughout this lesson, we're going to teach you some of the fundamentals of the R language as well as some best practices for organizing code for scientific projects that will make your life easier.
We'll be using RStudio: a free, open-source R Integrated Development Environment (IDE). 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/Connections (tabbed in the upper right)
- Files/Plots/Packages/Help/Viewer (tabbed in the lower right)
Once you open files, such as R scripts, an editor panel will also open in the top left.
R scripts
Any commands that you write in the R console can be saved in to a file to be re-run again. Files containg R code to be ran in this way are called R scripts. R scripts have
.R
at the end of their names to let you know what they are.
Workflow within RStudio
There are two main ways one can work within RStudio:
- Test and play within the interactive R console, then copy code into
a
.R
file to run later.- This works well when doing small tests and initially starting off.
- It quickly becomes laborious.
- Start writing in a
.R
file and use RStudio's shortcut keys for the Run command to push the current, selected, or modified lines to the interactive R console.- This is a great way to start; all your code is saved for later
- You will be able to run the file you create from within RStudio
or using R's
source()
function.
Tip: Running segments of your code
RStudio offers you great flexibility in running code from within the editor window. There are buttons, menu choices, and keyboard shortcuts. To run the current line, you can
- click on the
Run
button above the editor panel, or- select "Run Lines" from the "Code" menu, or
- hit Ctrl+Return in Windows or Linux or ⌘+Return on OS X. (This shortcut can also be seen by hovering the mouse over the button). To run a block of code, select it and then
Run
. If you have modified a line of code within a block of code you have just run, there is no need to reselect the section andRun
, you can use the next button along,Re-run the previous region
. This will run the previous code block including the modifications you have made.
Source: The Carpentries, https://swcarpentry.github.io/r-novice-gapminder/01-rstudio-intro/index.html
This work is licensed under a Creative Commons Attribution 4.0 License.