R - Environment Setup
R - Modern Environment Setup
Section titled “R - Modern Environment Setup”Core Components: R and RStudio
Section titled “Core Components: R and RStudio”Setting up a modern R development environment involves two key components:
- R: The programming language and core compiler itself.
- RStudio Desktop: A powerful and free Integrated Development Environment (IDE) that makes working with R much more efficient. It includes a code editor, an R console, plotting windows, package management tools, and much more.
The recommended setup process is to first install R, and then install RStudio.
Step 1: Installing R
Section titled “Step 1: Installing R”Always download R from the official Comprehensive R Archive Network (CRAN). This ensures you are getting the latest stable version.
Windows Installation
Section titled “Windows Installation”- Go to the CRAN website for Windows.
- Click the prominent link to “Download R-X.X.X for Windows” (where X.X.X is the latest version number).
- Run the downloaded
.exeinstaller. It is safe to accept the default settings during installation.
macOS Installation
Section titled “macOS Installation”- Go to the CRAN website for macOS.
- Choose the appropriate package (
.pkgfile) for your version of macOS and your Mac’s processor (Apple Silicon M1/M2 or Intel). - Open the downloaded
.pkgfile and follow the installer instructions. - Alternative (for command-line users): If you use Homebrew, you can install R by running
brew install rin your terminal.
Linux Installation
Section titled “Linux Installation”You can install R using your distribution’s package manager. Open a terminal and run the appropriate command:
# For Debian/Ubuntusudo apt updatesudo apt install r-base
# For Fedora/RHEL/CentOSsudo dnf install RFor more detailed instructions, refer to the CRAN Linux documentation.
Step 2: Installing RStudio Desktop
Section titled “Step 2: Installing RStudio Desktop”Once R is installed, download and install RStudio.
- Go to the RStudio Desktop download page.
- The website should automatically recommend the correct installer for your operating system. Download it.
- Run the installer, accepting the default settings.
After installation, you can launch RStudio. You will see an integrated environment with a console pane, which is where you can type R commands. You will write your code in the script editor (top-left pane).
Managing R Packages
Section titled “Managing R Packages”One of R’s greatest strengths is its vast collection of third-party packages. You can install packages from CRAN using the install.packages() function. For example, to install the ggplot2 package for advanced data visualization:
> install.packages("ggplot2")A highly recommended starting point for any new R user is to install the tidyverse, which is a collection of essential data science packages that share a common design philosophy, including ggplot2 (for plotting), dplyr (for data manipulation), readr (for reading data), and more.
# This single command installs a suite of powerful, modern R packages> install.packages("tidyverse")
# To use a package in your R session, you must load it with the library() function> library(tidyverse)