3 R
R is one of the main programming languages used in Shen Lab. We use R for statistical analysis, data cleaning, visualization, reproducible reports, package development, and many omics-related workflows. This chapter explains how to install R, RStudio, and the basic tools needed for daily work.
3.1 What To Install
New members who will use R should install:
- R;
- RStudio Desktop;
- RTools or command line developer tools, if package compilation is needed;
- Git, so RStudio can work with GitHub repositories;
- commonly used R packages for data analysis and reporting.
If you use a lab server or a shared computing environment, check with your mentor before installing or updating system-level software.
3.2 Installing R
R should be installed before RStudio. RStudio is an editor and integrated development environment; it does not replace R itself.
3.2.1 macOS
Recommended steps:
- Go to the official R project website: https://cran.r-project.org/.
- Choose the macOS download page.
- Download the latest stable R installer for your Mac.
- Open the installer and follow the default steps.
- After installation, open Terminal and check:
Apple Silicon and Intel Macs may have different installers. If you are unsure which one to choose, ask your mentor or the PI before installing.
3.2.2 Windows
Recommended steps:
- Go to https://cran.r-project.org/.
- Choose the Windows download page.
- Select “base” and download the latest R installer.
- Run the installer and use the default options unless you have a specific reason to change them.
- Open Command Prompt or PowerShell and check:
Windows users who need to compile packages from source may also need RTools. Install RTools only when needed, or when a package installation message asks for it.
3.2.3 Linux
On Linux, installation depends on the distribution and whether you are using a personal computer, lab workstation, or server.
For Ubuntu or Debian-based systems, R can usually be installed through the system package manager or from CRAN instructions:
On shared servers, do not use sudo or change system R without permission. Ask the person responsible for the server if you need a specific R version or package environment.
3.3 Installing RStudio
RStudio Desktop is the recommended local IDE for most lab members using R.
Recommended steps:
- Go to the Posit download page: https://posit.co/download/rstudio-desktop/.
- Download RStudio Desktop for your operating system.
- Install it using the default instructions.
- Open RStudio and confirm that it detects your R installation.
- In the RStudio Console, check:
R.version.stringIf RStudio cannot find R, restart your computer first. If the issue remains, check whether R was installed correctly.
3.4 First RStudio Setup
After installing RStudio, configure a few basic settings.
Recommended settings:
- use UTF-8 encoding for source files;
- use spaces instead of tabs;
- show file extensions in your operating system;
- avoid saving
.RDataautomatically when closing a project; - use RStudio Projects for organized work;
- connect RStudio with Git if you will use GitHub.
To avoid hidden workspace state, set RStudio not to automatically save and restore .RData unless a project has a specific reason to do so.
3.5 Installing R Packages
R packages can be installed from CRAN using:
install.packages("tidyverse")Commonly used packages include:
-
tidyversefor data cleaning, transformation, and plotting; -
data.tablefor fast table operations; -
readxlandopenxlsxfor Excel files; -
ggplot2for visualization; -
patchworkorcowplotfor figure layout; -
rmarkdown,knitr, andbookdownfor reports and websites; -
devtools,usethis, androxygen2for package development; -
BiocManagerfor installing Bioconductor packages.
Bioconductor packages should usually be installed through BiocManager:
install.packages("BiocManager")
BiocManager::install("limma")For project-specific analyses, avoid installing packages randomly until you understand the project environment. Some projects may use renv, containers, or server-managed libraries.
3.6 Using R Projects
For each research project, use an RStudio Project when possible. An RStudio Project helps keep file paths, working directories, Git settings, and project files organized.
Recommended project structure:
project-name/
data/
code/
results/
figures/
docs/
project-name.Rproj
Good practice:
- do not use absolute paths that only work on your computer;
- keep raw data separate from processed data;
- write scripts that can be rerun from a clean session;
- use clear file names and dates when appropriate;
- document package versions if the analysis needs to be reproducible.
3.7 Working With Data
When using R for lab projects:
- keep raw data unchanged;
- write scripts for each data-processing step;
- save intermediate results only when they are useful and documented;
- avoid manual spreadsheet edits unless they are recorded clearly;
- make figures from code whenever possible;
- keep analysis outputs in the agreed project folder.
For omics data, pay attention to sample metadata, feature identifiers, missing values, batch information, normalization steps, and quality-control results.
3.8 Reproducibility
A useful R analysis should be understandable and rerunnable by another lab member.
Recommended habits:
- start scripts with a short description of the goal;
- load packages at the top of the script;
- use relative paths inside a project;
- set random seeds when using random procedures;
- save important parameters in code, not only in memory;
- record package versions for analyses used in papers or reports;
- restart R and rerun the analysis before sharing final results.
For important projects, consider using renv to record package versions:
install.packages("renv")
renv::init()
renv::snapshot()Use renv only after discussing with your mentor if the project already has an environment-management strategy.
3.9 Getting Help
If you encounter R or RStudio problems, first collect:
- your operating system;
- R version;
- RStudio version;
- the exact error message;
- the code that produced the error;
- what you already tried.
Then ask your mentor, a relevant lab member, or the PI. Clear error reports make it much easier for others to help.
R 是 Shen Lab 最常用的开发语言之一。我们使用 R 进行统计分析、数据清洗、可视化、可复现报告、R package 开发,以及许多组学数据分析流程。本章介绍如何安装 R、RStudio,以及日常科研工作需要的基础工具。
需要使用 R 的新成员通常应安装:
- R;
- RStudio Desktop;
- 如需编译 R 包,安装 RTools 或 command line developer tools;
- Git,方便 RStudio 与 GitHub repository 协作;
- 常用数据分析和报告相关 R packages。
如果你在实验室服务器或共享计算环境中工作,请先咨询 mentor,不要自行安装或更新系统级软件。
R 应该先于 RStudio 安装。RStudio 是编辑器和集成开发环境,不替代 R 本身。
推荐步骤:
- 打开 R 官方网站:https://cran.r-project.org/。
- 进入 macOS 下载页面。
- 下载适合你电脑的最新稳定版 R installer。
- 打开安装包并按默认步骤安装。
- 安装后在 Terminal 检查:
Apple Silicon 和 Intel Mac 可能对应不同安装包。如果不确定该选哪一个,请先问 mentor 或 PI。
推荐步骤:
- 打开 https://cran.r-project.org/。
- 进入 Windows 下载页面。
- 选择
base并下载最新 R installer。 - 运行安装包,除非有特殊原因,一般使用默认选项。
- 在 Command Prompt 或 PowerShell 检查:
Windows 用户如果需要从源代码编译 R 包,可能还需要 RTools。建议在确实需要时,或安装包提示需要时再安装。
Linux 上的安装方式取决于发行版,以及你是在个人电脑、实验室 workstation 还是服务器上工作。
Ubuntu 或 Debian 系统通常可以通过 package manager 或 CRAN 指南安装:
在共享服务器上,不要未经允许使用 sudo 或修改系统 R。如果需要特定 R 版本或 package environment,请联系服务器负责人。
RStudio Desktop 是实验室成员本地使用 R 的推荐 IDE。
推荐步骤:
- 打开 Posit 下载页面:https://posit.co/download/rstudio-desktop/。
- 下载适合操作系统的 RStudio Desktop。
- 按默认说明安装。
- 打开 RStudio,确认它能识别你的 R 安装。
- 在 RStudio Console 中检查:
R.version.string如果 RStudio 找不到 R,先重启电脑;如果仍有问题,再检查 R 是否正确安装。
安装 RStudio 后,建议完成以下基础设置:
- 源文件使用 UTF-8 编码;
- 使用 spaces 而不是 tabs;
- 在操作系统中显示文件扩展名;
- 关闭自动保存和恢复
.RData; - 使用 RStudio Project 组织项目;
- 如果需要 GitHub 协作,在 RStudio 中配置 Git。
为避免隐藏的 workspace state,除非项目有明确需要,不建议自动保存和恢复 .RData。
CRAN 包可以这样安装:
install.packages("tidyverse")常用 R packages 包括:
-
tidyverse:数据清洗、转换和绘图; -
data.table:快速表格操作; -
readxl和openxlsx:读取和写入 Excel 文件; -
ggplot2:可视化; -
patchwork或cowplot:组合 figures; -
rmarkdown、knitr和bookdown:报告和网站; -
devtools、usethis和roxygen2:R package 开发; -
BiocManager:安装 Bioconductor packages。
Bioconductor packages 通常通过 BiocManager 安装:
install.packages("BiocManager")
BiocManager::install("limma")对于项目特定分析,不要随意向已有环境安装大量 packages。部分项目可能使用 renv、container 或服务器统一管理的 library。
每个研究项目尽量使用一个 RStudio Project。它能帮助管理 working directory、文件路径、Git 设置和项目文件。
推荐结构:
project-name/
data/
code/
results/
figures/
docs/
project-name.Rproj
良好实践:
- 不要使用只在自己电脑上有效的绝对路径;
- raw data 和 processed data 分开;
- 脚本应能从 clean session 重新运行;
- 文件名清楚,必要时加入日期;
- 用于论文或报告的分析应记录 package versions。
使用 R 进行实验室项目分析时:
- raw data 保持不变;
- 每一步数据处理都用脚本记录;
- 只有在有用且有文档说明时才保存中间结果;
- 尽量避免手动编辑 spreadsheet,若编辑必须清楚记录;
- figures 尽量由代码生成;
- 输出结果保存到约定项目文件夹。
组学数据尤其要注意 sample metadata、feature identifiers、missing values、batch information、normalization steps 和 quality-control results。
好的 R 分析应该能被另一位实验室成员理解并重新运行。
建议习惯:
- 脚本开头写清楚目的;
- packages 在脚本开头统一加载;
- 在项目内使用相对路径;
- 涉及随机过程时设置 random seed;
- 重要参数写在代码里,而不是只存在记忆中;
- 论文或报告相关分析记录 package versions;
- 分享最终结果前,重启 R 并重新运行分析。
重要项目可以考虑用 renv 记录 package versions:
install.packages("renv")
renv::init()
renv::snapshot()如果项目已有环境管理方式,使用 renv 前请先和 mentor 讨论。
遇到 R 或 RStudio 问题时,请先整理:
- 操作系统;
- R 版本;
- RStudio 版本;
- 完整错误信息;
- 产生错误的代码;
- 已经尝试过的解决方法。
然后再向 mentor、相关实验室成员或 PI 求助。清楚的错误描述会让别人更容易帮助你。