Environment Setup: R and RStudio
Before we start, we need to set up our development environment. While general software development often uses text editors, Data Science in R thrives inside RStudio, the premier Integrated Development Environment (IDE) specifically designed for R.
We will install R (the programming language/engine) and then RStudio Desktop (the visual interface that makes coding in R a breeze).
1. What is R and CRAN?
R is the core programming language that performs all the calculations. R is distributed via CRAN (the Comprehensive R Archive Network), a network of servers around the world that store up-to-date versions of R code and R packages.
2. What is RStudio?
RStudio Desktop is an IDE developed by Posit (formerly RStudio, PBC). It provides a code editor, a console to run R code directly, plotting windows, history tracking, and workspace visualization in a single, cohesive interface.
Installation Steps
Step 1: Install R (The Engine)
You must install R before installing RStudio.
- Go to the CRAN website: https://cran.r-project.org/
- Choose the download link matching your operating system:
- Download R for macOS
- Download R for Windows
- Download R for Linux
- Download the latest installer package (e.g.,
.pkgfor Mac,.exefor Windows) and run it. Accept all default settings.
Step 2: Install RStudio Desktop (The Dashboard)
Once R is installed, install RStudio.
- Go to the Posit download page: https://posit.co/download/rstudio-desktop/
- Scroll to Step 2: Install RStudio Desktop and click the download button for your operating system.
- Open the downloaded installer and follow the prompt instructions to complete the setup.
Launching RStudio
Once installed, open RStudio:
- macOS: Find RStudio in your
Applicationsfolder. - Windows: Press the Windows key, search for
RStudio, and press Enter.
Upon opening, you will see a workspace divided into four main panes:
- Source Editor (Top-Left): Where you write and save your R scripts.
- Console (Bottom-Left): Where R code runs interactively. You can type commands directly here and press Enter to see results.
- Environment/History (Top-Right): Shows all active datasets, variables, and history of run commands.
- Files/Plots/Packages/Help (Bottom-Right): Where you browse files, view graphs, install packages, and read documentation.
Think of R as the engine of a car and RStudio as the dashboard. The engine does the actual work, but you sit in the driver's seat and interact with the dashboard to drive. You cannot drive without the engine, and driving is much harder without the dashboard!
- Installation Order: If you open RStudio and get a warning that R is not found, it means you did not install R first. Close RStudio, install R from CRAN, and open RStudio again.
- Package Installation Errors: On Windows or macOS, if you are prompted to compile packages from source, it is generally safer to choose "No" to install pre-compiled binary packages, which install much faster without needing complex system tools.