RWEP/SD/20240326_1_codestyle/index.qmd

158 lines
2.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
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)
flights|>filter(carrier=="UA",
dest%in%c("IAH","HOU"),sched_dep_time>
0900,sched_arr_time<2000)|>group_by(flight)|>
summarize(delay=mean(
arr_delay,na.rm=TRUE),cancelled=sum(is.na(arr_delay)),
n=n())|>filter(n>10)
```
## quarto
![](../../image/quarto-flow.png)
## 欢迎讨论!{.center}
`r rmdify::slideend(wechat = FALSE, type = "public", tel = FALSE, thislink = "../")`