Efficient work with RStudio projects and RMarkdown

Опубликовано: 23 Июль 2026
на канале: Stats Pilot
545
13

This is an excerpt from my presentation on efficient organization and keeping track of project-related files in RStudio, including RMarkdown. See chapters and code below.

00:00 General tips
00:40 RStudio settings
02:42 Create RStudio project
04:04 Project folders, files
06:50 Data loading, save CSV
10:25 Data processing, save RDS or RData
15:03 Load RDS or RData
16:05 Plots for publications vector or raster
21:13 Report in RMarkdown
24:34 R code and output in-line
25:10 Some settings of RMarkdown chunks

R code
dataraw ----
Thompson SK. 2012. Sampling. 3rd ed. Wiley.
weather = read.table(file = "https://www.stat.sfu.ca/~thompson/dat...")
head(weather)
names(weather) = c("Day", "MaxTemp_C", "MinTemp_C", "MeanTemp_C",
"HDD", "CDD",
"Rain_mm", "Snow_cm", "Precip_mm")
write.csv(weather, file = "./dataraw/weather.csv", row.names = FALSE)

dataderived ----
D = read.csv("./dataraw/weather.csv")
summary(D)
D$Snow_mm = D$Snow_cm * 10
summary(D)
saveRDS(D, file = "./dataderived/weather.rds")
saveRDS(D, file = paste0("./dataderived/weather_", Sys.Date(), ".rds"))
W = readRDS("./dataderived/weather_2021-11-09.rds")

save.image(file = "./dataderived/image_weather.rdata")
load("./dataderived/image_weather.rdata")


Plots ----
cairo_pdf("./figures/fig_temp.pdf", width = 8, height = 6) # embedded fonts; across platforms
plot.ts(D[, 2:4], lwd = 2)
dev.off()