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
89
R研究集会(2024)のご案内
s_uryu
1
580
生成AIを用いたサービスの紹介
s_uryu
0
35
生成AIの基礎的事項と社会に与える影響
s_uryu
0
20
Rの機械学習フレームワークの紹介〜tidymodelsを中心に〜 / machine_learning_with_r2024
s_uryu
0
830
地理空間データの機械学習への適用 / 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
8
6.3k
統・再現性・協力: 人為的過誤を防ぎ、未来へ進む策 / Integration, Reproducible, and Collaboration
s_uryu
1
720
Other Decks in Programming
See All in Programming
Immutable ActiveRecord
megane42
0
130
[Fin-JAWS 第38回 ~re:Invent 2024 金融re:Cap~]FaultInjectionServiceアップデート@pre:Invent2024
shintaro_fukatsu
0
400
時計仕掛けのCompose
mkeeda
1
280
Domain-Driven Transformation
hschwentner
2
1.9k
Java Webフレームワークの現状 / java web framework at burikaigi
kishida
9
2.2k
法律の脱レガシーに学ぶフロントエンド刷新
oguemon
5
730
Djangoアプリケーション 運用のリアル 〜問題発生から可視化、最適化への道〜 #pyconshizu
kashewnuts
1
230
ASP. NET CoreにおけるWebAPIの最新情報
tomokusaba
0
360
2024年のkintone API振り返りと2025年 / kintone API look back in 2024
tasshi
0
220
Kubernetes History Inspector(KHI)を触ってみた
bells17
0
200
昭和の職場からアジャイルの世界へ
kumagoro95
1
360
いりゃあせ、PHPカンファレンス名古屋2025 / Welcome to PHP Conference Nagoya 2025
ttskch
1
270
Featured
See All Featured
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
175
51k
Agile that works and the tools we love
rasmusluckow
328
21k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
Designing on Purpose - Digital PM Summit 2013
jponch
117
7.1k
Being A Developer After 40
akosma
89
590k
GraphQLとの向き合い方2022年版
quramy
44
13k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Facilitating Awesome Meetings
lara
51
6.2k
Statistics for Hackers
jakevdp
797
220k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Scaling GitHub
holman
459
140k
The World Runs on Bad Software
bkeepers
PRO
67
11k
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ユーザ ⾼い まだ低い