这是任务回调的一个很好的用例。基本上,您可以注册一个回调,它将最后一个顶级表达式存储在一个变量中,然后您可以随后访问该变量。
.lastcall <- NULL # create an empty variable to store into
# register your callback
addTaskCallback(function(expr,value,ok,visible) {
assign(".lastcall", as.character(substitute(expr))[2], .GlobalEnv)
TRUE},
name='storelast')
# storelast
# 1
# check the callback on a bunch of things
2+2
# [1] 4
.lastcall
# [1] "2 + 2"
1:4
# [1] 1 2 3 4
.lastcall
# [1] "1:4"
matrix(1:4, nrow=2)
# [,1] [,2]
# [1,] 1 3
# [2,] 2 4
.lastcall
# [1] "matrix(1:4, nrow = 2)"
a <- 2; b <- 3
.lastcall
# [1] "b <- 3"
# cleanup everything; check that everything is cleaned up
removeTaskCallback('storelast')
# [1] TRUE
2+2
# [1] 4
.lastcall # confirm 2+2 was not stored as .lastcall
# [1] ".lastcall"
rm(.lastcall)
.lastcall
# Error: object '.lastcall' not found
您还可以编写回调函数,以便它简单地将最后一次调用添加到向量(或列表或其他),以便您构建完整的命令历史记录:
.lastcall <- character() # create an empty variable to store into
# register your callback
addTaskCallback(function(expr,value,ok,visible) {
assign(".lastcall",
append(.lastcall, as.character(substitute(expr))[2]),
.GlobalEnv)
TRUE},
name='storelast')
# storelast
# 1
# check the callback on a bunch of things
2+2
# [1] 4
.lastcall
# [1] "addTaskCallback(function(expr, value, ok, visible) {\n assign(\".lastcall\", append(.lastcall, as.character(substitute(expr))[2]), .GlobalEnv)\n TRUE\n}, name = \"storelast\")"
# [2] "2 + 2"
1:4
# [1] 1 2 3 4
.lastcall
# [1] "addTaskCallback(function(expr, value, ok, visible) {\n assign(\".lastcall\", append(.lastcall, as.character(substitute(expr))[2]), .GlobalEnv)\n TRUE\n}, name = \"storelast\")"
# [2] "2 + 2"
# [3] ".lastcall"
# [4] "1:4"
matrix(1:4, nrow=2)
# [,1] [,2]
# [1,] 1 3
# [2,] 2 4
.lastcall
# [1] "addTaskCallback(function(expr, value, ok, visible) {\n assign(\".lastcall\", append(.lastcall, as.character(substitute(expr))[2]), .GlobalEnv)\n TRUE\n}, name = \"storelast\")"
# [2] "2 + 2"
# [3] ".lastcall"
# [4] "1:4"
# [5] ".lastcall"
# [6] "matrix(1:4, nrow = 2)"
a <- 2; b <- 3
.lastcall
# [1] "addTaskCallback(function(expr, value, ok, visible) {\n assign(\".lastcall\", append(.lastcall, as.character(substitute(expr))[2]), .GlobalEnv)\n TRUE\n}, name = \"storelast\")"
# [2] "2 + 2"
# [3] ".lastcall"
# [4] "1:4"
# [5] ".lastcall"
# [6] "matrix(1:4, nrow = 2)"
# [7] ".lastcall"
# [8] "a <- 2"
# [9] "b <- 3"
# cleanup everything; check that everything is cleaned up
removeTaskCallback('storelast')
# [1] TRUE
2+2
# [1] 4
.lastcall # confirm 2+2 was not added to .lastcall
# [1] "addTaskCallback(function(expr, value, ok, visible) {\n assign(\".lastcall\", append(.lastcall, as.character(substitute(expr))[2]), .GlobalEnv)\n TRUE\n}, name = \"storelast\")"
# [2] "2 + 2"
# [3] ".lastcall"
# [4] "1:4"
# [5] ".lastcall"
# [6] "matrix(1:4, nrow = 2)"
# [7] ".lastcall"
# [8] "a <- 2"
# [9] "b <- 3"
# [10] ".lastcall"
rm(.lastcall)
.lastcall
# Error: object '.lastcall' not found
但是,请记住任务回调,在您自己之后进行清理非常重要removeTaskCallback
(或者您可以编写回调函数以在满足某些条件后将其自身删除;参见?addTaskCallback
示例),否则.lastcall
将永远在您的环境中徘徊,并且不断更新不必要的。