Using Sys.sleep in R function to delay multiple outputs

后端 未结 2 1534
灰色年华
灰色年华 2021-01-26 14:37

I have this function:

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

This function obviously wo

相关标签:
2条回答
  • 2021-01-26 14:48

    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"
    
    0 讨论(0)
  • 2021-01-26 14:59

    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"
    > 
    
    0 讨论(0)
提交回复
热议问题