[R] can one evaluate an expression in a comment? (or insert resultsinto history?)

Thomas Lumley tlumley at u.washington.edu
Mon Nov 8 23:58:57 CET 2004


On Mon, 8 Nov 2004 davidr at rhotrading.com wrote:

> But that doesn't put the result into the history buffer, to be written
> to a file only later when I savehistory(filename).
>
> Bert Gunter also suggested ?capture.output and ?textConnection,
> but I cannot see how to get text into the history buffer as comments,
> but with evaluated expressions (values).

You can cheat by writing out the history to a file, appending to the file, 
then reading the history back in.

> rewriteHistory <- function(comment){
  file1 <- tempfile("Rrawhist")
  on.exit(unlink(file1))
  savehistory(file1)
  conn<-file(file1,open="a")
  writeLines(comment, con=conn)
  close(conn)
  loadhistory(file1)
}

I now can get from history()

rewriteHistory("## 1984-4-12 13:00:00 GMT")
## 1984-4-12 13:00:00 GMT
rewriteHistory("TRUE == FALSE")
TRUE == FALSE

You still get the rewriteHistory() calls, though.


 	-thomas




More information about the R-help mailing list