将用户输入添加到readline历史记录

时间:2014-05-18 09:14:10

标签: r readline

在我的代码中,我接受来自用户的命令:

input <- readline("prompt> ")

我希望将这些命令添加到readline历史记录中,以便用户可以使用向上和向下箭头返回页面,等等。

是否有R命令或包可以做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

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,将当前历史记录保存到文件中,将用户输入的内容附加到该文件,然后从该保存的文件中重新加载历史记录。

相关问题