Reputation: 965
I am trying to find a way to receive a MacOS alert upon completion of the knitting. Ideally, this should run when the "Knit" button is pressed within RStudio without having to run a special build file.
I assume I can do so by strategic insertion of the following code somewhere within the code:
system('osascript -e \"display notification \\"Knit-Completed\\"\"')
Could someone familiar with the RStudio knitr interface and knitr point me toward where it would be most practical to insert such code?
Upvotes: 1
Views: 427
Reputation: 15419
I like the idea of a system popup, but this seems like a bit more tricky as there will be different protocols to run on Windows, Mac and Linux. Here are some ideas for system pop-ups.
There are two things which can happen when knitting:
We can use these events to trigger code as follows:
```{r echo = F}
# When R encounters an error
options(error = function(){
#Run your code here
}
)
# When R exits the knitr session
.Last <- function() {
#Run your code here
}
```
This script needs to be placed near the start of your Rmd file. Ideally include it with the setup at the very beginning or have it saved as a seperate R file and then include the line source("path_to_file.R")
within your project.
There are loads of different techniques which could be used to create notifications. The following examples show some techniques that could be used and placed in the #Run your code here
position in the code above:
Play a sound: As shown here, the easiest thing to do is run a sound on exit:
beepr::beep()
Sys.sleep(1) # This allows time for the sound to run
Note: beepr::beep(sound = 7)` might be more appropriate for an error message :D
Basic Notification on Windows: as found in this guide, you can create a popup window:
system('CMD /C "ECHO The R process has finished running && PAUSE"',
invisible=FALSE, wait=FALSE)
Notification on Mac: this post seems to be quite helpful
Send an email: the mail package could send an email notification. There is a limit of 20 emails per day:
mail::sendmail(recipient, subject="Notification from R",
message="Calculation finished!", password="rmail")
Notifications cross-platform: the notifier R package seems to be failing at the moment, but it should allow notifications on all systems:
library(notifier)
notify(
title = "Your title",
msg = "Add detail here")
)
Terminal notification on Mac: This tool could be used to create notifications from the command line. I don't have a Mac so cannot test this package.
Push Notifications to your phone: The pushoverr package lets you send a notification to your phone. The functionality looks good but it appears you have to pay for some features.
Pushbullet: RPushbullet lets you interact with the Pushbullet service. It offers a range of apps across systems, although doesn't cover Mac yet.
For any of the text-based notifications, you could easily customise the output message to the script. Here are some ideas:
Display Runtime: could make it display the time taken to run the code:
# Put this at the start of the document
time_start <- Sys.time()
# Add this to the notification code
(Sys.time() - time_start)
Display filename: knitr::current_input()
will display the current file name being knitted.
It would be great to see what recipes people come up with if they use this information. Share them in the comments or add it to the end of the post if you have any ideas.
Upvotes: 3