How to create periodically send text to a “log file” while printing normal output to console?

前端 未结 5 1470
隐瞒了意图╮
隐瞒了意图╮ 2021-02-12 22:43

I\'m creating R code for a Monte Carlo simulation of a professional sport. Because the game dynamics are very complicated and to make the debugging process simpler, I\'d like to

5条回答
  •  闹比i
    闹比i (楼主)
    2021-02-12 23:42

    Figured it out, thanks to shujaa and BigFinger. To summarize, here is how you would do it with my example code:

    log_con <- file("/filepath/log.txt", open="a")
    
    for (i in 1:100)
    {
    cat("loop begins", file = log_con, sep="\n")
    a <- rnorm(n = 100, mean = i, sd = 5)
    print(mean(a))
    cat("single loop completed", file = log_con, sep="\n")
    }
    
    close(log_con)
    

提交回复
热议问题