以下是 getGraphicsEvent 的源代码:
功能(提示=“等待输入”,onMouseDown = NULL,onMouseMove = NULL,
onMouseUp = NULL,onKeybd = NULL,consolePrompt = 提示)
{
如果(!交互式())
返回(空)
如果 (!missing(prompt) || !missing(onMouseDown) || !missing(onMouseMove) ||
!missing(onMouseUp) || !missing(onKeybd)) {
setGraphicsEventHandlers(提示 = 提示,onMouseDown = onMouseDown,
onMouseMove = onMouseMove,onMouseUp = onMouseUp,
onKeybd = onKeybd)
}
.External2(C_getGraphicsEvent, consolePrompt)
}
您可以看到为什么它返回 NULL,因为这是用if (!interactive()) return(NULL)
. 试试这个插入你的原始代码:
getGraphicsEvent2 = function (prompt = "Waiting for input", onMouseDown = NULL, onMouseMove = NULL,
onMouseUp = NULL,onKeybd = NULL,consolePrompt = 提示)
{
# if (!interactive())
# 返回(空)
如果 (!missing(prompt) || !missing(onMouseDown) || !missing(onMouseMove) ||
!missing(onMouseUp) || !missing(onKeybd)) {
setGraphicsEventHandlers(提示 = 提示,onMouseDown = onMouseDown,
onMouseMove = onMouseMove,onMouseUp = onMouseUp,
onKeybd = onKeybd)
}
.External2(C_getGraphicsEvent, consolePrompt)
}
环境(getGraphicsEvent2)= 环境(grDevices::getGraphicsEvent)
拖动图(rnorm(1000),rnorm(1000))
kbd = 功能(键){
if (key == "q") { "Quit" } else NULL }
getGraphicsEvent2("等待输入", onKeybd = kbd)
它似乎失去了功能(即我无法使用 X11 作为图形设备在 OSX 上操作绘图),但至少绘图保持不变。该函数的内部代码调用最近发生了变化(要确认,请查看 R 的两个版本的源代码 - R 2.6.2 中此函数的代码,例如在 R-2.6.2/src/library/grDevices /R/gevents.R)。