🏠 Home
Author: Bhanoji Duppada | 2025-07-26 12:13:54

R Installation, IDEs (RStudio), and File Structure

Introduction

Welcome to Chapter 2 of From Clinical SAS Programmer to R Programmer: A Quick Primer! If you're coming from a SAS background, transitioning to R might feel like stepping into a new world. Fear notβ€”this chapter will guide you through the essential first steps: installing R, setting up a powerful Integrated Development Environment (IDE) like RStudio, and organizing your files efficiently.

By the end of this chapter, you'll:
- Know how to install R on Windows, macOS, and Linux.
- Understand the benefits of using RStudio and how to configure it.
- Learn best practices for structuring your R projects (a crucial skill for reproducibility).

Let's dive in!


Installing R

Before writing any R code, you’ll need to install the R programming language. Here’s how to do it on different operating systems.

Step 1: Download R

  1. Windows
  2. Visit the CRAN (Comprehensive R Archive Network) website.
  3. Click on "Download R for Windows" > "base" > "Download R-X.X.X for Windows" (latest version).
  4. Run the installer and follow the prompts (default settings work fine).

  5. macOS

  6. Go to CRAN.
  7. Click on "Download R for macOS".
  8. Choose the .pkg file under "Latest Release" and install it.

  9. Linux

  10. Open a terminal and use the package manager:

Step 2: Verify Installation

Open R from your terminal (Linux/macOS) or Start Menu (Windows). You should see the R console, a simple interface where you can type commands.

Example:

print("Hello, R World!")  

If you see the output [1] "Hello, R World!", congratulationsβ€”R is installed correctly!


Choosing an IDE: Why RStudio?

While the base R console works, RStudio is the most popular IDE for R, offering a user-friendly environment tailored for data science.

Benefits of RStudio

Installing RStudio

  1. Download the free version of RStudio Desktop from RStudio's website.
  2. Install the software (follow the default settings).

Configuring RStudio

When you first open RStudio, you'll see four panes:

  1. Source Editor (Top-left): Write and edit scripts.
  2. Console (Bottom-left): Execute R commands.
  3. Environment/History (Top-right): View variables and command history.
  4. Files/Plots/Packages (Bottom-right): Navigate files, view plots, and manage packages.

Customizing RStudio


Setting Up Your File Structure

A well-organized file structure is crucial for reproducibility and collaboration, especially in clinical programming.

Basic R Project Structure

For a typical data analysis project, organize files like this:

My_Project/  
β”‚  
β”œβ”€β”€ data/               # Raw and processed data  
β”‚   β”œβ”€β”€ raw/            # Original datasets (never modify!)  
β”‚   └── processed/      # Cleaned/derived datasets  
β”‚  
β”œβ”€β”€ scripts/            # R scripts  
β”‚   β”œβ”€β”€ 01_data_import.R  
β”‚   β”œβ”€β”€ 02_data_cleaning.R  
β”‚   └── 03_analysis.R  
β”‚  
β”œβ”€β”€ outputs/            # Results (tables, reports, figures)  
β”‚   β”œβ”€β”€ tables/  
β”‚   β”œβ”€β”€ plots/  
β”‚   └── reports/  
β”‚  
β”œβ”€β”€ docs/               # Documentation  
β”‚   β”œβ”€β”€ protocol.md  
β”‚   └── references/  
β”‚  
└── README.md           # Project overview  

Creating an R Project

To enforce this structure:

  1. In RStudio, click File > New Project.
  2. Choose New Directory > New Project.
  3. Name your project (e.g., Clinical_Trial_Analysis) and select a folder.

Now, all your work will be contained in this directory, making paths relative (e.g., data/raw/patients.csv instead of C:/Users/...).


Practical Example: Loading Data

Let’s practice importing a dataset in RStudio:

  1. Save a CSV file (e.g., patients.csv) in data/raw/.
  2. Create a script (scripts/01_data_import.R) with:
# Load required package  
library(tidyverse)  

# Import data  
patients <- read_csv("data/raw/patients.csv")  

# Preview data  
head(patients)  
  1. Run the script (Ctrl+Shift+Enter).

Note: If you get an error like "tidyverse not found", install it first:

install.packages("tidyverse")  

Troubleshooting Common Issues

  1. R or RStudio Won’t Open
  2. Reinstall or check for conflicting software.

  3. Package Installation Fails

  4. Ensure you’re connected to the internet.
  5. Try a different CRAN mirror:
    r options(repos = c(CRAN = "https://cloud.r-project.org")) install.packages("tidyverse")

  6. File Path Errors

  7. Use relative paths (e.g., data/raw/file.csv) instead of absolute paths.
  8. Check your working directory with getwd().

Key Takeaways

  1. Install R from CRAN and RStudio for an optimized workflow.
  2. Organize projects with a clear file structure (e.g., data/, scripts/, outputs/).
  3. Use R Projects to manage dependencies and paths effortlessly.
  4. Practice importing data using relative paths and packages like tidyverse.

In the next chapter, we’ll explore R syntax and basic operations, bridging the gap between SAS and R coding!


Coming Up in Chapter 3: R Syntax Fundamentals: From SAS to R.


This chapter provides a hands-on, beginner-friendly approach to setting up R, ensuring you’re ready to tackle data analysis efficiently. Happy coding! πŸš€