I implemented the following procedure that aims to write some files and print a message in the end of each file when the writing is done:
# Print one file per piaf
output_dir_piafs <- "OUTPUT_dataset_piafs"
unlink(output_dir_piafs, recursive = TRUE, force = TRUE)
dir.create(output_dir_piafs)
for (i in 1:length(lst_sorted)) {
sink() # Generates warnings...
filename <- paste(output_dir_piafs, "/piaf_", i, ".txt", sep="")
sink(file = filename, append = TRUE)
sink(type = "message")
cat(" ", colnames(file1), "\n")
for (j in 1:length(lst_sorted[[i]])) {
cat(j, " ")
lapply( lst_sorted[[i]][[j]],
function(x) {
cat(as.character(x), " ")
}
)
cat("\n")
}
## back to the console
sink()
cat(paste(filename, "done !\n"))
#flush(stdout()) # Tested, no particular effect
}
My problem is that, if I do not add a sink()
at the very beginning of the loop, the final writing on the standard output (cat(paste(filename, "done !\n"))
) has no effect. On the other hand, adding this early sink()
generates warnings that I would like to avoid:
There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In sink() : no sink to remove
2: In sink() : no sink to remove
3: In sink() : no sink to remove
Does anyone has an idea on how sink() behaves, and/or how to get rid of those warnings?
Note: I also tried try(sink(), silent=TRUE)
, but the silent
option prevents only from errors...
sink.number("message")
.sink.number("message")
doesn't return anything, but from a fresh new session, the standard output gets written fine. It is only when I stop the process (Ctrl-C
), then re-run it, that the standard output fails. So, I think you're right. My bad: interrupting the process might have some bad consequences on thestdout
connection. Moreover, if I typesink()
right before re-running the process, it still doesn't work...Ctrl-C
interruption which generates the issue. Any suggestion about how to make the things right, after having interrupted that way?