Creating a COVID-19 Notifier in R

In this tutorial, we are going to create a desktop Notifier in R using the Notifier package.

Tattooed Geek
6 min readMay 9, 2020

I will divide the post into 3 parts to properly explain to you the basics and build up complex Notifiers:

  1. A basic desktop notifier using notifier() in R.
  2. Creating a desktop notifier to send notifications after regular intervals.
  3. Creating a desktop notifier to send COVID-19 updates after every 1 hour.

It is really simple and straight forward. You can use this to create reminders, applications and motivating quotes for yourself which you can tweak accordingly to send you reminders at the regular time intervals. In this post, I will also explain to you about sending notification at a certain time or at regular time intervals.

Link to a few of the applications/dashboards I have developed in R.

  1. Analysing Corona Virus spread Worldwide in R
  2. COVID-19 India Tracker in R
  3. Analysis of Indian Economy using R
  4. kaggle Data Science Survey Data Analysis Dashboard

So go ahead and check out these dashboards/web-apps made in R. Use it as a reference to create your own projects. The link to the code and GitHub repositories are in the App.

Bonus Tip: One great tool I recently started using for writing and tasks such as plagiarism checker, grammar checker, Co-writer, paraphraser, summariser, and translator is QuillBot .

I wanted to try something similar and cheaper than Grammarly.

I took up its yearly premium for around $2/month (45% off) during the Year-end sale using coupon code — (HOLIDAY45), valid till December end. The price was literally dirt cheap compared to other writing tools I have used in the past.

Personally, it’s UI and UX is very simple and easy to use. So I just wanted to share this awesome, productive tool with you all. Do check it out and use it in your day-to-day writing tasks.

https://try.quillbot.com/

Best Productivity Writing tool for this month

SIDE NOTE:

I always make sure I share my knowledge and resources which helped me. So I will be adding a few links to my favourite courses below related to this post and which can help you experiment with this post on your own. My focus has always been to learn things in a practical manner, that is why DataCamp has always been my go-to place for learning from the very start. It’s just that in this manner you can actually learn faster and better. DataCamp also has an amazing library of Projects. They started this new service recently and it’s really amazing. DataCamp projects is another amazing thing which I keep doing from time to time. Make sure to give it a try. Just once try them and then you will understand why I love DataCamp so much.

DataCamp will be offering a first-month subscription for only $1 from today till 3rd Dec, 8 PM ET. Also, they are having a black Friday sale where Yearly subscriptions have 75 % off. This is the first time they have ever run a sale like this. So do make sure to go check out DataCamp. Also, DATCAMP CHEATSHEETS is a pretty handy and very useful tool that I use from time to time.

Firstly, If you are not familiar with the basics of the R language, I urge readers to go complete this amazing course by DataCamp on R Programming, and this is the course from where I started my Data science journey using R years ago. Trust me with this, these courses are worth your time and money or if you want to learn the complete data science fundamentals from scratch using R which includes Statistics and probability, Data viz, exploring the data, data modelling and machine learning and other important concepts, I strongly recommend this foundation course Data Science with R .

Introduction to writing functions in R.

Introduction to machine learning in R.

Writing efficient code in R.

Building web apps in R using Shiny.

So this would literally be the best time to grab some yearly subscriptions(which I have) which basically has unlimited access to all the courses and other things on DataCamp and make fruitful use of your time sitting at home during this Pandemic. So go for it folks and Happy learning, make the best use of this quarantine time and come out of this pandemic stronger and more skilled.

How do Notifier works?

  1. MAC OS: It uses MAC’s terminal-Notifier tool(https://github.com/julienXX/terminal-notifier).
  2. Linux based and BSD based systems: It uses 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.
  3. Windows: Windows versions, notifier uses the toaster tool, see https://github.com/nels-o/toaster.

Usage of Notifier

“notifier” uses one basic function to create a notification or as the function says, “notify“. The function notify() takes the below three arguments:

  1. title — Message title.
  2. msg — Actual Notification Message.
  3. image —add Image, along with the message — optional.

Below is a basic example of using notify() function.

#installing the Notifier packagedevtools::install_github(“gaborcsardi/notifier”)library(notifier)notify(title = “This is Anish”,msg= c(“Hi”,”this is my first desktop notifier”))# For more details about the function you can use:
# ?notify
Output

And this is it. Just one function notify(). You will notice notifications popping up on your desktop.

Making a notifier which will send notifications after regular Intervals:

There are multiple use cases for the desktop notifications for example you want to get notified every time the script gets completed i.e after a specific time interval.

# making a function to send notifications every 1 minute#'randquotes' for generating random quotes
library(randquotes)
repeat {

# getting the current time
startTime <- Sys.time()


notify(

title = "Though of the day:",
msg = c(randquote_simple())
)
#the logic is simple (Start_time + time_interval in seconds - #current_sys_time) will give us the sleep time i.e interval after #which notifications is again sent sleepTime <- startTime + 60 - Sys.time()
if (sleepTime > 0)
Sys.sleep(sleepTime)
}
Output

So this is it. Another simple implementation and no rocket science.

Making a notifier which will send notifications about COVID-19 cases for India every hour

Let’s first get the data from the data source. Here you can scrape data from any source in R. The process is fairly simple. This is a really good post on scraping data in R using a really good package rvest. You can follow this article to scrape web data in R.

Here we are also going to use another amazing R package “beepr” to add notification sound tones to make it better. BeepR simply uses a function “beep()”. We are going to use “RCurl” package to read data in R.

require(RCurl)myfile1 <-getURL('https://api.covid19india.org/csv/latest/state_wise.csv', 
ssl.verifyhost=FALSE, ssl.verifypeer=FALSE)

#dataset with summarised COVID-19 case counts of Indian states
StateCOVID_19 <- read.csv(textConnection(myfile1), header=T)

msg1=paste("Confirmed Cases: ",Confirmed,", Recovered:",Recovered)
msg2=paste("Deaths: ",Deaths, ", Active:\n ",Active)



repeat {

# getting the current time
startTime <- Sys.time()


notify(

title = "COVID-19 India's Summary",
msg = c(msg1,msg2)
)

#adding the beep notification sound
beep("wilhelm")

#send notification after every hour(3600 seconds)
sleepTime <- startTime + 3600 - Sys.time()
if (sleepTime > 0)
Sys.sleep(sleepTime)


}
The output of the above function

And that is it. Go ahead and create your own notifier in R.

I hope you guys liked the post. Make sure to like it and share it with your colleagues. Use this post to make something interesting and experiment with it.

You can reach out to me on the following social profiles below for any doubts or queries and follow me there as well.

Stay safe and stay indoors and make sure to make the best use of your time during this lockdown.

  1. Github
  2. LinkedIn
  3. RPubs
  4. DataScience+

--

--

Tattooed Geek

Main-Blog:https://medium.com/@anishsingh20 / | Medium Top Writers(India) | Solopreneur | Founder@DataInksights | Medium 150,000+ views/70,000+ Reads - monthly