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
90
R研究集会(2024)のご案内
s_uryu
1
580
生成AIを用いたサービスの紹介
s_uryu
0
39
生成AIの基礎的事項と社会に与える影響
s_uryu
0
21
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
7
6.3k
統・再現性・協力: 人為的過誤を防ぎ、未来へ進む策 / Integration, Reproducible, and Collaboration
s_uryu
1
720
Other Decks in Programming
See All in Programming
『GO』アプリ バックエンドサーバのコスト削減
mot_techtalk
0
150
Ruby on cygwin 2025-02
fd0
0
150
データベースのオペレーターであるCloudNativePGがStatefulSetを使わない理由に迫る
nnaka2992
0
210
Code smarter, not harder - How AI Coding Tools Boost Your Productivity | Angular Meetup Berlin
danielsogl
0
100
CSS Linter による Baseline サポートの仕組み
ryo_manba
1
140
PHPカンファレンス名古屋2025 タスク分解の試行錯誤〜レビュー負荷を下げるために〜
soichi
1
570
Grafana Loki によるサーバログのコスト削減
mot_techtalk
1
130
How mixi2 Uses TiDB for SNS Scalability and Performance
kanmo
40
15k
PHP ステートレス VS ステートフル 状態管理と並行性 / php-stateless-stateful
ytake
0
110
PRレビューのお供にDanger
stoticdev
1
200
React 19アップデートのために必要なこと
uhyo
5
730
Domain-Driven Transformation
hschwentner
2
1.9k
Featured
See All Featured
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
A designer walks into a library…
pauljervisheath
205
24k
Mobile First: as difficult as doing things right
swwweet
223
9.3k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Designing Experiences People Love
moore
140
23k
Speed Design
sergeychernyshev
27
800
Embracing the Ebb and Flow
colly
84
4.6k
GraphQLとの向き合い方2022年版
quramy
44
13k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
For a Future-Friendly Web
brad_frost
176
9.5k
The Cult of Friendly URLs
andyhume
78
6.2k
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ユーザ ⾼い まだ低い