Advanced R Week 1
這是Advanced R第一周的課程內容整理,很有趣。
FUNCTION
To understand computations in R, two slogans are helpful:
Everything that exists is an object.
Everything that happens is a function call.
— John Chambers
- 你可以把function當作參數傳到另一個fucntion
A Great Function Example!!!!!!!
1 | library(readr) |
Function with default value
課程中改寫上例,滿漂亮的
1 | library(dplyr) |
Refactoring!
而且不用下載檔案兩次了
1 | check_for_logfile <- function(date) { |
Dependency Checking
1 | check_pkg_deps <- function() { |
Vectorization
通常R常見得使用模式要讓FUNCTION吃Vector,這邊告訴我們如何讓R能吃Vector及,最重要的,處理Vector。R這樣得特性,在這個例子中,可以讓我們一次傳入多個package,可以一次傳回多個package的資訊。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15num_download <- function(pkgname, date = "2016-07-20") {
check_pkg_deps()
dest <- check_for_logfile(date)
cran <- read_csv(dest, col_types="ccicccccci", progress=FALSE)
cran %>% filter(package %in% pkgname) %>%
group_by(package) %>%
summarize(n=n())
}
# num_download 原本傳回integer,現在傳回data.frame
num_download(c("filehash", "weathermetrics"))
# A tibble: 2 × 2
package n
<chr> <int>
1 filehash 179
2 weathermetrics 7
Argument Checking
hmmm, this is argument checking using is.character
and length
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18num_download <- function(pkgname, date = "2016-07-20") {
check_pkg_deps()
## Check arguments
if(!is.character(pkgname))
stop("'pkgname' should be character")
if(!is.character(date))
stop("'date' should be character")
if(length(date) != 1)
stop("'date' should be length 1")
dest <- check_for_logfile(date)
cran <- read_csv(dest, col_types = "ccicccccci",
progress = FALSE)
cran %>% filter(package %in% pkgname) %>%
group_by(package) %>%
summarize(n = n())
}
When Sould I Write a Function
這個心法滿不錯的,先用code
解決當前問題,再用function
進行模組化以利處理廣泛一些的問題,過程中會持續重構,最後改用package
做統整。
Developing functions is a key aspect of programming in R and typically involves a bottom-up process.
- Code is written to accomplish a specific task or a specific instance of a task.
- The code is examined to identify key aspects that may be modified by other users; these aspects are abstracted out of the code and made arguments of a function.
- Functions are written to accomplish more general versions of a task; specific instances of the task are indicated by setting values of function arguments.
- Function code can be re-factored to provide better modularity and to divide functions into specific sub-tasks.
- Functions can be assembled and organized into R packages.
實作部份的摘要
1 | evalute <- function(func,args) { |
paste (..., sep = " ", collapse = NULL)
在 ellipsis之後出現得argument都必需要有default value.
ellipses
1 | telegram <- function(...) { |
1 | mad_lib <- function(...) { |
self-maded binary operator
1 | "%mult_add_one%" <- function(left, right){ |