R process$new write stdout and stderr to same file

108 views Asked by At

In my R code I call another R script as a separate process like this

rp <- callr::process$new(command  = "rcode.R", args     = c("--date", date)
        , stdout   = 'logfile.txt', stderr   = 'logfile.txt'
        , cleanup  = FALSE
        , echo_cmd = TRUE
)

Both logger messages as well as any errors are getting written to same log file. But the error messages are writing on top of the file instead of writing on the bottom of the page

Sample logfile

Error in eval(parse(text = text, keep.source = FALSE), envir) : 
  object 'filepath' not found
Calls: script2... glue_data ->  -> .transformer -> eval -> eval
In addition: Warning messages:
1: In rm(var traceback available 
lEnv") :
INFO [2021-04-20 13:30:37] date : 2021-03-28 
INFO [2021-04-20 13:30:37] user : aaa
INFO [2021-04-20 13:30:37] param2 : value

In the above sample log file when the error occured i would like the error to print at the end of the file

May I ask a sub question too? why rp$get_exit_status() returns zero even after there is an error in the code?

0

There are 0 answers