在我的代码中,我接受来自用户的命令:
input <- readline("prompt> ")我希望将这些命令添加到readline历史记录中,这样用户就可以用向上和向下箭头翻回它们,等等。
有没有R命令或包可以做到这一点?
发布于 2014-05-18 20:51:47
您可以尝试如下所示:
getlineandsave <- function(...){
input <- readline('prompt>', ...)
# load current history
file1 <- tempfile("Rrawhist")
savehistory(file1)
# append new command to history
cat(input, '\n', file=file1)
loadhistory(file1)
unlink(file1)
return(input)
}
history() # load history before line
getlineandsave() # this will create a `readline` prompt
"this is a test line to enter at prompt"
history() # check history after line entered在这里,您将创建一个包装readline的函数,该函数将当前历史记录保存到文件中,将用户输入的内容附加到该文件中,然后从保存的文件中重新加载历史记录。
https://stackoverflow.com/questions/23720361
复制相似问题