Skip to content
Prev 174593 / 398506 Next

How to get commands history as a character vector instead of displaying them?

Romain Francois wrote:
i think the following is an acceptable alternative:

    history = function() {
       file = tempfile()
       on.exit(unlink(file))
       savehistory(file)
       readLines(file) }

the output is *lines* of text, but if you need whole executable
expressions, you can parse the output:

    1 + 1
    ph = parse(text=history())
    as.list(ph)
    ph[length(ph)-1]
    # expression(1 + 1)
    eval(ph[length(ph)-1])
    # [1] 2


vQ