Rで処理を書いて計算すると、計算量が多すぎて数十分返ってこない事が時々ある。
そのため処理が終わっているかどうか、作業の合間に確認することをよくやりがちだが、正直面倒くさい上に集中力が削がれる。
ので、Rに通知させる方法を調べていたら面白い記事があったのでトライしてみた。
けっこういい感じ。ちなみにMacだけ。Rstudio-serverでの方法はしらない。Winでのやりかたはもっと知らない。
やりかた
- terminal-notifierをインストール。
$ brew install terminal-notifier
- Rのコンソールに移動し、下記コマンドを入力。
> install.packages("devtools")
> library(devtools)
> source_gist("9c419af5547fde20d2a7")
- 好きに通知する
> notify("hogepyiyo ~")
その結果はこんな感じ。
![7c31425e-6506-cf2b-7e24-c5cabc763a66.jpeg](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F28555%2Fe5cc93e3-2191-b12c-a9d7-05dd75956655.jpeg?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=49a0dbc069b759925af38ccd4a67a269)
notifyの中身はただのsystem関数。
notify.R
function(msg="Operation complete") {
in.osx <- (Sys.info()['sysname'] == "Darwin")
in.rstudio <- (Sys.getenv("RSTUDIO") == "1")
in.rgui <- (Sys.getenv("R_GUI_APP_REVISION") != "")
dir.notifier <- system("which terminal-notifier", intern = TRUE)
if (in.rstudio) { # hack to see if running in RStudio
title <- "RStudio"
sender <- activate <- "org.rstudio.RStudio"
}
if (in.rgui) { # running in R GUI app?
title <- "R GUI"
sender <- activate <- "org.R-project.R"
}
# if running in RStudio or R GUI app use NotificationCenter otherwise use message()
if ((in.rstudio | in.rgui) & in.osx) {
system(sprintf(paste(dir.notifier, "-title '%s' -message '%s' -sender %s -activate %s", sep = " "),
title, msg, sender, activate ),
ignore.stdout=TRUE, ignore.stderr=TRUE, wait=FALSE)
} else {
message(msg)
}
}