# 第Ⅲ部 プログラム (p225~)

# 「nhn-techorus.datascienceteam / bookreading · GitLab」 https://gitlab.com/nhn-techorus.datascienceteam/bookreading
# 「personal/sakai · master · nhn-techorus.datascienceteam / bookreading · GitLab」 https://gitlab.com/nhn-techorus.datascienceteam/bookreading/tree/master/personal/sakai

# 『Hands-on programming with R』(Garrett Grolemund著、邦題『RStudioではじめるRプログラミング入門』大橋監訳、長尾訳、オライリー・ジャパン、2015)。
# 『Advanced R』(Hadley Wickham著、邦題『R言語徹底解説』石田ほか訳、共立出版、2014)http://adv-r.had.co.nz/

# 14章 magrittrでパイプ (p229~235)

# 「R for Data Science」 http://r4ds.had.co.nz/pipes.html
# 「r4ds-exercise-solutions/pipes.Rmd at master · jrnold/r4ds-exercise-solutions」 https://github.com/jrnold/r4ds-exercise-solutions/blob/master/pipes.Rmd
# 「R for Data Science Solutions」 https://jrnold.github.io/r4ds-exercise-solutions/pipes.html

# 「RPubs - r4ds_ch14」 http://rpubs.com/tocci36/r4ds_ch14

# 14.1 はじめに

# 14.1.1 用意するもの

# install.packages("magrittr")
#install.packages(c("poppr", "mmod", "magrittr", "treemap"), repos = "http://cran.rstudio.com", dependencies = TRUE)
#install.packages(c("magrittr"), repos = "http://cran.rstudio.com", dependencies = TRUE)
#githubinstall::githubinstall('magrittr')
library(magrittr)
## Warning: package 'magrittr' was built under R version 3.3.3
# 14.2 パイプの代用
#install.packages("poppr")
#library("poppr")
little_bunny <- function(){
  "Little bunny Foo Foo"
  "Went hopping through the forest"
  "Scooping up the field mice"
  "And bopping them on the head"
}
foo_foo <- little_bunny()
#??little_bunny
#?magrittr::divide_by

# 14.2.1 中間ステップ

#foo_foo_1 <- hop(foo_foo, through = forest)
#foo_foo_2 <- scoop(foo_foo_1, up = field_mice)
#foo_foo_3 <- bop(foo_foo_2, on = head)

diamonds <- ggplot2::diamonds
diamonds2 <- diamonds %>%
  dplyr::mutate(price_per_carat = price / carat)
## Warning: package 'bindrcpp' was built under R version 3.3.3
pryr::object_size(diamonds)
## 3.46 MB
pryr::object_size(diamonds2)
## 3.89 MB
pryr::object_size(diamonds, diamonds2)
## 3.89 MB
diamonds$carat[1] <- NA
pryr::object_size(diamonds)
## 3.46 MB
pryr::object_size(diamonds2)
## 3.89 MB
pryr::object_size(diamonds, diamonds2)
## 4.32 MB
# 14.2.2 元のオブジェクトを書き換える

#foo_foo <- hop(foo_foo, through = forest)
#foo_foo <- scoop(foo_foo, up = field_mice)
#foo_foo <- bop(foo_foo, on = head)

# 14.2.3 関数作成
# bop(
#   scoop(
#     hop(foo_foo, through = forest),
#     up = field_mice
#   ),
#   on = head
# )

# 14.2.4 パイプを使う
# foo_foo %>%
#   hop(through = forest) %>%
#   scoop(up = field_mouse) %>%
#   bop(on = head)

# my_pipe <- function(.) {
#   . <- hop(., through = forest)
#   . <- scoop(., up = field_mice)
#   bop(., on = head)
# }
# my_pipe(foo_foo)
assign("x", 10)
x
## [1] 10
"x" %>% assign(100)
x
## [1] 10
env <- environment()
"x" %>% assign(100, envir = env)
x
## [1] 100
tryCatch(stop("!"), error = function(e) "An error")
## [1] "An error"
# stop("!") %>%
#   tryCatch(error = function(e) "An error")

# 14.3 パイプを使ってはいけないとき

# 14.4 magrittrの他のツール
rnorm(100) %>%
  matrix(ncol = 2) %>%
  plot() %>%
  str()

##  NULL
rnorm(100) %>%
  matrix(ncol = 2) %T>%
  plot() %>%
  str()

##  num [1:50, 1:2] 1.164 1.749 -0.41 -0.515 0.753 ...
mtcars %$%
  cor(disp, mpg)
## [1] -0.8475514
mtcars <- mtcars %>%
  transform(cyl = cyl * 2)

mtcars %<>% transform(cyl = cyl * 2)

# assignで関数内からグローバル変数にアクセスできる
# 「R-Source」 http://cse.naro.affrc.go.jp/takezawa/r-tips/r/32.html