0

I want to write a error log for one of my functions (see below). However, when the function fails, the sink won't get closed properly. Is there a way to always close the sinks() upon exiting the function?

some_function <- function(){


 con <- file("test.log")
 sink(con, append=TRUE,type="output",split=TRUE)
 sink(con, append=TRUE,type="message")

 >> do some stuff that may fail. 

 sink() 
 sink(type = "message")

}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
tafelplankje
  • 563
  • 1
  • 7
  • 21

1 Answers1

0

Use try.

try(lm(1), outFile="error.txt")  ## failing stuff

File contains:

Error in formula.default(object, env = baseenv()) : invalid formula
jay.sf
  • 60,139
  • 8
  • 53
  • 110