Send Desktop Notifications from R in Windows, Linux and Mac

October 4, 2018
By

(This article was first published on R Programming – DataScience+, and kindly contributed to R-bloggers)

    Categories

    1. Programming

    Tags

    1. Best R Packages
    2. R Programming
    3. Tips & Tricks

    In the age of smartphones, Notifications have become an integral part of life that Smart watches have started popping up to handle our notifications. Desktop Notification is a very good way of letting the user know about a task completion or some information. Say, your model has been running and at the end, you just send a notification of the AUC score. Wouldn’t it be nice to have? With this R package notifier, You can do that.

    About notifier:

    notifier is an R package by the well-known Gábor Csárdi of RStudio. notifier can be used to send desktop notifications from R, on macOS, Windows and Linux. notifier works across platform (Windows / Mac/ Linux) but the following code has been written and tested on macOS High Sierra Machine.

    notifier Installation & Loading

    notifier is currently available only on github, hence can be installed using the following code:

    #install.packages("devtools") if devtools is not installed
    devtools::install_github("gaborcsardi/notifier")
    

    We can load notifier into our current R session using the following code (just like any other R package).

    library(notifier)
    

    How does it work?

    As described in the documentation, This is how the notification appears:

  • On macOS, notifier uses the terminal-notifier tool, see https://github.com/julienXX/terminal-notifier
  • On Linux and *BSD systems, including Solaris the notify-send command line tool is used. This requires the libnotify-bin package on Ubuntu/Debian and similar systems, or the libnotify package on RedHat/CentOS/Fedora and similar systems.
  • On Windows 8 or newer Windows versions, notifier uses the toaster tool, see https://github.com/nels-o/toaster.
  • On older Windows versions, notifier uses the notifu program, see https://www.paralint.com/projects/notifu.
  • Basic usage

    notifier is very minimal with one function to create a notification or as the function says, “notify“. The function notify() takes the following three arguments:

    * title – Message title.
    * msg – Actual Notification Message.
    * image – Image, along with the message – optional.

    Hello World

    As in every computer programming exercise, Let us begin with a simple Hello World notification message.

    #composing the first notification message
    
    notify(
      title = "Here goes the Title",
      msg = c("Hello","World")
      )
      
    

    Gives this notification (screenshot):

    Slightly more useful

    Now that we have learnt how to send desktop notifications from R, let us try to make that notification with some meaning and use. The one I could think of immediately is a world clock or Time of different countries. The following is the code how we can do that.

    This code simply takes the current system time and formats it in respective time zones. Remember, the parameter msg takes only character type, hence we need to convert the date type to character type which by default happens as we have used paste0.

    #composing a slightly complex and also useful notification
    
    notify(
      title = "World Clock",
      msg = c(paste0(" India - ", format(Sys.time(), format = "%H:%M:%S" ,tz = "Asia/Calcutta"),"\n",
                     paste0("Singapore - ", format(Sys.time(), format = "%H:%M:%S" ,tz = "Asia/Singapore"),"\n"))
              )
    )
    
    

    Gives this notification (screenshot):

    A Motivational Notification

    As we have used base-R functions to create a slightly meaningful notification, let’s upgrade to a good notification that can motivate us – in the form of quotes. For that, we will use another R package – randquotes.

    #composing a different use-case notification
    
    
    library(randquotes)
    notify(
      title = "Quote of the Day",
      msg = c(randquote_simple())
    )
    
    
    

    Gives this notification (screenshot):

    Further Improvement

    This could be further improved by using Task Scheduler (on Windows) or Shell Scripting Automation (on Linux/Mac) to automate this notification at regular interval – like showing a Quote notification every morning.

    Summary

    I hope this post would have helped you learning about this R package notifier which makes sending Desktop notifications from R possible – which I didn’t even have an idea that could be possible. The complete code used in this post is available on my github.

    Let us know in comments of what notification you’d like to try or you’ve tried!

    Related Post

    1. CHAID vs. ranger vs. xgboost — a comparison
    2. Common Mistakes to Avoid When Learning to Code in Python
    3. Top Python Libraries for Machine Learning
    4. Get Popular Programming Languages from TIOBE Index using R
    5. How to make Seaborn Pairplot and Heatmap in R (Write Python in R)

    To leave a comment for the author, please follow the link and comment on their blog: R Programming – DataScience+.

    R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



    If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

    Comments are closed.

    Search R-bloggers

    Sponsors

    Never miss an update!
    Subscribe to R-bloggers to receive
    e-mails with the latest R posts.
    (You will not see this message again.)

    Click here to close (This popup will not appear again)