0

I have this R script:

test.R

library(future.callr)   
plan(callr)

executeInternalActivity<-function(){

 sink(file = "output.txt",append = TRUE)    
 print(Sys.time())
 Sys.sleep(3)
 print(Sys.time())    
 sink()

}

executeInternalActivity()    
#future({executeInternalActivity()})

When test.R is executed: source("test.R") (Note the calling to executeInternalActivity), the output.txt is created properly

[root@db1 Rscripts]# cat output.txt
[1] "2023-08-27 15:51:25 GMT"
[1] "2023-08-27 15:51:28 GMT"

However, if we change the calling to executeInternalActivity to do it with future:

#executeInternalActivity()
future({executeInternalActivity()})

... and then execute it:

source("test.R")

Only the first print appears in the output.txt file:

[root@db1 Rscripts]# cat output.txt
[1] "2023-08-27 16:10:55 GMT"

I would like all prints to appear in the output.txt file. How can I fix it?

Lev
  • 693
  • 1
  • 8
  • 24
  • New package to me, but don't you need to add a `value()` call to retrieve the output of `future()` ? – Carl Witthoft Aug 27 '23 at 20:27
  • Yes. thats true: I found this "Note that the captured standard output (stdout) will be relayed each time value() is called" from [here](https://cran.r-project.org/web/packages/future/vignettes/future-2-output.html). So, `future()` evals the expression, and `value()` returns the output. Using `value()`, works as expected. However, with `value()`, the prompt of console waits till the end of the process (It is something I want to avoid). It seems the problem is the `source(test.R)`. If I execute the script from outside R: `nohup R CMD BATCH test.R &` it works as expected. – Lev Aug 27 '23 at 21:29
  • maybe `fflush()` – Carl Witthoft Aug 28 '23 at 00:13

0 Answers0