# 第 38 章 Tidyverse代码书写规范

## 38.1 书写规范的重要性

mpg %>%
filter(cty > 10, class == "compact")

mpg %>% filter(cty > 10, class == "compact")

mpg %>%
filter(cty > 10,
class == "compact")

mpg %>% filter(cty>10, class=="compact")

filter(mpg,cty>10,class=="compact")

mpg %>%
filter(cty > 10,
class == "compact")

filter ( mpg,cty>10,     class=="compact" )

## 38.2 赋值符号

• 赋值的时候用 <-
• 在函数中传递参数的时候用 =

round(x = 3.1415, digits = 2)
## [1] 3.14
x
## Error in eval(expr, envir, enclos): object 'x' not found

round(x <- 3.1415,  digits <- 2)
## [1] 3.14
x
## [1] 3.1415
digits
## [1] 2

## 38.3 空格

### 38.3.1 赋值符号前后要有空格

<- 前后要有空格，避免混淆。比如x<-2，有可能 x <- 2 也有可能是 x < -2 第一个是赋值 2 给x，第二个是比较x是否小于 -2

### 38.3.2 在逗号之后要有空格

# Good
filter(mpg, cty > 10)

# Bad
filter(mpg , cty > 10)
filter(mpg ,cty > 10)
filter(mpg,cty > 10)

### 38.3.3 在+, -, >, =等算符前后要有空格

# Good
filter(mpg, cty > 10)

# Bad
filter(mpg, cty>10)
filter(mpg, cty> 10)
filter(mpg, cty >10)

### 38.3.4 函数括号的前后不要有空格

# Good
filter(mpg, cty > 10)

# Bad
filter (mpg, cty > 10)
filter ( mpg, cty > 10)
filter( mpg, cty > 10 )

## 38.4 换行

# Good
filter(mpg, cty > 10, class == "compact")

# Good
filter(mpg, cty > 10,
class == "compact")

# Good
filter(mpg,
cty > 10,
class == "compact")

# Bad
filter(mpg, cty > 10, class %in% c("compact", "pickup", "midsize", "subcompact", "suv", "2seater", "minivan"))

# Good
filter(mpg,
cty > 10,
class %in% c("compact", "pickup", "midsize", "subcompact",
"suv", "2seater", "minivan"))

## 38.5 管道符号( %>% )和ggplot图层叠加(+)

ggplot2每个图层的语句要单独一行，缩进两个空格，+ 位于一行的末尾

# Good
ggplot(mpg, aes(x = cty, y = hwy, color = class)) +
geom_point() +
geom_smooth() +
theme_bw()

# Bad
ggplot(mpg, aes(x = cty, y = hwy, color = class)) +
geom_point() + geom_smooth() +
theme_bw()

# Super bad
ggplot(mpg, aes(x = cty, y = hwy, color = class)) + geom_point() + geom_smooth() + theme_bw()

# Super bad and won't even work
ggplot(mpg, aes(x = cty, y = hwy, color = class))
+ geom_point()
+ geom_smooth()
+ theme_bw()

# Good
mpg %>%
filter(cty > 10) %>%
group_by(class) %>%
summarize(avg_hwy = mean(hwy))

# Bad
mpg %>% filter(cty > 10) %>% group_by(class) %>%
summarize(avg_hwy = mean(hwy))

# Super bad
mpg %>% filter(cty > 10) %>% group_by(class) %>% summarize(avg_hwy = mean(hwy))

# Super bad and won't even work
mpg %>%
filter(cty > 10)
%>% group_by(class)
%>% summarize(avg_hwy = mean(hwy))

## 38.6 注释

# Good

#Bad

#Bad

mpg %>%
filter(cty > 10) %>%  # Only rows where cty is 10 +
group_by(class) %>%  # Divide into class groups
summarize(avg_hwy = mean(hwy))  # Find the average hwy in each group

mpg %>%
filter(cty > 10) %>%            # Only rows where cty is 10 +
group_by(class) %>%             # Divide into class groups
summarize(avg_hwy = mean(hwy))  # Find the average hwy in each group

# Good
# Happy families are all alike; every unhappy family is unhappy in its own way.
# Everything was in confusion in the Oblonskys’ house. The wife had discovered
# that the husband was carrying on an intrigue with a French girl, who had been
# a governess in their family, and she had announced to her husband that she
# could not go on living in the same house with him. This position of affairs
# had now lasted three days, and not only the husband and wife themselves, but
# all the members of their family and household, were painfully conscious of it.

# Bad
# Happy families are all alike; every unhappy family is unhappy in its own way. Everything was in confusion in the Oblonskys’ house. The wife had discovered that the husband was carrying on an intrigue with a French girl, who had been a governess in their family, and she had announced to her husband that she could not go on living in the same house with him. This position of affairs had now lasted three days, and not only the husband and wife themselves, but all the members of their family and household, were painfully conscious of it.

## 38.7 偷懒

## install.packages("styler")