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!
Before writing any R code, youβll need to install the R programming language. Hereβs how to do it on different operating systems.
Run the installer and follow the prompts (default settings work fine).
macOS
Choose the .pkg file under "Latest Release" and install it.
Linux
sh
sudo apt-get update
sudo apt-get install r-base sh
sudo dnf install R 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!
While the base R console works, RStudio is the most popular IDE for R, offering a user-friendly environment tailored for data science.
When you first open RStudio, you'll see four panes:
Ctrl+Enter to run code). A well-organized file structure is crucial for reproducibility and collaboration, especially in clinical programming.
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
To enforce this structure:
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/...).
Letβs practice importing a dataset in RStudio:
patients.csv) in data/raw/. scripts/01_data_import.R) with: # Load required package
library(tidyverse)
# Import data
patients <- read_csv("data/raw/patients.csv")
# Preview data
head(patients)
Ctrl+Shift+Enter). Note: If you get an error like "tidyverse not found", install it first:
install.packages("tidyverse")
Reinstall or check for conflicting software.
Package Installation Fails
Try a different CRAN mirror:
r
options(repos = c(CRAN = "https://cloud.r-project.org"))
install.packages("tidyverse")
File Path Errors
data/raw/file.csv) instead of absolute paths. getwd(). data/, scripts/, outputs/). 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! π