File I/O
## Learning Objectives
- Read and write CSV files
- Work with Excel files
- Handle text files
- Work with R data formats
## Working Directory
### Getting/Setting Directory
```r
# Get current directory
getwd()
# Set working directory
setwd("/path/to/directory")
# RStudio shortcut: Session -> Set Working Directory
```
### Relative Paths
```r
# Within project directory
read.csv("data/file.csv") # relative
read.csv("/home/user/data/file.csv") # absolute
# Check if file exists
file.exists("data.csv")
```
## CSV Files
### Reading CSV
```r
# Base R
read.csv("file.csv")
read.csv("file.csv", header = TRUE)
read.csv("file.csv", sep = ",")
# With strings as characters (not factors)
read.csv("file.csv", stringsAsFactors = FALSE)
# Skip rows
read.csv("file.csv", skip = 2)
# Read first N rows
read.csv("file.csv", nrows = 100)
# NA strings
read.csv("file.csv", na.strings = c("NA", "", "N/A"))
```
### Reading with readr (tidyverse)
```r
library(readr)
# Read CSV
read_csv("file.csv")
# Read TSV
read_tsv("file.tsv")
# Read delimited
read_delim("file.txt", delim = "|")
```
### readr Options
```r
library(readr)
# Specify column types
read_csv("file.csv",
col_types = cols(
name = col_character(),
age = col_integer(),
score = col_double()
))
# Skip lines
read_csv("file.csv", skip = 2)
# No header
read_csv("file.csv", col_names = FALSE)
# Custom column names
read_csv("file.csv",
col_names = c("Name", "Age", "Score"))
# File from URL
read_csv("https://example.com/data.csv")
```
### Writing CSV
```r
# Base R
write.csv(df, "output.csv")
write.csv(df, "output.csv", row.names = FALSE)
# Overwrite without quotes
write.csv(df, "output.csv", quote = FALSE, row.names = FALSE)
# With append
write.csv(df, "output.csv", append = TRUE)
```
### Writing with readr
```r
library(readr)
write_csv(df, "output.csv")
write_tsv(df, "output.tsv")
```
## Excel Files
### Reading Excel
```r
# Install readxl package
install.packages("readxl")
library(readxl)
# Read Excel file
read_excel("file.xlsx")
read_excel("file.xlsx", sheet = 1)
read_excel("file.xlsx", sheet = "SheetName")
# Read all sheets
excel_sheets("file.xlsx")
list_frames <- lapply(excel_sheets("file.xlsx"), read_excel, path = "file.xlsx")
```
### Writing Excel
```r
# Install writexl package
install.packages("writexl")
library(writexl)
write_xlsx(df, "output.xlsx")
# Multiple dataframes
list_df <- list("Sheet1" = df1, "Sheet2" = df2)
write_xlsx(list_df, "output.xlsx")
```
### openxlsx Package
```r
install.packages("openxlsx")
library(openxlsx)
# Write with formatting
wb <- createWorkbook()
addWorksheet(wb, "Data")
writeData(wb, "Data", df)
saveWorkbook(wb, "output.xlsx", overwrite = TRUE)
# Read with formatting preserved
read.xlsx("file.xlsx")
```
## Text Files
### Reading Text
```r
# Read entire file as lines
lines <- readLines("file.txt")
head(lines, 10)
# Read delimited text
read.table("file.txt", sep = "\t")
# Scan (fast reading of numeric data)
numbers <- scan("numbers.txt", what = numeric())
```
### Writing Text
```r
# Write lines
writeLines(c("Line 1", "Line 2", "Line 3"), "output.txt")
# Append lines
writeLines(c("Line 4"), "output.txt", append = TRUE)
# cat (print to file)
cat("Hello\n", file = "output.txt")
cat("World\n", file = "output.txt", append = TRUE)
```
## R Data Formats
### .RData files
```r
# Save workspace
save.image("my_session.RData")
# Save specific objects
save(df, model, file = "my_data.RData")
# Load
load("my_session.RData")
```
### .rds files
```r
# Save single object (more efficient)
saveRDS(df, "df.rds")
# Load
df <- readRDS("df.rds")
```
### save vs saveRDS
```r
# save preserves names, loads to same names
save(df, file = "df.RData")
load("df.RData") # df appears in environment
# saveRDS returns object, you assign it
df <- readRDS("df.Rds")
```
## File Operations
### Check File Info
```r
# Check if file exists
file.exists("file.csv")
# File info
file.info("file.csv")
# size isdir mode mtime ctime atime exe
# 12345 FALSE "rw-r--" ...
# Get file extension
tools::file_ext("file.csv") # "csv"
# Get file without extension
tools::file_path_sans_ext("file.csv") # "file"
```
### File Paths
```r
# Build paths
file.path("dir", "subdir", "file.csv")
# Normalize path
normalizePath("~/file.csv")
# basename and dirname
basename("/path/to/file.csv") # "file.csv"
dirname("/path/to/file.csv") # "/path/to"
```
### Create/Delete Files
```r
# Create directory
dir.create("new_directory")
# Create nested directories
dir.create("a/b/c", recursive = TRUE)
# Copy file
file.copy("source.csv", "destination.csv")
# Delete file
file.remove("file.csv")
# Rename file
file.rename("old.csv", "new.csv")
```
## Connection Interfaces
### Reading from URL
```r
# Read from URL
read.csv("https://example.com/data.csv")
# Read from gzipped file
read.csv(gzfile("data.csv.gz"))
# Read from clipboard
# Windows
read.csv("clipboard")
# Mac
read.csv(pipe("pbpaste"))
```
### Reading Large Files
```r
# readr for large files
library(readr)
# Progress bar
read_csv("large_file.csv", progress = TRUE)
# Specify n_max to preview
read_csv("large_file.csv", n_max = 1000)
```
## Data Import Best Practices
### Import Checklist
```r
# 1. Check file structure first
readLines("file.csv", n = 5)
# 2. Read with appropriate function
# 3. Check structure with str()
# 4. Convert types as needed
# 5. Handle missing values
```
### Common Import Issues
```r
# Extra header row
read.csv("file.csv", header = FALSE) # then remove
# Wrong delimiter
read.delim("file.txt", sep = "\t")
# Encoding issues
read.csv("file.csv", encoding = "UTF-8")
# Trailing whitespace
read.csv("file.csv", strip.white = TRUE)
```
## Saving Plots
### Save as Image
```r
# PNG
png("plot.png", width = 800, height = 600)
ggplot(df, aes(x = x, y = y)) + geom_point()
dev.off()
# JPEG
jpeg("plot.jpg", width = 800, height = 600, quality = 90)
# PDF
pdf("plot.pdf", width = 10, height = 8)
```
### Save with ggsave
```r
library(ggplot2)
p <- ggplot(df, aes(x = x, y = y)) + geom_point()
ggsave("plot.png", p, width = 10, height = 8, dpi = 300)
ggsave("plot.pdf", p, width = 10, height = 8)
ggsave("plot.svg", p)
```
## Summary
- `getwd()` and `setwd()` manage working directory
- `read.csv()` / `read_csv()` for CSV files
- readxl package for Excel files
- `readLines()` for text files
- `saveRDS()` / `readRDS()` for single R objects
- `save()` / `load()` for multiple R objects
- `write.csv()` / `write_csv()` for writing CSV
- `file.exists()` checks if file exists
- `file.path()` builds paths safely
- `ggsave()` for saving ggplot2 plots
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →