R sink() message and output to same file - sanity check

◇◆丶佛笑我妖孽 提交于 2020-01-24 17:38:06

问题


I'm using R's sink() function to capture errors, warnings, messages and console output into a single text file.

I'm wondering if simultaneously sinking both message and output types to a single open file is bad to do?

I capture all of the above into a single file, but I must open a file handle to allow both sink types to be captured. The following code illustrates using the file handle approach:

message_filename = 'script_messages.txt'
try(message_file <- file(message_filename, open="at")) # open file for appending in text mode
sink(message_file, type="message")
sink(message_file, type="output")

cat("\n\n")
message(format(Sys.time(), "%a %b %d %Y %X TZ(%z)"), appendLF = TRUE)
# next line produces messages since file doesn't exist
try(source("import_file.R"), silent = TRUE)

# Save and close writing errors, warnings, messages, and console output to a file
sink(type="output")
sink(type="message")
close(message_file)

If I don't open a file handle, then the sink 'output' type messages are the only ones captured in the text file.

The documentation on sink {base} has some key info in the first half of the Details section, but I'm not fluent enough to be sure I've implemented it properly.

来源:https://stackoverflow.com/questions/59120777/r-sink-message-and-output-to-same-file-sanity-check

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