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

前端 未结 5 1467
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  时光说笑
    2021-02-12 23:27

    See ?cat. You can open a file connection to your log file and specify that in your cat call. When you don't specify a file name or connection it will print to the console.

    As you say, don't use sink() as it will make the log file the default connection. Rather, open a named connection with file().

    > log_con <- file("test.log")
    > cat("write to log", file = log_con) # creates file and writes to it
    > cat("write to console") # prints to console
    write to console
    

    The above results in a log file with the line "write to log" and "write to console" printed on the console.

    If you need to append to your log file, set append = TRUE and use the file name instead of the file() connection.

    > cat("add to log", file = "test.log", append = TRUE)
    

提交回复
热议问题