Files
su2026rwep/SD/2.1_codestyle/index.qmd
T
2026-05-21 12:48:42 +08:00

145 lines
2.0 KiB
Plaintext

---
title: "代码编写规则"
format:
dwsd-revealjs:
logo: _extensions/drwater/dwsd/inst/ucaslogo.png
---
```{r}
#| include: false
#| cache: false
lang <- "cn"
require(tidyverse)
require(learnr)
knitr::opts_chunk$set(echo = TRUE)
```
## tidy data
```{r}
knitr::include_graphics("../../image/tidy-1.png", dpi = 270)
```
## pipe(管道) |>
```{r}
#| eval: false
require(patchwork)
plot(1:10)
1:10 |> plot()
plot(x = 1:10, y = sin(1:10))
1:10 |> plot(y = sin(1:10))
```
```{r}
#| echo: false
#| layout-nrow: 1
#| fig-width: 4
#| fig-height: 3
#| out-height: 90%
require(patchwork)
plot(1:10)
1:10 |> plot()
plot(x = 1:10, y = sin(1:10))
1:10 |> plot(y = sin(1:10))
```
## pipe(管道):%>%
```{r}
#| eval: false
#| layout-nrow: 1
#| fig-width: 3
#| fig-height: 4
#| out-height: 125%
require(magrittr)
1:10 %>% plot()
1:10 %>% plot(y = sin(1:10))
sin(1:10) %>% plot(1:10, .)
sin(1:10) |> plot(x = 1:10, y = _)
```
```{r}
#| echo: false
#| layout-nrow: 1
#| fig-width: 3
#| fig-height: 4
#| out-height: 125%
require(magrittr)
1:10 %>% plot()
1:10 %>% plot(y = sin(1:10))
sin(1:10) %>% plot(1:10, .)
sin(1:10) |> plot(x = 1:10, y = _)
```
## 代码编写规则
```{r}
#| eval: false
# Strive for:
short_flights <- flights |> filter(air_time < 60)
# Avoid:
SHORTFLIGHTS <- flights |> filter(air_time < 60)
# Strive for
z <- (a + b)^2 / d
# Avoid
z <- (a + b)^2 / d
# Strive for
mean(x, na.rm = TRUE)
# Avoid
mean(x, na.rm = TRUE)
```
## 练习
```{r}
#| eval: false
flights |>
filter(dest == "IAH") |>
group_by(year, month, day) |>
summarize(n = n(), delay = mean(arr_delay, na.rm = TRUE)) |>
filter(n > 10)
```
## 练习
```{r}
#| eval: false
flights |>
filter(dest == "IAH") |>
group_by(year, month, day) |>
summarize(n = n(), delay = mean(arr_delay, na.rm = TRUE)) |>
filter(n > 10)
```
## quarto
![](../../image/quarto-flow.png)
## 欢迎讨论!{.center}
```{r}
#| results: 'asis'
rmdify::slideend(
wechat = FALSE,
type = "public",
tel = FALSE,
thislink = "../"
)
```