问题
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