Output error/warning log (txt file) when running R script under command line

前端 未结 2 1486
借酒劲吻你
借酒劲吻你 2020-12-01 09:49

If I run R script under command line (actually I run that from calling in VBA), how can I output any error/warning messages to a txt file?

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 10:20

    You can use sink() to divert messages as well as warnings to a file. The trick is to set the argument type="message":

    Here is an example adapted from the help for ?sink:

    setwd(tempdir())
    
    ## capture messages and errors to a file.
    zz <- file("all.Rout", open="wt")
    sink(zz, type="message")
    
    try(log("a"))
    
    ## reset message sink and close the file connection
    sink(type="message")
    close(zz)
    
    ## Display the log file
    readLines("all.Rout")
    [1] "Error in log(\"a\") : Non-numeric argument to mathematical function"
    

提交回复
热议问题