1 Setting Up Your Computer
Before diving into R, make sure your system is set up properly:
Install the Latest Version of R
Download the most current release from CRAN.
IMPORTANT: Ensure you update to the latest version before starting this course—even if R is already installed—as older versions may cause unexpected issues when using newerRpackages (like this 1)Install RStudio (Recommended IDE for R)
Get RStudio from here.Launch RStudio
Open RStudio, and you’re all set to begin!
1.1 Configure RStudio Settings
Refer to the video rstudio_settings.mp4 for a visual guide. Follow these steps to configure your RStudio settings:
1.1.1 1. General Settings
Navigate to Tools → Global Options → General.
- Workspace subsection:
- Uncheck
Restore .RData into workspace at startup
Prevents old objects from automatically loading when opening RStudio, ensuring a clean and reproducible environment. - Set
Save workspace to .RData on exitto Never
This avoids saving hidden dependencies that can make your scripts less reproducible.
- Uncheck
1.1.2 2. Code Editing Settings
Go to Tools → Global Options → Code → Editing.
- Enable “Use Native Pipe Operator (
|>)”:
Since you’ll be using a later version of R, this setting improves readability and reduces dependency on packages like{magrittr}. - Enable “Soft-wrap R source files”:
This ensures that long lines wrap within the editor, so you don’t have to scroll horizontally even if the code line is long.
1.1.3 3. Code Display Settings
Under Tools → Global Options → Code → Display:
- Enable “Rainbow Parentheses”:
Displays matching parentheses in different colors, making nested expressions easier to read and reducing errors.
1.1.4 4. Appearance Settings
Finally, go to Tools → Global Options → Appearance.
- Editor Font: Change to a monospaced font (for example, JetBrains Mono). A monospaced font ensures each character takes up the same width, improving alignment and readability.
- Editor Theme: Choose a dark theme instead of the default light theme to reduce eye strain. Good options include:
- Cobalt (recommended)
- Monokai
- Solarized Dark
- Twilight
These settings help improve usability, maintainability, and comfort while coding in RStudio.
2 Overview of the RStudio Interface
Refer to the video rstudio_interface.mp4 for a detailed overview. RStudio is divided into four main panes:
2.1 1. Script Editor (Top Left)
This is where you write and edit your scripts (files ending in .R, .Rmd, or .qmd). A script is essentially a record of instructions that R executes. Use Ctrl + Enter (Windows/Linux) or Cmd + Enter (Mac) to run code from the script; the command output will appear in the Console.
2.2 2. Console and Terminal (Bottom Left)
- Console:
Where R executes commands. You can run code from your script or type directly into the Console to test ideas. - Terminal:
Adjacent to the Console, this pane runs system shell commands. It shares the same working directory as R but interprets commands using your system shell (PowerShell on Windows, Bash on Linux, or Zsh/Bash on Mac).
2.3 3. Environment and History (Top Right)
- Environment Tab:
Lists all objects (variables, data, functions) currently loaded in your R session. - History Tab:
Displays a record of all commands you have executed.
2.4 4. Files, Plots, Packages, and Help (Bottom Right)
- Files Tab:
Shows files and folders in your current working directory. - Plots Tab:
Displays any generated plots; you can zoom in, export (to PNG, PDF, JPEG, etc.), and clear old plots. - Packages Tab:
Lists installed R packages and offers management options. - Help Tab:
Provides documentation for functions and packages. You can search for help topics here or use the?function_namecommand in the Console.
This interface layout makes it easy to write, test, and manage your R code.
- installing source package ‘httr2’ …
** package ‘httr2’ successfully unpacked and MD5 sums checked ** using staged installation
Warning in system(“sh ./configure.win”) : ‘sh’ not found
ERROR: configuration failed for package ‘httr2’
- removing ‘C:/Users/Wang/AppData/Local/R/win-Library/4.2/httr2’ Warning in install.packages :
```
The packages we ask you to install are usually easy to install, so if you have errors like this, it might be because your
Ris out of date:↩︎