Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send a notification with R under Windows

I am currently recording / updating my tables in the bank with the R language:

dbWriteTable (with," table ", table, row.names = FALSE, overwrite = TRUE)

This script of mine that updates the tables in R in a postgres database, is automatically run from time to time by a windows task.
I need to know when the information is recorded or not, perhaps in an alert format on the screen, or something that catches the eye.

note: I know that there is "r.out", but it overwrites, does not warn when the problem occurs, or anything like that.

like image 634
Theorp Avatar asked Dec 19 '25 09:12

Theorp


1 Answers

You could call Windows Powershell:

  1. Run powershell and authorize script execution:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
  1. Create a notification script and execute it with system:
showMessage <- function(message) {
  MessageScript <-'
    [CmdletBinding()]
    
    $ErrorActionPreference = "Stop"
    
    $notificationTitle = "{message}"
    
    [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
    $template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText01)
    
    #Convert to .NET type for XML manipuration
    $toastXml = [xml] $template.GetXml()
    $toastXml.GetElementsByTagName("text").AppendChild($toastXml.CreateTextNode($notificationTitle)) > $null
    
    #Convert back to WinRT type
    $xml = New-Object Windows.Data.Xml.Dom.XmlDocument
    $xml.LoadXml($toastXml.OuterXml)
    
    $toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
    $toast.Tag = "PowerShell"
    $toast.Group = "PowerShell"
    $toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(5)
    #$toast.SuppressPopup = $true
    
    $notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("PowerShell")
    $notifier.Show($toast);'

  system('powershell',input=glue::glue(MessageScript),show.output.on.console=F)
  
}

showMessage(message = 'this is a test')

This works for me: enter image description here

like image 65
Waldi Avatar answered Dec 20 '25 22:12

Waldi