5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rに通知させる

Last updated at Posted at 2016-05-24

Rで処理を書いて計算すると、計算量が多すぎて数十分返ってこない事が時々ある。
そのため処理が終わっているかどうか、作業の合間に確認することをよくやりがちだが、正直面倒くさい上に集中力が削がれる。

ので、Rに通知させる方法を調べていたら面白い記事があったのでトライしてみた。
けっこういい感じ。ちなみにMacだけ。Rstudio-serverでの方法はしらない。Winでのやりかたはもっと知らない。

やりかた

  1. terminal-notifierをインストール。
$ brew install terminal-notifier
  1. Rのコンソールに移動し、下記コマンドを入力。
> install.packages("devtools")
> library(devtools)
> source_gist("9c419af5547fde20d2a7")
  1. 好きに通知する
> notify("hogepyiyo ~")

その結果はこんな感じ。

7c31425e-6506-cf2b-7e24-c5cabc763a66.jpeg

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)      
  }
  
}
5
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?