Strange addTaskCallback work in RStudio

坚强是说给别人听的谎言 提交于 2020-01-25 04:44:30

问题


This is my next question from cycle of "strange" questions.

I found same difference in code execution in R console and RStudio and couldn't understand reason of it. It's also connected with incorrect work of "track" package in RStudio and R.NET as I'd written before in Incorrect work of track package in R.NET

So, let's look at example from https://search.r-project.org/library/base/html/taskCallback.html

(I corrected it a little for correct data output for sum in RStudio)

times <- function(total = 3, str = "Task a") {
   ctr <- 0

   function(expr, value, ok, visible) {
    ctr <<- ctr + 1
    cat(str, ctr, "\n")
    if(ctr == total) {
      cat("handler removing itself\n")
    }
    return(ctr < total)
   }
 }

 # add the callback that will work for
 # 4 top-level tasks and then remove itself.
 n <- addTaskCallback(times(4))

 # now remove it, assuming it is still first in the list.
 removeTaskCallback(n)

## Not run: 
# There is no point in running this
# as
 addTaskCallback(times(4))

 print(sum(1:10))
 print(sum(1:10))
 print(sum(1:10))
 print(sum(1:10))
 print(sum(1:10))

## End(Not run)

An output in R console:

> 
>  # add the callback that will work for
>  # 4 top-level tasks and then remove itself.
>  n <- addTaskCallback(times(4))
Task a 1 
> 
>  # now remove it, assuming it is still first in the list.
>  removeTaskCallback(n)
[1] TRUE
> 
> ## Not run: 
> # There is no point in running this
> # as
>  addTaskCallback(times(4))
1 
1 
Task a 1 
> 
>  print(sum(1:10))
[1] 55
Task a 2 
>  print(sum(1:10))
[1] 55
Task a 3 
>  print(sum(1:10)) 
[1] 55
Task a 4 
handler removing itself
>  print(sum(1:10))
[1] 55
>  print(sum(1:10))
[1] 55
> 
> ## End(Not run)
> 

Okay, let's run this in RStudio. Output:

> source('~/callbackTst.R')
[1] 55
[1] 55
[1] 55
[1] 55
[1] 55
Task a 1 
> 

Second run give us this:

> source('~/callbackTst.R')
[1] 55
[1] 55
[1] 55
[1] 55
[1] 55
Task a 2 
Task a 1 
> 

Third:

> source('~/callbackTst.R')
[1] 55
[1] 55
[1] 55
[1] 55
[1] 55
Task a 3 
Task a 2 
Task a 1 
> 

and so on.

There is a strange difference between RStudio and R console and I don't know why. Could anyone help me? Is is bug or it's normal and I have curved hands?

Thank you.

P.S. This post connected with correct working of "track" package, because "track.start" method consist this part of code:

assign(".trackingSummaryChanged", FALSE, envir = trackingEnv)
assign(".trackingPid", Sys.getpid(), envir = trackingEnv)
if (!is.element("track.auto.monitor", getTaskCallbackNames())) 
    addTaskCallback(track.auto.monitor, name = "track.auto.monitor")
return(invisible(NULL))

which, I think, doesn't work correct in RStudio and R.NET

P.P.S. I use R 3.2.2 x64, RStudio 0.99.489 and Windows 10 Pro x64. On RRO this problem also exists under R.NET and RStudio


回答1:


addTaskCallback() will add a callback that's executed when R execution returns to the top level. When you're executing code line-by-line, each statement executed will return control to the top level, and callbacks will execute.

When executed within source(), control isn't returned until the call to source() returns, and so the callback is only run once.



来源:https://stackoverflow.com/questions/33887671/strange-addtaskcallback-work-in-rstudio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!