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
8.1k
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
生成AIサービスを用いた研究活動の支援
s_uryu
0
95
R研究集会(2024)のご案内
s_uryu
1
590
生成AIを用いたサービスの紹介
s_uryu
0
39
生成AIの基礎的事項と社会に与える影響
s_uryu
0
23
Rの機械学習フレームワークの紹介〜tidymodelsを中心に〜 / machine_learning_with_r2024
s_uryu
0
840
地理空間データの機械学習への適用 / machine_learning_for_spatial_data
s_uryu
0
230
mandaRa: R言語ユーザのための新しい知識共有の場 / mandara_tokyor111
s_uryu
2
650
R言語入門 (R-4.3.3 2024年4月版) / introduction to r
s_uryu
7
6.3k
統・再現性・協力: 人為的過誤を防ぎ、未来へ進む策 / Integration, Reproducible, and Collaboration
s_uryu
1
720
Other Decks in Programming
See All in Programming
Generating OpenAPI schema from serializers throughout the Rails stack - Kyobashi.rb #5
envek
1
340
なぜイベント駆動が必要なのか - CQRS/ESで解く複雑系システムの課題 -
j5ik2o
14
4.5k
Flutter × Firebase Genkit で加速する生成 AI アプリ開発
coborinai
0
170
Honoのおもしろいミドルウェアをみてみよう
yusukebe
1
220
苦しいTiDBへの移行を乗り越えて快適な運用を目指す
leveragestech
0
930
ナレッジイネイブリングにAIを活用してみる ゆるSRE勉強会 #9
nealle
0
130
PHPのバージョンアップ時にも役立ったAST
matsuo_atsushi
0
210
How mixi2 Uses TiDB for SNS Scalability and Performance
kanmo
40
16k
Domain-Driven Transformation
hschwentner
2
1.9k
Rubyで始める関数型ドメインモデリング
shogo_tksk
0
130
GoとPHPのインターフェイスの違い
shimabox
2
200
1年目の私に伝えたい!テストコードを怖がらなくなるためのヒント/Tips for not being afraid of test code
push_gawa
1
490
Featured
See All Featured
It's Worth the Effort
3n
184
28k
Code Reviewing Like a Champion
maltzj
521
39k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Automating Front-end Workflow
addyosmani
1368
200k
How GitHub (no longer) Works
holman
314
140k
Gamification - CAS2011
davidbonilla
80
5.1k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
30
4.6k
A designer walks into a library…
pauljervisheath
205
24k
Build The Right Thing And Hit Your Dates
maggiecrowley
34
2.5k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2.1k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
640
Rails Girls Zürich Keynote
gr2m
94
13k
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ユーザ ⾼い まだ低い