首页 > 解决方案 > Windows下用R发送通知

问题描述

我目前正在使用 R 语言记录/更新我在银行中的表格:

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

我的这个脚本更新了 postgres 数据库中 R 中的表,由 windows 任务不时自动运行。
我需要知道何时记录了信息,可能是屏幕上的警报格式,或者是什么吸引眼球的东西。

注意:我知道有“r.out”,但它会覆盖,当问题发生时不会发出警告,或者类似的东西。

标签: rpostgresqldatatable

解决方案


您可以调用 Windows Powershell:

  1. 运行 powershell 并授权脚本执行
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
  1. 创建一个通知脚本并执行它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')

这对我有用: 在此处输入图像描述


推荐阅读