Using Sys.sleep in R function to delay multiple outputs

ε祈祈猫儿з 提交于 2019-12-02 21:26:20

问题


I have this function:

func<-function(name){
    paste("Your name is. . .")
    Sys.sleep(1.5)
    paste(name)
}

This function obviously won't work, but the idea is to wait 1.5 seconds between each output.

For example, calling func("Catherine") should print to console:

[1] "Your name is..."

Then wait 1.5 seconds, and print:

[1] "Catherine"

回答1:


Just wrap your desired output in a print statement:

func<-function(name){
  print("Your name is. . .")

  Sys.sleep(1.5)

  print(name)
}

#Execute Function
func("Martin")

[1] "Your name is. . ."
[1] "Martin"



回答2:


I'm not quite sure what the question is, but this produces the behavior you are talking about.

func <- function(name)
{
print("Your name is. . .")
flush.console()
Sys.sleep(1.5)
print(name)
}

> func('Test')
[1] "Your name is. . ."
[1] "Test"
> 


来源:https://stackoverflow.com/questions/36022623/using-sys-sleep-in-r-function-to-delay-multiple-outputs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!