Skip to content
Prev 351242 / 398502 Next

Remove entry with sensitive information from history

To answer your question on filtering the command-line history: You can
use savehistory()/loadhistory() to rewrite the history, but like all
other solutions/suggestions, it's not guaranteed to work everywhere.
Example:

filterhistory <- function(filter) {
  stopifnot(is.function(filter))
  hf <- tempfile()
  on.exit(file.remove(hf))
  savehistory(hf)
  history <- readLines(hf)
  historyF <- filter(history)
  ## Always write the same number of history lines as
  ## read to make sure everything is overwritten,
  ## cf. 'R_HISTSIZE' in help('savehistory').
  ndropped <- length(history)-length(historyF)
  clear <- rep("'<command-line history erased>'", times=ndropped)
  historyF <- c(clear, historyF)

  writeLines(historyF, con=hf)
  loadhistory(hf)
}

update_password <- function(...) {
  filterhistory(filter=function(x) {
    str(x)
    start <- grep("update_password", x, fixed=TRUE)[1]
    x[seq_len(start-1L)]
  })
  ## ...
  cat("Hello world!\n")
}

This won't work if someone does:

foo <- update_password

and calls foo().  Then you need to use a more clever filter function,
e.g. one that drops the last call, which may be spread out on multiple
lines so not just the last line.

/Henrik

On Wed, May 27, 2015 at 2:06 AM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote: