3

我正在尝试使用该tcltk包为 R 创建一个脚本小部件。但我不知道如何创建一个停止按钮来中断来自小部件的脚本。基本上,我想要一个按钮、一个菜单选项和/或一个键绑定来中断当前脚本的执行,但我不知道如何让它工作。

一种(非理想)策略是仅使用 RGui STOP 按钮(或<ESC>控制台<Ctrl-c>上的或),但这似乎会导致 tk 小部件永久挂起。

这是基于 tcl/tk 示例 ( http://bioinf.wehi.edu.au/~wettenhall/RTclTkExamples/evalRcode.html ) 的小部件的最小示例:

require(tcltk)

tkscript <- function() {
    tt <- tktoplevel()
    txt <- tktext(tt, height=10)
    tkpack(txt)
    run <- function() {
        code <- tclvalue(tkget(txt,"0.0","end"))
        e <- try(parse(text=code))
        if (inherits(e, "try-error")) {
            tkmessageBox(message="Syntax error", icon="error")
            return()
        }
        print(eval(e))
    }
    tkbind(txt,"<Control-r>",run)
}

tkscript()

在脚本小部件中,如果您尝试执行Sys.sleep(20)然后从控制台中断,小部件会挂起。如果一个人要运行同样的事情,例如,一个无限循环,比如while(TRUE) 2+2.

我认为我遇到的可能与此处报告的错误类似:https ://bugs.r-project.org/bugzilla3/show_bug.cgi?id=14730

另外,我应该提到我在 Windows (x64) 上的 R 3.0.0 上运行它,所以问题可能是特定于平台的。

关于如何中断正在运行的脚本而不导致小部件挂起的任何想法?

4

2 回答 2

6

这取决于脚本在做什么;等待用户做某事的脚本很容易被中断(因为你可以让它监听你的中断消息),但是一个正在执行密集循环的脚本相当棘手。可能的解决方案取决于内部 Tcl 的版本。

口译员取消 - 需要 8.6

如果您使用的是 Tcl 8.6,您可以使用解释器取消来停止脚本。您所要做的就是安排:

interp cancel -unwind

运行,脚本会将控制权返回给您。一个合理的方法是使用额外的 Tcl 包TclX(或Expect)来安装一个信号处理程序,该处理程序将在收到信号时运行命令:

package require Tcl 8.6
package require TclX

# Our signal handler
proc doInterrupt {} {
    # Print a message so you can see what's happening
    puts "It goes boom!"
    # Unwind the stack back to the R code
    interp cancel -unwind
}

# Install it...
signal trap sigint doInterrupt

# Now evaluate the code which might try to run forever

在早期版本中添加信号处理是可能的,但不是那么容易,因为你不能保证事情会那么容易地返回给你;堆栈展开不存在。

执行时间限制——需要 8.5(或 8.6)

您可以尝试的另一件事是在从解释器上设置执行时间限制并在该从解释器中运行用户脚本。然后,时间限制机制将保证每隔一段时间就会给你一个陷阱,让你有机会检查中断和进行堆栈展开的方法。这是一种相当复杂的方法。

proc nextSecond {} {
    clock add [clock seconds] 1 second
}
interp create child
proc checkInterrupt {} {
    if {["decide if the R code wanted an interrupt"]} {
        # Do nothing
        return
    }
    # Reset the time limit to another second ahead
    interp limit child time -seconds [nextSecond]
}

interp limit child time -seconds [nextSecond] -command checkInterrupt
interp eval child "the user script"

认为这种机制很像操作系统的工作方式,是的,它可以停止一个紧密的循环。

使用子进程——任何版本的 Tcl

最可移植的机制是在子进程中运行脚本(与tclsh程序一起;确切的名称因版本、平台和发行版而异,但都是变体),然后在不再需要该子进程时用pskill. 这样做的缺点是您不能(轻松)将任何状态从一次执行转移到另一次执行;子进程彼此非常隔离。上面描述的其他方法可以使状态能够从另一个运行中访问:它们执行真正的中断,而这会破坏。

另外,我不确切知道如何启动子进程,以便您可以在 R 仍在运行时与它进行通信;system并且system2似乎没有给予足够的控制,并且用分叉破解某些东西是不可移植的。这里需要 R 专家。或者,使用 Tcl 脚本(在 R 进程中运行)来执行此操作:

set executable "tclsh";    # Adjust this line
set scriptfile "file/where/you/put/the_user/script.tcl"

# Open a bi-directional pipe to talk to the subprocess
set pipeline [open |[list $executable $scriptfile] "r+"]

# Get the subprocess's PID
set thePID [pid $pipeline]

这实际上可以合理地移植到 Windows(如果不是完全如此),但带有分叉的中间状态不是。

于 2013-06-01T07:37:23.397 回答
1

我似乎找到了一种解决方案,通过嵌入处理中断条件的 tk 小部件来防止eval挂起tryCatch。不幸的是,它需要来自控制台而不是小部件的中断,但它确实有效。tryCatch记录很差,所以我把它放在这里,以防其他人将来有类似的需求。

require(tcltk)

tkscript <- function() {
    tt <- tktoplevel()
    txt <- tktext(tt, height=10)
    tkpack(txt)
    run <- function() {
        code <- tclvalue(tkget(txt,"0.0","end"))
        e <- try(parse(text=code))
        if (inherits(e, "try-error")) {
            tkmessageBox(message="Parse error", icon="error")
            tkfocus(txt)
            return()
        }
        e <- tryCatch(eval(e),
            error = function(errmsg)
                tkmessageBox(message=as.character(errmsg), icon="error"),
            interrupt = function(errmsg)
                tkmessageBox(message=as.character(errmsg), icon="error")
        )
        print(eval(e))
    }
    tkbind(txt,"<Control-r>",run)
}

tkscript()

我偶然发现的另一个策略是使用tools::pskill(以pskill(Sys.getpid(), SIGINT)tk 菜单选项的形式)来中断进程,但是 - 至少在 Windows 上 - 这会终止整个 R 进程(包括 tk 小部件)。所以,这不是一个很好的解决方案,但至少似乎作为绝对后备退出一切。

于 2013-06-02T13:44:17.587 回答