0

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?

sjd
  • 1,329
  • 4
  • 28
  • 48
  • 1) You should load the packages you are using with `library(process)`; 2) What are the values of `cmd`, `date` and `logfile`? Like this the error is not reproducible. – Rui Barradas Apr 20 '21 at 19:04
  • Isn't this `library(processx)`? – r2evans Apr 20 '21 at 19:07
  • 1
    @r2evans Yes, sorry for the typo :(. – Rui Barradas Apr 20 '21 at 19:12
  • FYI, this has been discussed before, https://github.com/r-lib/processx/issues/103 (since closed). I believe `processx::run` does not allow you to write both to a single file safely/simultaneously; the best offered was to write both to `"|"` and poll in R (not always the best answer). – r2evans Apr 20 '21 at 19:16
  • btw, the reason that the error appears first is likely either due to blind luck (it might be last in other runs) or due to the stderr being flushed first, then the process exits, and then stdout is flushed. – r2evans Apr 20 '21 at 19:18
  • Sorry the library was `callr` . updated the question – sjd Apr 20 '21 at 19:20

0 Answers0