RStudio 中奇怪的 addTaskCallback 工作 [英] Strange addTaskCallback work in RStudio

查看:47
本文介绍了RStudio 中奇怪的 addTaskCallback 工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我从奇怪"问题循环中的下一个问题.

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

我发现 R 控制台和 RStudio 中的代码执行存在相同的差异,但无法理解其原因.正如我之前在 R.NET 中跟踪包的错误工作

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

那么,让我们看看 https://search 的示例.r-project.org/library/base/html/taskCallback.html

(为了在 RStudio 中输出正确的数据,我稍微修正了它)

(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)

R 控制台中的输出:

> 
>  # 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)
> 

好的,让我们在 RStudio 中运行它.输出:

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 
> 

第三:

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

等等.

RStudio 和 R 控制台之间有一个奇怪的区别,我不知道为什么.有人可以帮助我吗?是错误还是正常,我的手是弯曲的?

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?

谢谢.

附言这篇文章与track"包的正确工作有关,因为track.start"方法包含这部分代码:

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))

我认为在 RStudio 和 R.NET 中不能正常工作

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

P.P.S.我使用 R 3.2.2 x64、RStudio 0.99.489 和 Windows 10 Pro x64.在 RRO 上这个问题在 R.NET 和 RStudio 下也存在

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

推荐答案

addTaskCallback() 将添加一个回调,在 R 执行返回到顶层时执行.当您逐行执行代码时,执行的每个语句都会将控制权返回给顶层,并且会执行回调.

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.

当在 source() 中执行时,控制不会返回,直到对 source() 的调用返回,因此回调只运行一次.

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

这篇关于RStudio 中奇怪的 addTaskCallback 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