Skip to content

Instantly share code, notes, and snippets.

@strboul
Created August 6, 2018 20:17
Show Gist options
  • Save strboul/e763222619192a93022e75769ddcabd9 to your computer and use it in GitHub Desktop.
Save strboul/e763222619192a93022e75769ddcabd9 to your computer and use it in GitHub Desktop.
'sink' diverts R output to a connection
# sink() sends output to stdout to get it written in a file.
# reports how many sink diversions are in use:
sink.number()
sink(file = "sink.log")
cat("hello")
runif(10)
cat(runif(10), sep = "\n")
sink(file = NULL) # be sure you closed the sink!
## hello [1] 0.60970869 0.66170051 0.55986574 0.25873516 0.77806846 0.05607446
## [7] 0.03564875 0.42021482 0.09749262 0.58600739
## 0.5458542
## 0.2357056
## 0.1370189
## 0.3506223
## 0.733947
## 0.7913567
## 0.0733205
## 0.402124
## 0.1060525
## 0.5546568
zz <- file("sink.log", open = "wt")
sink(zz, type = "message")
cat("hello")
runif(10)
log("a")
sink(type = "message")
sink()
## Error in log("a") : non-numeric argument to mathematical function
## Warning message:
## In sink() : no sink to remove
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment