Output error/warning log (txt file) when running R script under command line
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"
Create warning log file when running R script
Try options(warn=1)
From ?options
:
'warn': sets the handling of warning messages. If 'warn' is
negative all warnings are ignored. If 'warn' is zero (the
default) warnings are stored until the top-level function
returns. If 10 or fewer warnings were signalled they will be
printed otherwise a message saying how many were signalled.
An object called 'last.warning' is created and can be printed
through the function 'warnings'. If 'warn' is one, warnings
are printed as they occur. If 'warn' is two or larger all
warnings are turned into errors.
How to write errors and warnings to a log file?
I'm sure of all of the pitfalls, I've read some people having trouble with sink
not releasing file access to a log file, and you could potentially forget to reset sink
to output back to console instead of the log file, which could potentially corrupt your log file. But you should be able to generate an error log by running your code to download the files through a try-catch block and writing out the error messages similar to below.
log.path <- # Path to log file
tryCatch({
# Code to attempt
log(q)
}, error = function(err.msg){
# Add error message to the error log file
write(toString(err.msg), log.path, append=TRUE)
}
)
How to make a log-file of an R-session which combines commands, results and warnings/messages/errors from the R-console
If you are running R from a Unix/Linux/Mac/etc. terminal, you can do:
R | tee mydir/mylog.txt
On windows, you can run the script in
R CMD BATCH yourscript.R
and your result will appear in the same folder as yourscript.out
Error Handling and logging in R
use sink() to divert messages as well as warnings to a file. The trick is to set the argument type="message"
refer http://www.statmethods.net/interface/io.html
and Output error/warning log (txt file) when running R script under command line
https://stat.ethz.ch/R-manual/R-devel/library/base/html/sink.html
The sink( )
function defines the direction of the output.
Description
sink diverts R output to a connection (and stops such diversions).
sink.number()
reports how many diversions are in use.
sink.number(type = "message")
reports the number of the connection currently being used for error messages.
Usage
sink(file = NULL, append = FALSE, type = c("output", "message"),
split = FALSE)
sink.number(type = c("output", "message"))
direct output to a file
sink("myfile", append=FALSE, split=FALSE)
return output to the terminal
sink()
The append option controls whether output overwrites or adds to a file. The split option determines if output is also sent to the screen as well as the output file.
Here are some examples of the sink() function.
output directed to output.txt in c:\projects directory.
output overwrites existing file. no output to terminal.
sink("c:/projects/output.txt")
output directed to myfile.txt in cwd. output is appended
to existing file. output also send to terminal.
sink("myfile.txt", append=TRUE, split=TRUE)
When redirecting output, use the cat( ) function to annotate the output.
Unix Run Command is not generating the log for R Script
as it turned out, i had a script to cleanup the output folder and was deleting these .txt files in the process.
Appreciate your inputs guys, thanks.
Related Topics
Can't Loop with R's Leaflet Package to Produce Multiple Maps
Change the Position of the Strip Label in Ggplot from the Top to the Bottom
Getting a Function Name as a String
Range Standardization (0 to 1) in R
Why Has Data.Table Defined := Rather Than Overloading <-
Categorize Continuous Variable with Dplyr
How to Resolve the "No Font Name" Issue When Importing Fonts into R Using Extrafont
How to Retry a Statement on Error
R - What Algorithm Does Geom_Density() Use and How to Extract Points/Equation of Curves
How to Create a Bipartite Network in R with Igraph or Tnet
Get Connected Components Using Igraph in R
Plotting During a Loop in Rstudio
Row-Wise Sort Then Concatenate Across Specific Columns of Data Frame