How can I find how did I first declare a certain variable when I am a few hundred lines down from where I first declared it. For example, I have declared the following:
You could try using the history
command:
history(pattern = "a <-")
to try to find lines in your history where you assigned something to the variable a
. I think this matches exactly, though, so you may have to watch out for spaces.
Indeed, if you type history
at the command line, it doesn't appear to be doing anything fancier than saving the current history in a tempfile, loading it back in using readLines
and then searching it using grep
. It ought to be fairly simple to modify that function to include more functionality...for example, this modification will cause it to return the matching lines so you can store it in a variable:
myHistory <- function (max.show = 25, reverse = FALSE, pattern, ...)
{
file1 <- tempfile("Rrawhist")
savehistory(file1)
rawhist <- readLines(file1)
unlink(file1)
if (!missing(pattern))
rawhist <- unique(grep(pattern, rawhist, value = TRUE,
...))
nlines <- length(rawhist)
if (nlines) {
inds <- max(1, nlines - max.show):nlines
if (reverse)
inds <- rev(inds)
}
else inds <- integer()
#file2 <- tempfile("hist")
#writeLines(rawhist[inds], file2)
#file.show(file2, title = "R History", delete.file = TRUE)
rawhist[inds]
}