gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:55:44 26 4
gpt4 key购买 nike

类似于 .Last.value有什么办法可以访问上次通话吗?低于预期的潜在结果.Last.call .

sum(1, 2)
# [1] 3
str(.Last.call)
# language sum(1, 2)

如果不需要从文件系统解析文件,最好。

最佳答案

last.call包不再在起重机上,但您仍然可以获得代码:

# -----------------------------------------------------------------------
# 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)

}

现在,使用它:
sum(1, 2)
# [1] 3
last.call(2)
# sum(1, 2)

关于R .Last.call 功能 - 类似于 .Last.value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30552847/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com