---
title: "代码编写规则"
format:
  dwsd-revealjs:
    logo: _extensions/drwater/dwsd/inst/ucaslogo.png
---


## 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 = "../"
)
```

