Skip to content

Commit 3a644f8

Browse files
committed
Update slides: add images, content and style tweaks
Add three slide figures (positron.png, quarto.jpg, rstudio.png) and update slide content and layout across slides-r-basics.qmd and slides-rmarkdown.qmd. Bump the R basics slide date, reorganize and add many subsection headings, fix/standardize code block markers, insert Hadley/Posit/Quarto visuals and links, adjust inline knitr usage and small-sized headings, and add Positron/RStudio informational content. Tidy up wording (e.g. tidyverse link) and minor content cleanups. Remove the decorative quote pseudo-element from slides.scss to adjust slide styling.
1 parent e67dbd8 commit 3a644f8

6 files changed

Lines changed: 131 additions & 54 deletions

File tree

371 KB
Loading
185 KB
Loading
792 KB
Loading

r-and-rmarkdown/slides/slides-r-basics.qmd

Lines changed: 91 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "R语言基础"
33
subtitle: "R语言在生物信息学中的应用"
44
author: "王诗翔 副教授<br>中南大学生物医学信息系"
5-
date: "2025"
5+
date: "2026-03-19"
66
format:
77
revealjs:
88
theme: [default, ../styles/slides.scss]
@@ -15,6 +15,8 @@ format:
1515
height: 720
1616
margin: 0.1
1717
logo: ../logo.png
18+
#https://stackoverflow.com/questions/74404680/rescaling-a-figure-in-quarto
19+
#auto-stretch: false
1820
footer: "R语言基础 | 中南大学"
1921
execute:
2022
echo: true
@@ -64,7 +66,7 @@ showtext::showtext_auto()
6466
> 目的不是培养程序员,而是用数据推动学科发展
6567
:::
6668
::: {.column width="45%"}
67-
### Hadley Wickham的数据科学流程
69+
### 数据科学流程
6870
1. 数据导入
6971
2. 数据规整(清洗)
7072
3. 数据处理
@@ -80,10 +82,12 @@ showtext::showtext_auto()
8082

8183
> 1992年,新西兰奥克兰大学 **Ross Ihaka****Robert Gentleman** 开发了R语言
8284
85+
- 🌐 官网:<https://www.r-project.org/>
86+
8387
::: {.columns}
8488
::: {.column width="50%"}
8589
### 核心特点
86-
- 🌐 官网:<https://www.r-project.org/>
90+
8791
- 📊 统计计算与图形可视化
8892
- 💻 跨平台:Windows / Mac / Linux
8993
- 💰 开源免费(GPL协议)
@@ -108,6 +112,8 @@ showtext::showtext_auto()
108112
::: {.column width="55%"}
109113
### Hadley Wickham
110114

