-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-functions.Rmd
124 lines (83 loc) · 1.88 KB
/
06-functions.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 函数 {#functions}
## 概述
### 测试 {-}
### 题纲 {-}
## 函数基础知识
### 函数构成 {#function-components}
### 原始函数
### 函数的“头等性”
### 调用函数
### 习题
## 函数组合调用 {#function-composition}
## 变量作用域 {#lexical-scoping}
### 变量名屏蔽
### 函数与变量
### 全新的开始
### 动态查询
### 习题
## 延迟计算 {#lazy-evaluation}
### Promise
### 默认参数
### 缺失参数 {#missing-arguments}
### 习题
## `...`(点点点)
```{r}
print(factor(letters), max.levels = 4)
print(y ~ x, showEnv = TRUE)
```
```{r}
mean(1,2,3)
```
```{r}
function (x, trim = 0, na.rm = FALSE, ...)
{
if (!is.numeric(x) && !is.complex(x) && !is.logical(x)) {
warning("argument is not numeric or logical: returning NA")
return(NA_real_)
}
if (na.rm)
x <- x[!is.na(x)]
if (!is.numeric(trim) || length(trim) != 1L)
stop("'trim' must be numeric of length one")
n <- length(x)
if (trim > 0 && n) {
if (is.complex(x))
stop("trimmed means are not defined for complex data")
if (anyNA(x))
return(NA_real_)
if (trim >= 0.5)
return(stats::median(x, na.rm = FALSE))
lo <- floor(n * trim) + 1
hi <- n + 1 - lo
x <- sort.int(x, partial = unique(c(lo, hi)))[lo:hi]
}
.Internal(mean(x))
}
```
```{r}
g09 <- function(x) x + 100
g10 <- function() {
g09 <- 10
g09(g09)
}
g10()
```
```{r}
names(.GlobalEnv)
environment()
```
### 习题
## 退出函数
### 隐式返回与显示返回
### 不可见返回值
### 错误
### 退出处理
### 习题
## 函数的多种形式 {#function-form}
### 重写函数为前置形式 {#prefix-transform}
### 前置函数 {#prefix-form}
### 居中函数
### 替代函数
### 特殊形式函数
### 习题
## 测试题答案 {#function-quiz-answer}