create logfile for R session that stores info, error, warnings in R

47 Views Asked by At

I want to create a log file which should have info, errors, warnings, commands just like similar to RStduio console. save that in a text file.

I've tried savehistory(), sink(), Rcmd() function. but none of them are working since I'm running the R codes in non Rgui environment. is there any other possibilities to do that.

1

There are 1 best solutions below

1
stefan_aus_hannover On

sink() does not take all of those arguments. I got output running this code.

log_file <- "c:/data/session_log.txt" 
log_connection <- file(log_file, open = "a") 
sink(log_connection, type="message") 
cat("Starting the script...\n") 
warning("This is a warning message.") 
stop("This is an error message.") 
cat("Processing data...\n") 
sink() 
close(log_connection)