11

有没有办法将最后一个顶级命令存储到字符串中,而无需将历史记录保存到文件中并将其读回以获取最后一个命令?我有代码

lastcmd <- function(){
    tmp <- tempfile()
    savehistory(tmp)

    # If we call this function then that call will
    # be the last line in the history so we want the one
    # before that
    tail(readLines(tmp), 2)[1]
}

这还不错,但我想知道是否有一种方法可以在不先写入文件的情况下将历史记录为字符。

4

2 回答 2

15

这是任务回调的一个很好的用例。基本上,您可以注册一个回调,它将最后一个顶级表达式存储在一个变量中,然后您可以随后访问该变量。

.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将永远在您的环境中徘徊,并且不断更新不必要的。

于 2014-08-05T09:04:11.923 回答
-1
lastcmd <- function(){
   tmp<-tempfile()
   savehistory(tmp)
   readLines(tmp)[length(readLines(tmp))-1]
}

你认为它更好吗?

于 2013-09-24T06:02:10.753 回答