Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
R 4.1.0で導入された パイプ演算子 (|>)の紹介 / r_native_pipe
Search
Uryu Shinya
May 30, 2021
Programming
4
6.2k
R 4.1.0で導入された パイプ演算子 (|>)の紹介 / r_native_pipe
パイプ演算子の処理をmagrittrパッケージが提供するものと比較しながら解説します。
Uryu Shinya
May 30, 2021
Tweet
Share
More Decks by Uryu Shinya
See All by Uryu Shinya
Rの機械学習フレームワークの紹介〜tidymodelsを中心に〜 / machine_learning_with_r2024
s_uryu
0
220
地理空間データの機械学習への適用 / machine_learning_for_spatial_data
s_uryu
0
39
mandaRa: R言語ユーザのための新しい知識共有の場 / mandara_tokyor111
s_uryu
2
430
R言語入門 (R-4.3.3 2024年4月版) / introduction to r
s_uryu
8
5.2k
統・再現性・協力: 人為的過誤を防ぎ、未来へ進む策 / Integration, Reproducible, and Collaboration
s_uryu
1
590
Rによる大規模データの処理
s_uryu
2
1.7k
textlintr: Rユーザのためのtextlintによる校正
s_uryu
0
440
Rによるデータ可視化と地図表現
s_uryu
1
2k
データ分析入門 / tokupon-ds2022
s_uryu
4
5.3k
Other Decks in Programming
See All in Programming
FigmaとPHPで作る1ミリたりとも表示崩れしない最強の帳票印刷ソリューション
ttskch
43
19k
Git Rebase
bkuhlmann
11
1.6k
Site Reliability Engineering for GMO
pyama86
7
1k
Elm Form Validation
bkuhlmann
0
510
try! Swift Tokyo 2024 参加報告 / try! Swift Tokyo 2024 Report
hironytic
0
200
Ruby GitHub Packages
bkuhlmann
0
630
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
640
코틀린으로 멀티플랫폼 만들기
pangmoo
0
150
見た目から始める生産性向上
ikumatadokoro
7
820
MetricKitで予期せぬ終了を検知する話 / Detect unexpected termination with MetricKit
nekowen
1
180
単体テストを書かない技術 #phpcon_odawara
o0h
PRO
26
8.2k
try! Swift Tokyo 2024のLT枠に採択されたプロポーザルを出すときに考えていたこと
ski
0
350
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
289
19k
Scaling GitHub
holman
457
140k
Agile that works and the tools we love
rasmusluckow
325
20k
Why You Should Never Use an ORM
jnunemaker
PRO
51
8.6k
Fantastic passwords and where to find them - at NoRuKo
philnash
37
2.5k
Designing for humans not robots
tammielis
248
25k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
241
1.2M
YesSQL, Process and Tooling at Scale
rocio
164
13k
Into the Great Unknown - MozCon
thekraken
10
990
Building a Modern Day E-commerce SEO Strategy
aleyda
17
6.4k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
Design by the Numbers
sachag
274
18k
Transcript
Ͱಋೖ͞Εͨ ύΠϓԋࢉࢠ c ͷհ Shinya Uryu @u_ribo uribo
Rにおける連続した処理の記述 パイプ演算⼦の使い⽅〜magrittrパッケージのパイプ演算⼦(%>%)との⽐較〜 2種類のパイプ、どちらを使う? RStudioでのパイプ演算⼦の利⽤ ༰ 1 2 3 4
3ʹ͓͚Δ࿈ଓͨ͠ॲཧͷهड़⁞ r <- rnorm(100) d <- matrix(r, ncol = 2)
plot(d) 処理ごとにオブジェクトへ保存する 中間オブジェクトを残して結果を参照できる …不要な中間オブジェクトを発⽣させる可能性もある 処理の流れ
3ʹ͓͚Δ࿈ଓͨ͠ॲཧͷهड़ plot( matrix( rnorm(100), ncol = 2)) 処理内容を⼊れ⼦構造で記述する 内側の結果が外側の関数に渡される ⼊⼒の⾯倒さ(処理順とは逆に⼊⼒する必要)
複雑な⼊れ⼦構造だとコードの可読性の低下 処理の流れ
3ʹ͓͚Δ࿈ଓͨ͠ॲཧͷهड़ library(magrittr) rnorm(100) %>% matrix(ncol = 2) %>% plot() パイプ演算⼦(%>%)を使う
パイプから⾒て左辺の値を右辺の値(関数)に与える 改⾏と字下げを⾏うことでコードの可読性も⾼まる 処理の流れ
ύΠϓԋࢉࢠͷ͍ํ ʙNBHSJUUSύοέʔδͷύΠϓԋࢉࢠ ͱͷൺֱʙ x %>% f() R 4.1.0 で導⼊された
|> (組み込みパイプ) x |> f() magrittrパッケージが提供する %>% f(x) と等価 左辺の値を右辺の第⼀引数に渡す x f()
ύΠϓԋࢉࢠͷ͍ํ ʙNBHSJUUSύοέʔδͷύΠϓԋࢉࢠ ͱͷൺֱʙ 右辺の扱いの違い magrittr … 関数、関数オブジェクトどちらもOK 組み込み …
関数呼び出しでなければいけない x %>% f x |> f #> Error: The pipe operator requires a function call as RHS
ύΠϓԋࢉࢠͷ͍ํ ʙNBHSJUUSύοέʔδͷύΠϓԋࢉࢠ ͱͷൺֱʙ 左辺の値を第⼀引数以外に渡すためのplace holder magrittr … 「.」を使う 組み込み
…デフォルトではplace holderを使えない x %>% f(..., x = .) x |> f(..., x = .) #> Error in is.data.frame(data) : object '.' not found
ύΠϓԋࢉࢠͷ͍ํ ʙNBHSJUUSύοέʔδͷύΠϓԋࢉࢠ ͱͷൺֱʙ 関数を定義 してplace holderを実現する my_lm <- function(x)
{ lm(mpg ~ cyl, data = x) } mtcars |> my_lm() または無名関数を利⽤
無名関数を利⽤してplace holderを実現する ύΠϓԋࢉࢠͷ͍ํ ʙNBHSJUUSύοέʔδͷύΠϓԋࢉࢠ ͱͷൺֱʙ mtcars |> (function(x) {
lm(mpg ~ cyl, data = x) })() 関数を定義または mtcars |> (\(x) lm(mpg ~ disp, data = x))() mtcars |> (\(passed_data) lm(mpg ~ disp, data = passed_data))() \(x) もR4.1.0で導⼊
試験中の機能? ύΠϓԋࢉࢠͷ͍ํ ʙNBHSJUUSύοέʔδͷύΠϓԋࢉࢠ ͱͷൺֱʙ Sys.setenv(`_R_USE_PIPEBIND_` = TRUE) mtcars |>
. => lm(mpg ~ disp, data = .) => の左辺「.」が右辺の処理内で「.」として渡される 組み込みパイプ処理でもplace holderとして機能する
ύΠϓԋࢉࢠͷ͍ํ ʙNBHSJUUSύοέʔδͷύΠϓԋࢉࢠ ͱͷൺֱʙ 実⾏速度では組み込み > magrittr ユーザが違いを認識できるほどではない https://www.tidyverse.org/blog/2020/11/magrittr-2-0-is-here/ f1
<- function(x) x f2 <- function(x) x f3 <- function(x) x f4 <- function(x) x p <- bench::mark( `1` = NULL %>% f1(), `4` = NULL %>% f1() %>% f2() %>% f3() %>% f4(), `1_native` = NULL |> f1(), `4_native` = NULL |> f1() |> f2() |> f3() |> f4()) 組み込み 組み込み magrittr magrittr
34UVEJPͰͷύΠϓԋࢉࢠͷར༻ ショートカット ⌘Command ⇧Shift M ^Control ⇧Shift M Windows +
+ + +
34UVEJPͰͷύΠϓԋࢉࢠͷར༻ 組み込みパイプのショートカットを利⽤するには 👉 ※2021年5⽉30⽇現在は Preview, Daily Buildでのみ利⽤可能 メニュー Preferences... Code
|> |> の順に選択した画⾯ チェック
छྨͷύΠϓɺͲͪΒΛ͏ magrittr (%>%) 組み込み (|>) R 4.1.0以上縛り パッケージのインストール 導⼊しやすさ 実⾏速度
place holder magrittrをImportしたパッケージが多数 知名度 v2.0で改善 magrittrより⾼速 「.」を指定する 関数を定義する 特にtidyverseユーザ ⾼い まだ低い