代码之家  ›  专栏  ›  技术社区  ›  Maiasaura

有没有办法让R哔哔声/播放一个声音在一个脚本的结尾?

r
  •  121
  • Maiasaura  · 技术社区  · 16 年前

    16 回复  |  直到 16 年前
        1
  •  101
  •   Mathias711    6 年前
    alarm()
    

    这个 alarm 功能。它通过发送 \a 到控制台

        2
  •  170
  •   Rasmus Bååth    12 年前

    我有一个包裹( beepr )唯一的目的是在R中发出通知声音,它应该跨平台工作。运行以下程序安装蜂鸣器并发出声音:

    install.packages("beepr")
    library(beepr)
    beep()
    

    github上的更多信息: https://github.com/rasmusab/beepr

        3
  •  60
  •   VLC    11 年前

    在MacOSX上,您可以让计算机说话:

    system("say Just finished!")
    

    您还可以更改将要讲话的人工语音:

    system("say -v Kathy Just finished!")
    

    系统首选项->听写和;演讲->文本到语音 .

        5
  •  34
  •   Dason    13 年前

    alarm 在我的Windows机器上不工作,所以我创建了一个函数,它确实会发出噪音。

    beep <- function(n = 3){
        for(i in seq(n)){
            system("rundll32 user32.dll,MessageBeep -1")
            Sys.sleep(.5)
        }
    }
    

    这显然只能在Windows上运行,但我不能保证它甚至可以在任意Windows计算机上运行。我只在我的机器上测试过,但我想我会把它贴出来,以防有人对它有同样的问题 警报

        6
  •  29
  •   mpk    16 年前

    猫('你好世界\a')

        7
  •  17
  •   Dason    13 年前

    不仅如此,当程序循环完成时,你还可以从Youtube上传一些史诗般的音乐:)(对于Ubuntu/Debian:)

    system("xdg-open 'http://www.youtube.com/watch?v=9jK-NcRmVcw'")
    
        8
  •  14
  •   Zoe - Save the data dump 张群峰    7 年前

    对于带有gui和web浏览器的操作系统来说,做些与操作系统相当独立的事情怎么样?它甚至可以在RStudio服务器上运行!

    browseURL('https://www.youtube.com/watch?v=QH2-TGUlwu4')
    
        9
  •  12
  •   nwinkler Astrogat    13 年前

    请使用 shell.exec("url")

        10
  •  12
  •   Stewart Macdonald    7 年前

    更新:

    使用macOS 10.9(Mavericks)及更高版本,您可以使用纯AppleScript发布通知:

    theTitle <- "A Title"
    theMsg <- "A message here"
    
    cmd <- paste("osascript -e ", "'display notification ", '"', theMsg, '"', ' with title ', '"', theTitle, '"', "'", sep='')
    system(cmd)
    

    terminal-notifier ,参考如下。

    我有 terminal-notifier 安装在我的Mac上以从命令行获取桌面通知。然后你就可以结束对 system()

    notify <- function(msgString='Message from R', titleString='Message from R', speakIt=FALSE) {
        cmd <- paste('~/terminal-notifier/terminal-notifier.app/Contents/MacOS/terminal-notifier -message ', '"', msgString, '"  -title "', titleString, '"', sep='')
        system(cmd)
    
        if (speakIt) {
            system(paste('say', msgString))
        }
    
    }
    

    你可以这样调用函数

    notify("R is done", "Message from R", speakIt=TRUE)
    

    要得到这样的信息:

    enter image description here

    更新:包括@VLC's say 命令。

        11
  •  9
  •   aL3xa    16 年前

    或者如果您使用的是GNU/Linux发行版 system 通过一些听觉/视觉通知,例如。

    system("aplay -t wav /usr/share/sounds/phone.wav") # for auditive bell (an I mean it literary)
    system("zenity --title=\"R script info\" --text=\"Script has finished with zero exit status\" --info") # for GTK dialog
    

    你可以查一下 功能,你可以做几乎任何事情:发送电子邮件,运行其他脚本,重新启动机器, sudo rm -rf *.* ... 我是认真的。

    如果 您正在运行GNU/Linux(或UNIX)发行版,否则,请坚持使用特定于Windows的命令,尽管在这种情况下,我不能提供太多信息。。。

        12
  •  6
  •   Mullefa    11 年前

    灵感来自 beepr ,这是我目前用来解决这类问题的函数:D

    work_complete <- function() {
      cat("Work complete. Press esc to sound the fanfare!!!\n")
      on.exit(beepr::beep(3))
    
      while (TRUE) {
        beepr::beep(4)
        Sys.sleep(1)
      }
    }
    
        13
  •  6
  •   Chirayu Chamoli    9 年前

    放点音乐怎么样?

    shell.exec("foo/Born.to.be.wild.mp3")
    
        14
  •  5
  •   Meme    9 年前

    看看这个包裹: RPushBullet

    Pushbullet消息服务的R接口,它提供了 以及计算机之间的高效通知(和文件传输),

    RPushbullet是完全免费的多平台产品。至于你的问题,你可以使用这个库发送推送到你的浏览器,但很明显,当你需要一些东西而不是在你不在的时候通知你时,它会变得很神奇。

        15
  •  0
  •   JonnyRobbie    10 年前

    你可以用 notify-send 命令:

    system("notify-send \"R script finished running\"")
    
        16
  •  0
  •   Mongutea    7 年前

    由于这些想法,我创建了一个没有互联网接入的解决方案,因为我使用的是带有Windows的VPN客户端。所以它播放的是典型的Windows声音,通常在任何Windows操作系统上都是这样。

    #Function with loop, press Esc to stopp      
        alarm2 <- function(){
          while(TRUE){
            system("cmd.exe",input="C:/Windows/WinSxS/amd64_microsoft-windows-shell-sounds_31bf3856ad364e35_10.0.17134.1_none_fc93088a1eb3fd11/tada.wav")
            Sys.sleep(1)
          }
        }
    

    无环函数

        alarm3 <- function(){
            system("cmd.exe",input="C:/Windows/WinSxS/amd64_microsoft-windows-shell-sounds_31bf3856ad364e35_10.0.17134.1_none_fc93088a1eb3fd11/tada.wav")
            Sys.sleep(1)
        }
    
        17
  •  0
  •   MikolajM    5 年前

    使用Windows怎么样 SpeechSynthesizer ?

    message <- "job done!"
    
    system2(command = "PowerShell", 
            args = c("-Command", 
                     "\"Add-Type -AssemblyName System.Speech;",
                      "$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer;",
                      paste0("$speak.Speak('", message, "');\"")
            ))
                                                   
    

    这可以很好地用在迭代操作中,比如“第一个任务完成了”,“第二个任务完成了”等等:

    say_something <- function(message) {
        
         message <- paste0("$speak.Speak('", message, "');\"")
        
         system2(command = "PowerShell", 
                 args = c("-Command", 
                           "\"Add-Type -AssemblyName System.Speech;",
                           "$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer;",
                                        
                           message
                ))
      }
    
    operations <- c("1st.", "2nd.", "3rd.")
    lapply(operations, function(x) say_something(message=paste(x, "job done")))
    
     
    

    *注意,这使用系统默认语言设置。该示例基于英语lector,可以使用 SelectVoice

      system2(command = "PowerShell", 
            args = c("-Command", 
                     "\"Add-Type -AssemblyName System.Speech;",
                      "$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer;",
                     "$speak.GetInstalledVoices().VoiceInfo")
            )
    

    这就产生了:

        Gender                : Female
    Age                   : Adult
    Name                  : Microsoft Paulina Desktop
    Culture               : pl-PL
    Id                    : TTS_MS_PL-PL_PAULINA_11.0
    Description           : Microsoft Paulina Desktop - Polish
    SupportedAudioFormats : {}
    AdditionalInfo        : {[Age, Adult], [Gender, Female], [Language, 415], [Name, Microsoft Paulina Desktop]...}
    
    Gender                : Male
    Age                   : Adult
    Name                  : Microsoft David Desktop
    Culture               : en-US
    Id                    : TTS_MS_EN-US_DAVID_11.0
    Description           : Microsoft David Desktop - English (United States)
    SupportedAudioFormats : {}
    AdditionalInfo        : {[Age, Adult], [Gender, Male], [Language, 409], [Name, Microsoft David Desktop]...}
    

    最后是一个函数,通过“David”、“Paulina”或“Hazel”等“name”来选择选择器:

    say_something <- function(message , voice) {
            
      voice <- paste0("\"$speak.SelectVoice('Microsoft ", voice, " Desktop');" )
      message <- paste0("$speak.Speak('", message, "');\"")
          
      system2(command = "PowerShell", 
              args = c("-Command", 
                        "\"Add-Type -AssemblyName System.Speech;",
                        "$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer;",
                        voice,
                        message
              ))
    }
    
    
    operations <- c("1st.", "2nd.", "3rd.")
    
    lapply(operations, function(x) say_something(message=paste(x, "job done"), voice="David"))
    
        18
  •  0
  •   dzmanto    5 年前

    switch(Sys.info()[['sysname']],
    Linux = {
        system('play -n synth 0.1 tri  1000.0')}
    )