0

I want to source a file and redirect to print the errors in a variable. I tried the next code which i found in this url In R, is it possible to redirect console output to a variable? but without success...

here is the example i run:

out<-vector("character")
con<- textConnection('out', 'wr')
sink(con)
source('some_file.R', encoding = 'UTF-8') #failed capturing
sink()
close(con)

I also found this -> 4.3. Redirecting Output to a File but also without success...

here is the example i run:

sink("script_output.txt")            # Redirect output to file
source("script.R")                   # Run the script, capturing its output
sink()                               # Resume writing output to console

I don't have idea why it's not working. Where is the wrong?

If the tags or the question is not "complete" feel free to change it.

Community
  • 1
  • 1
Manos Papadakis
  • 564
  • 5
  • 17
  • Why are you creating `con` and then not using it? Try changing `sink("out")` to `sink(con)`. Additionally, you can't pass a string to `sink`, if you want to redirect to a file, say out.txt you need to open it first with `zz <- file("out.txt", open = "wt")` and then pass the file to sink, `sink(zz); ... ; sink()` – Eumenedies Apr 10 '17 at 14:21
  • Sorry i edit my question! I paste it wrong. I have already use this and it didn't worked. It still printing in the console. – Manos Papadakis Apr 10 '17 at 14:23
  • But the first link says that i can sink to a string... – Manos Papadakis Apr 10 '17 at 14:24
  • your solution didn't worked. It still print the results. – Manos Papadakis Apr 10 '17 at 14:27
  • Could you indicate where in that question/answers anyone says you can sink to a string? I think they sink to textconnections, e.g. `sink(textconnection("results"))`. This may be a silly question, but is it printing the results because you are calling `out` at the end? – Eumenedies Apr 10 '17 at 14:32
  • sink's man says that the file can be a writable connection or a character string naming the file to write to, or NULL to stop sink-ing and inside the link the second answer is actually my first example. No it didn't print because i run the variable in the end but i i will erase it from my example. – Manos Papadakis Apr 10 '17 at 14:36
  • 1
    What does the source file contain? How is it printing output? `sink` redirects most but not all output. Generally, since you talk about errors, it sounds like you should wrap the `eval` call into `try` rather than redirecting the output. – Konrad Rudolph Apr 10 '17 at 14:39
  • It contains just one R function. I deleted a bracket in order to prompt an error. So it prints the error like i havent use sink at all. – Manos Papadakis Apr 10 '17 at 14:43
  • @Csd Yeah, that’s a use-case for `try`, not for redirecting output. – Konrad Rudolph Apr 10 '17 at 14:46
  • Is there any way to understand when source is printing an error?? try didn't worked. Should i run -> try(source("file.R") ? should i use tryCatch maybe? – Manos Papadakis Apr 10 '17 at 14:49
  • Also, `sink` can redirect either the message stream (errors) or the output stream. You have to sink to the same file twice if you want to capture both errors and output see: http://stackoverflow.com/questions/7096989/how-to-save-all-console-output-to-file-in-r , if you want to just capture errors then pass `type = "message"` – Eumenedies Apr 10 '17 at 14:56
  • 1
    @Csd What do you mean by ”try didn’t work”? It works for me, but do read the documentation of `try` and set the `silent=TRUE` argument. – Konrad Rudolph Apr 10 '17 at 15:06
  • I found a solution finally based on @Eumenedies answer. I will right an answer and ascepted. Try didn't work because i want not to find an error but also store it in a variable. – Manos Papadakis Apr 10 '17 at 21:08
  • I will accept it in 2 days... – Manos Papadakis Apr 10 '17 at 21:15

1 Answers1

0

I found a solution after all.

out<-vector("character")
con<- textConnection('out', 'wr')
sink(con,type = "message")
source('some_file.R', encoding = 'UTF-8') 
sink(type = "message")
close(con)

It's not working always. But i found a better solution. Konrad Rudolph told me to use try but i wanted tryCatch eventually. Here is the code that is working perfectly.

tryCatch(source('some_file.R', encoding = 'UTF-8') ,
         error=function(the error that occurs){
           do something wih that error.
         },
         warning=function(the warning that occurs){
           do something wih that warning.
         })
Manos Papadakis
  • 564
  • 5
  • 17