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
87
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
[JAWS-UG横浜 #80] うわっ…今年のServerless アップデート、少なすぎ…?
maroon1st
1
170
Bedrock Agentsレスポンス解析によるAgentのOps
licux
2
720
ASP. NET CoreにおけるWebAPIの最新情報
tomokusaba
0
360
ARA Ansible for the teams
kksat
0
150
Java Webフレームワークの現状 / java web framework at burikaigi
kishida
9
2.2k
いりゃあせ、PHPカンファレンス名古屋2025 / Welcome to PHP Conference Nagoya 2025
ttskch
1
270
WebDriver BiDiとは何なのか
yotahada3
1
140
TokyoR116_BeginnersSession1_環境構築
kotatyamtema
0
110
Unity Android XR入門
sakutama_11
0
140
知られざるDMMデータエンジニアの生態 〜かつてツチノコと呼ばれし者〜
takaha4k
4
1.3k
社内フレームワークとその依存性解決 / in-house framework and its dependency management
vvakame
1
550
Domain-Driven Transformation
hschwentner
2
1.9k
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Scaling GitHub
holman
459
140k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
The World Runs on Bad Software
bkeepers
PRO
67
11k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Speed Design
sergeychernyshev
25
780
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.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ユーザ ⾼い まだ低い