3

Can anyone tell me how to keep a simple log of errors and warnings thrown during sourcing of code in R? Specifically I want to write them to an object during sourcing so I can print them into a message after execution in case the user missed it as the lines scrolled by.

Background: I wrote a small data validation program, about 600 lines, that allows the user to select an excel file which will be automatically imported, processed, and exported again. I tried hard to include automatic checks in the code (such as checking for required column names and throwing a pop up box if not present), but am aware that I can't think of every possible error that could occur, especially with other users. What I would like is to have every error/warning/etc. that occurs during sourcing to be written to an object that I could then call later such as in a pop up box. I have been able to figure out every step except for creating the error log.

ErrorLog <- (code for collecting errors/warnings here)

(Program Code - already completed)

ErrorPopUp <- tkmessageBox(title = "ERRORS", message = paste("
Please note the following errors/warnings occurred during file processing:

", 
ErrorLog , 

"To proceed and export file please press OK.  To exit the program without      
saving, please press cancel."), icon = "warning", type = "okcancel")

(code to continue and export or quit)

I appreciate any ideas, and thx for the patience as I saw many posts on advanced topics such as creating your own error messages or sending them to windows, but none for simply writing an object listing them.

schradera
  • 115
  • 2
  • 10
  • 1
    `tryCatch`ing the errors, `cat`ing them to a file, or [this suggestion](http://stackoverflow.com/questions/11666086/output-error-warning-log-txt-file-when-running-r-script-under-command-line) – rawr Aug 19 '16 at 20:34
  • 1
    Don't have a full solution bu running R CMD BATCH file.R saves the output to a log file. Alternatively you can use knitr::spin and possible only keep error messages. – timcdlucas Aug 19 '16 at 20:37
  • hmmm... I know I don't want an external file, just an object in the global environment that clears when the program closes. I always know just enough to get get myself stuck on another problem O.o haha. I will have to learn more about tryCatch and cat but they look intriguing if I can figure them out. Thank you for the suggestion @rawr – schradera Aug 19 '16 at 20:58
  • I have knitr installed, I will have to learn to use it better it sounds like. I love programming but have a long way to go I think. Thank you @timcdlucas for the suggestions. – schradera Aug 19 '16 at 20:59
  • @rawr suggestion of sink and tryCatch has worked well for me in the past. – jdharrison Aug 19 '16 at 21:26
  • Can one of you write basic instructions on how to do this with tryCatch and cat? I will be sure to mark it as the correct answer if it works, but I have no idea how to get started doing this on my own. – schradera Aug 22 '16 at 12:41

0 Answers0