R.Last.call功能 - 类似于.Last.value

jan*_*cki 6 r

类似于.Last.value有没有办法访问最后一次通话?低于预期的潜在结果.Last.call.

sum(1, 2)
# [1] 3
str(.Last.call)
#  language sum(1, 2)
Run Code Online (Sandbox Code Playgroud)

如果它不需要从文件系统解析文件,则为bests.

Jot*_*ota 3

last.call软件包不再在 cran 上,但您仍然可以获得代码:

# -----------------------------------------------------------------------
# FUNCTION: last.call
#   Retrieves a CALL from the history and returns an unevaluated 
#   call.
#
#   There are two uses for such abilities.  
#   - To be able to recall the previous commands, like pressing the up key
#     on the terminal.
#   - The ability to get the line that called the function.
#   
#   TODO:
#   - does not handle commands seperated by ';'
#
# -----------------------------------------------------------------------

last.call <-
function(n=1) {

 f1 <- tempfile()
 try( savehistory(f1), silent=TRUE ) 
 try( rawhist <- readLines(f1), silent=TRUE )
 unlink(f1)

 if( exists('rawhist') ) { 

   # LOOK BACK max(n)+ LINES UNTIL YOU HAVE n COMMANDS 
   cmds <- expression() 
   n.lines <- max(abs(n)) 
   while( length(cmds) < max(abs(n)) ) { 
      lines <- tail( rawhist, n=n.lines )
      try( cmds <- parse( text=lines ), silent=TRUE ) 
      n.lines <- n.lines + 1 
      if( n.lines > length(rawhist) ) break 
   }
   ret <- rev(cmds)[n] 
   if( length(ret) == 1 ) return(ret[[1]]) else return(ret) 

 }

 return(NULL)

}
Run Code Online (Sandbox Code Playgroud)

现在,使用它:

sum(1, 2)
# [1] 3
last.call(2)
# sum(1, 2)
Run Code Online (Sandbox Code Playgroud)