获取最后一个顶级命令作为字符串

时间:2013-09-24 04:12:17

标签: r

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

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]
}

并且这不是太糟糕但是我想知道是否有办法在没有先写入文件的情况下将历史记录作为字符。

2 个答案:

答案 0 :(得分:14)

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

.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自行清理后非常重要(或者您可以在满足某些条件后编写回调函数来自行删除;请参阅{{1例如,否则?addTaskCallback将永远留在您的环境中并不断更新。

答案 1 :(得分:-1)

lastcmd <- function(){
   tmp<-tempfile()
   savehistory(tmp)
   readLines(tmp)[length(readLines(tmp))-1]
}

您认为它更好吗?