Introduction to R and RStudio

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)

RStudio

Once you open files, such as R scripts, an editor panel will also open in the top left.

RStudio


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:

  1. Test and play within the interactive R console, then copy code into a .Rfile to run later.
    • This works well when doing small tests and initially starting off.
    • It quickly becomes laborious.
  2. 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

  1. click on the Run button above the editor panel, or
  2. select "Run Lines" from the "Code" menu, or
  3. 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 and Run, 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
Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 License.

Last modified: Monday, January 9, 2023, 12:13 PM