Skip to content

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

5 messages · Yihui Xie, Romain Francois, Erich Neuwirth +1 more

#
Hi Everyone,

I want to get the commands history as a character vector instead of
just displaying them, but the function history() just returns NULL. I
checked the source code of 'history' and could not find a solution.
Anybody has an idea? Thanks!

P. S. My original problem is, when a user opens a graphics device like
png() or pdf(), I want to know the file name used by this device. I
thought history() would help, but it could not.
R version 2.8.1 (2008-12-22)
i386-pc-mingw32

locale:
LC_COLLATE=Chinese_People's Republic of
China.936;LC_CTYPE=Chinese_People's Republic of
China.936;LC_MONETARY=Chinese_People's Republic of
China.936;LC_NUMERIC=C;LC_TIME=Chinese_People's Republic of China.936

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
Mobile: +86-15810805877
Homepage: http://www.yihui.name
School of Statistics, Room 1037, Mingde Main Building,
Renmin University of China, Beijing, 100872, China
#
Yihui Xie wrote:
history eventually calls file.show, which will use the pager option to 
determine how to show the file, so you can do something like that:

history <- function( ... ){
old.op <- options( pager = function( files, header, title, delete.file ) 
readLines( files ) ); on.exit( options( old.op ) )
utils::history(...)
}
history( pattern = "png" )

I don't see a way to get device information other than the name of the 
device (with dev.cur)

Romain

  
    
#
Your original question probably is answered by
dev.cur and dev.list
Yihui Xie wrote:

  
    
#
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
7 days later
#
Thanks, Wacek and Romain! Your solutions worked very well in RGui and
Rterm in interactive mode.

My final purpose was to obtain the file name of the postscript device
in Rweb (http://pbil.univ-lyon1.fr/Rweb/); now I found savehistory()
would not work because Rweb was non-interactive. I didn't realize it
until I try(savehistory()) and got an error message.

Now I found a solution by myself: we can list the *.ps files and pick
the most recently created (modified, visited, ...) one, e.g.

x = file.info(list.files(pattern = ".*\\.ps$"))
x = x[order(x$atime), ]
rownames(x)[nrow(x)]

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
Mobile: +86-15810805877
Homepage: http://www.yihui.name
School of Statistics, Room 1037, Mingde Main Building,
Renmin University of China, Beijing, 100872, China



On Mon, Mar 23, 2009 at 6:13 PM, Wacek Kusnierczyk
<Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote: