2024-03-25 21:48:30 +08:00
|
|
|
|
---
|
|
|
|
|
title: "代码编写规则"
|
|
|
|
|
subtitle: 《区域水环境污染数据分析实践》<br>Data analysis practice of regional water environment pollution
|
|
|
|
|
author: 苏命、王为东<br>中国科学院大学资源与环境学院<br>中国科学院生态环境研究中心
|
|
|
|
|
date: today
|
|
|
|
|
lang: zh
|
|
|
|
|
format:
|
|
|
|
|
revealjs:
|
|
|
|
|
theme: dark
|
|
|
|
|
slide-number: true
|
|
|
|
|
chalkboard:
|
|
|
|
|
buttons: true
|
|
|
|
|
preview-links: auto
|
|
|
|
|
lang: zh
|
|
|
|
|
toc: true
|
|
|
|
|
toc-depth: 1
|
|
|
|
|
toc-title: 大纲
|
|
|
|
|
logo: ./_extensions/inst/img/ucaslogo.png
|
|
|
|
|
css: ./_extensions/inst/css/revealjs.css
|
|
|
|
|
pointer:
|
|
|
|
|
key: "p"
|
|
|
|
|
color: "#32cd32"
|
|
|
|
|
pointerSize: 18
|
|
|
|
|
revealjs-plugins:
|
|
|
|
|
- pointer
|
|
|
|
|
filters:
|
|
|
|
|
- d2
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```{r}
|
|
|
|
|
#| echo: false
|
|
|
|
|
knitr::opts_chunk$set(echo = TRUE)
|
|
|
|
|
# source("../../coding/_common.R")
|
|
|
|
|
library(tidyverse)
|
|
|
|
|
library(nycflights13)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 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)
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2024-03-26 16:58:06 +08:00
|
|
|
|
## 练习
|
|
|
|
|
|
|
|
|
|
```{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)
|
|
|
|
|
|
|
|
|
|
```
|
2024-03-25 21:48:30 +08:00
|
|
|
|
|
|
|
|
|
## quarto
|
|
|
|
|
|
|
|
|
|
![](../../image/quarto-flow.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 欢迎讨论!{.center}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
`r rmdify::slideend(wechat = FALSE, type = "public", tel = FALSE, thislink = "../")`
|
|
|
|
|
|