115+
![](https://hadley.nz/hadley.jpg){width=30%}
116+
111117
2019年荣获**考普斯总统奖**(统计学诺贝尔奖)
112118

113119
- 创建了 **tidyverse** 生态系统
@@ -124,6 +130,7 @@ library(tidyverse)
124130
```
125131

126132
核心包:
133+
127134
- `dplyr` — 数据处理
128135
- `ggplot2` — 数据可视化
129136
- `tidyr` — 数据整理
@@ -132,7 +139,7 @@ library(tidyverse)
132139
:::
133140
:::
134141

135-
> 官网<https://www.tidyverse.org/>
142+
tidyverse官网<https://www.tidyverse.org/>
136143

137144
---
138145

@@ -168,6 +175,21 @@ library(tidyverse)
168175
:::
169176
:::
170177

178+
## 1.4 安装R和RStudio
179+
180+
### RStudio
181+
182+
![](figures/rstudio.png)
183+
184+
185+
## 1.5 Positron - The Data Science IDE
186+
187+
- <https://positron.posit.co/>
188+
189+
::: {.columns}
190+
![](figures/positron.png){width="70%"}
191+
:::
192+
171193
---
172194

173195
# 第2部分:R对象
@@ -184,6 +206,8 @@ library(tidyverse)
184206
sqrt(16)
185207
```
186208

209+
## 2.1 一切皆对象
210+
187211
### 查看对象类型
188212

189213
```{r}
@@ -210,13 +234,6 @@ gene_name
210234
expression
211235
```
212236

213-
```{r}
214-
# 查看类型
215-
class(expression)
216-
typeof(sample_count)
217-
length(gene_name)
218-
```
219-
220237
---
221238

222239
## 2.3 变量命名规则
@@ -265,6 +282,8 @@ class(x) # 对象类型
265282
length(x) # 元素个数
266283
```
267284

285+
## 2.5 属性与赋值
286+
268287
```{r}
269288
# 属性赋值
270289
names(x) <- c("S1", "S2", "S3", "S4")
@@ -318,9 +337,7 @@ rep(c("Tumor", "Normal"), each = 3)
318337

319338
---
320339

321-
## 3.2 向量类型详解
322-
323-
### 数值型:integer vs double
340+
## 3.2 向量类型详解 - 数值型:integer vs double
324341

325342
```{r}
326343
x_int <- 42L # integer(整数,后缀L)
@@ -331,7 +348,7 @@ typeof(x_dbl)
331348
is.integer(x_int)
332349
```
333350

334-
### 字符串型(String)
351+
## 3.2 向量类型详解 - 字符串型(String)
335352

336353
```{r}
337354
gene <- "TP53" # 双引号或单引号
@@ -353,6 +370,8 @@ levels(stage)
353370
table(stage)
354371
```
355372

373+
## 3.3 因子型向量(factor)
374+
356375
```{r}
357376
# 有序因子(如温度等级)
358377
severity <- factor(c("mild", "severe", "moderate", "mild"),
@@ -389,12 +408,16 @@ character > numeric > logical
389408
double > integer
390409
```
391410

411+
## 3.5 强制类型转换
412+
392413
```{r}
393414
# 混合类型会自动转换
394415
c(1, "USA", TRUE) # 全部变为字符型
395416
c(1, TRUE, FALSE) # 逻辑→数值
396417
```
397418

419+
## 3.5 强制类型转换
420+
398421
```{r}
399422
# 显式转换
400423
as.numeric("3.14")
@@ -438,6 +461,8 @@ colnames(expr_mat) <- c("S1", "S2", "S3", "S4")
438461
expr_mat
439462
```
440463

464+
## 4.2 矩阵(matrix)
465+
441466
```{r}
442467
dim(expr_mat) # 维度
443468
nrow(expr_mat) # 行数
@@ -459,6 +484,12 @@ result <- list(
459484
is_DE = TRUE
460485
)
461486
487+
result
488+
```
489+
490+
## 4.3 列表(list)
491+
492+
```{r}
462493
# 访问元素
463494
result$gene
464495
result[["p_value"]]
@@ -493,13 +524,17 @@ str(patients)
493524
dim(patients)
494525
```
495526

527+
## 4.5 数据框操作
528+
496529
```{r}
497530
# 访问列(三种方式等价)
498531
patients$age
499532
patients[["age"]]
500533
patients[, "age"]
501534
```
502535

536+
## 4.5 数据框操作
537+
503538
```{r}
504539
# 筛选行
505540
patients[patients$age > 50, ]
@@ -512,7 +547,7 @@ subset(patients, stage == "III" | stage == "IV")
512547

513548
## 5.1 算术运算符
514549

515-
```{r}
550+
```r
516551
x <- c(10, 20, 30, 40)
517552
y <- c(2, 4, 5, 8)
518553

@@ -539,6 +574,8 @@ c(1, 2, 3, 4) * 2
539574
c(1, 2, 3, 4) + c(10, 20) # c(10,20)补齐为c(10,20,10,20)
540575
```
541576

577+
## 5.2 循环补齐原则
578+
542579
```{r}
543580
# ⚠️ 非整数倍时R会警告
544581
c(1, 2, 3) + c(10, 20)
@@ -548,13 +585,20 @@ c(1, 2, 3) + c(10, 20)
548585

549586
## 5.3 关系运算符
550587

551-
```{r}
588+
```r
552589
x <- c(8.5, 12.3, 6.7, 15.2, 9.1)
553590

554591
x > 10 # 大于
555592
x >= 10 # 大于等于
556593
x == 9.1 # 等于(注意是==)
557594
x != 9.1 # 不等于
595+
```
596+
597+
598+
## 5.3 关系运算符
599+
600+
```{r}
601+
x <- c(8.5, 12.3, 6.7, 15.2, 9.1)
558602
559603
# 找出高表达基因的位置
560604
which(x > 10)
@@ -565,7 +609,7 @@ x[x > 10]
565609

566610
## 5.4 逻辑运算符
567611

568-
```{r}
612+
```r
569613
a <- c(TRUE, TRUE, FALSE, FALSE)
570614
b <- c(TRUE, FALSE, TRUE, FALSE)
571615

@@ -574,6 +618,8 @@ a | b # 元素级 OR(至少一个TRUE)
574618
!a # 取反
575619
```
576620

621+
## 5.4 逻辑运算符
622+
577623
```{r}
578624
# 实际应用:多条件筛选
579625
x <- c(8.5, 12.3, 6.7, 15.2, 9.1)
@@ -592,7 +638,11 @@ x[x > 8 & x < 13] # 中等表达水平
592638
593639
# NaN:非数(Not a Number)
594640
0 / 0; log(-1)
641+
```
595642

643+
## 5.5 特殊值
644+
645+
```{r}
596646
# NA:缺失值(Not Available)
597647
x <- c(8.5, NA, 12.3, NA, 6.7)
598648
is.na(x)
@@ -640,6 +690,8 @@ if (expr_mean > 15) {
640690
}
641691
```
642692

693+
## 6.1 if 语句
694+
643695
```{r}
644696
# ifelse:向量化条件判断
645697
x <- c(8.5, 12.3, 6.7, 15.2, 9.1)
@@ -663,6 +715,8 @@ for (i in seq_along(genes)) {
663715
}
664716
```
665717

718+
## 6.2 for 循环
719+
666720
```{r}
667721
# break:找到第一个高表达基因后退出
668722
for (i in seq_along(expr)) {
@@ -713,6 +767,8 @@ mean(x); median(x); sd(x); var(x)
713767
min(x); max(x); sum(x); length(x)
714768
```
715769

770+
## 7.1 内置函数
771+
716772
```{r}
717773
# 向量化函数(返回等长向量)
718774
sqrt(x)
@@ -731,6 +787,8 @@ round(c(3.14159, 2.71828), digits = 2)
731787
| **汇总函数** | 向量(n个元素)| 标量(1个元素)| `mean()`, `sum()` |
732788
| **其他** | 向量 | 不等长向量 | `unique()`, `table()` |
733789

790+
## 7.2 向量化函数 vs 汇总函数
791+
734792
```{r}
735793
x <- c(2, 7, 8, 9, 3)
736794
x ^ 2 + 5 # 向量化运算
@@ -757,6 +815,8 @@ my_std(x)
757815
my_std(y)
758816
```
759817

818+
## 7.3 自定义函数
819+
760820
```{r}
761821
# 带默认参数的函数
762822
analyze_expr <- function(expr, threshold = 10, log2_transform = FALSE) {
@@ -812,7 +872,11 @@ names(x) <- c("S1", "S2", "S3", "S4", "S5")
812872
813873
# 位置索引(R从1开始!)
814874
x[1]; x[c(1, 3, 5)]; x[2:4]
875+
```
876+
877+
## 8.2 向量取子集
815878

879+
```{r}
816880
# 逻辑索引
817881
x[x > 10]
818882
@@ -834,6 +898,8 @@ expr_mat[, 1:2] # 前2列(S1, S2)
834898
expr_mat["KRAS", "S2"] # 按名字
835899
```
836900

901+
## 8.3 矩阵取子集
902+
837903
```{r}
838904
# 矩阵运算
839905
apply(expr_mat, 1, mean) # 每行均值(1=行)
@@ -851,6 +917,8 @@ result[[1]] # 返回元素本身
851917
result$gene # 等价于result[["gene"]]
852918
```
853919

920+
## 8.4 列表与数据框取子集
921+
854922
```{r}
855923
# 数据框取子集
856924
patients[1:3, ] # 前3行
@@ -918,15 +986,15 @@ height <- rnorm(20, mean = 170, sd = 20)
918986
- 📖 王敏杰,《数据科学中的R语言》
919987
<https://bookdown.org/wangminjie/R4DS/>
920988

921-
- ⚡ Hadley Wickham,*R for Data Science*(第2版)
922-
<https://r4ds.hadley.nz/>
989+
- ⚡ Hadley Wickham,*R for Data Science*(第2版)
990+
991+
<https://r4ds.hadley.nz/>
992+
993+
<https://sunpast.github.io/r4ds/>(丁加笔记📒)
923994

924995
- 📋 Posit R Cheatsheets(速查表)
925996
<https://posit.co/resources/cheatsheets/>
926997

927-
- 🎨 Carbon:漂亮地展示代码
928-
<https://carbon.now.sh/>
929-
930998
---
931999

9321000
## 下节预告

0 commit comments

Comments
 (0)