How to send Desktop Notification in R

Hello friends! today we’ll be learning how to send desktop notification in R. There are push notification in mobile phones, site notifications for websites like gmail sends desktop notification for every new received mail (if you’ve switched on the notification).

There are multiple use cases for the desktop notifications for example you want to get notified every time the script gets completed. Here I’m using desktop toast notification to show cricket score in every 5 minutes.

Find the code below used for the tutorial also install below packages before starting.

XML, jsonlite, notifier

library(XML)
library(jsonlite)
library(notifier)
repeat {
startTime <- Sys.time()
#Fetch cricket score for desktop notification 
xmlfile<- xmlTreeParse("http://synd.cricbuzz.com/j2me/1.0/livematches.xml")
#convert it to list
xmlfiletolist<-xmlToList(xmlfile)
#convert xml list file to json for easy readablity
listtojson<- toJSON(xmlfiletolist)
#read json data
data<- fromJSON(listtojson)
#storing scores,overs and other match details
score<- paste(data$match.1$mscr$btTm$`2`,"/",data$match.1$mscr$btTm$`6`) 
overs<- data$match.1$mscr$btTm$`5`
partnership<- data$match.1$mscr$inngsdetail[4]
Desc<- data$match.1$.attrs[4]
aa<- paste(score,"Overs:",overs)
#send desktop notification 
notify(title=Desc,msg = aa)
#sending notification in every 5 minutes (300 seconds)
sleepTime <- startTime + 300 - Sys.time()
if (sleepTime > 0)
Sys.sleep(sleepTime)
}

Check the data source link to get the information properly. If every thing is done perfectly you’ll get notification like the image below and by this you can check the score without distraction and disturbing your work since the script is running in the background that’ll take care of everything.

I’m fairly new to the web scrapping and couldn’t find the easier way to fetch the score node so I used json.

Further reading and source

Data source: Cricbuzz

Notifier package in R, Start with jsonlite in R

 

Thanks for reading! Comment your queries and suggestion!

Keep visiting Analytics Tuts for more tutorials!

 

 

 

 

One comment

  • This is what I was looking for some of my works. I’ve achieved desktop notification using python but looking to replicate the same in R. Simple and easy tutorial.
    Thanks Niket.

Leave a Reply

Your email address will not be published. Required fields are marked *