Welcome to R

Setting Up Your Workspace and Tools

Author

Raju Rimal

Published

June 11, 2026

R is one of the most popular programming languages for data analysis, visualization, and statistical computing. Whether you’re exploring data for the first time or building advanced machine-learning models, R provides a comprehensive toolkit. This guide will help you install R and set up your workspace, ensuring a smooth start to your data journey.


Step 1: Installing R

Downloading R

  1. Visit the official CRAN (Comprehensive R Archive Network) website.
  2. Choose your operating system:
    • Windows: Select “Download R for Windows”.
    • macOS: Select “Download R for macOS”.
    • Linux: Follow instructions under “Download R for Linux”.

Installing R on Your System

  • Download the R installer (.exe file).
  • Run the installer and follow the on-screen prompts.
  • Accept default settings unless you have specific preferences.
  • Download the appropriate .pkg file for your version of macOS.
  • Open the file and follow the installation wizard.

Open a terminal and add CRAN to your system’s package manager.

sudo apt update
sudo apt install r-base

Step 3: Setting Up Your Workspace

Getting Familiar with RStudio

When you open RStudio, you’ll see four main panes:

  1. Console: Run R commands interactively.
  2. Environment/History: View variables and command history.
  3. Files/Plots/Packages/Help/Viewer: Manage files, view plots, and access help.
  4. Source Pane: Write and edit scripts.

Setting Preferences

  1. Go to Tools > Global Options to customize RStudio to your liking.
  2. Adjust font size, theme (dark or light mode), and default working directory.

Step 4: Installing Essential Packages

R’s capabilities can be extended with packages. Here’s how to install and load them:

Installing a Package

Run this command in the console:

install.packages("tidyverse")

The tidyverse is a collection of essential packages like dplyr (data manipulation) and ggplot2 (data visualization).

Loading a Package

After installation, load the package into your session:

library(tidyverse)

Step 5: Running Your First R Script

  1. Click File > New File > R Script in RStudio to create a new script.

  2. Write your R code, for example:

    # Load a built-in dataset
    data(mtcars)
    
    # Display the first few rows
    head(mtcars)
    
    # Plot miles per gallon vs horsepower
    with(mtcars, plot(mpg, hp, main = "MPG vs Horsepower"))
  3. Save the script with a .R extension.

  4. Run the code line-by-line by pressing Ctrl + Enter (Windows) or Cmd + Enter (Mac).


Step 6: Exploring R Help and Documentation

R comes with comprehensive documentation and built-in help:

  • Use ?function_name to access a function’s help file.

    ?mean
  • Browse package documentation:

    help(package = "ggplot2")
  • Search for help across all installed packages:

    ??regression

Step 7: Next Steps

Once you’ve set up R, here are some directions to deepen your skills:

  1. Learn the Basics: Explore functions, loops, and conditionals.
  2. Data Analysis: Work with data frames, manipulate datasets, and create visualizations.
  3. Projects and Scripts: Organize your code into reusable scripts and functions.
  4. Reproducible Reports: Learn R Markdown or Quarto to create dynamic documents.

Troubleshooting Tips

  • Package Installation Issues:

    If a package fails to install, ensure your R version is up-to-date.

    install.packages(
        "package_name", 
        dependencies = TRUE
    )
  • Accessing Old R Versions:

    Visit the CRAN Archive for older R versions.

  • Error Messages:

    Don’t panic! Google the error or check Stack Overflow for solutions.


Conclusion

Congratulations! You’ve set up R and are ready to start your data journey. With its versatility and powerful ecosystem, R is a fantastic choice for analysis, visualization, and beyond. As you progress, remember that practice is key. Keep experimenting, exploring new packages, and building projects to unlock the full potential of R.