Retrieving Variable Declaration

前端 未结 4 965
攒了一身酷
攒了一身酷 2021-01-25 07:34

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:

相关标签:
4条回答
  • 2021-01-25 07:55

    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]
    }
    
    0 讨论(0)
  • 2021-01-25 08:03

    You can run grep 'a<-' .Rhistory from terminal (assuming that you've cdd to your working directory). ESS has several very useful history-searching functions, like (comint-history-isearch-backward-regexp) - binded to M-r by default.

    For further info, consult ESS manual: http://ess.r-project.org/Manual/ess.html

    0 讨论(0)
  • 2021-01-25 08:03

    When you define a function, R stores the source code of the function (preserving formatting and comments) in an attribute named "source". When you type the name of the function, you will get this content printed.

    But it doesn't do this with variables. You can deparse a variable, which generates an expression that will produce the variable's value but this doesn't need to be the original expression. For example when you have b <- c(17, 5, 21), deparse(b) will produce the string "c(17, 5, 21)".

    In your example, however, the result wouldn't be "c(vectorA,vectorB,vectorC)", it would be an expression that produces the combined result of your three vectors.

    0 讨论(0)
  • 2021-01-25 08:04

    I will assume you're using the default R console. If you're on Windows, you can File -> Save history and open the file in your fav text browser, or you can use function savehistory() (see help(savehistory)).

    What you need to do is get a (good) IDE, or at least a decent text editor. You will benevit from code folding, syntax coloring and much more. There's a plethora of choices, from Tinn-R, VIM, ESS, Eclipse+StatET, RStudio or RevolutionR among others.

    0 讨论(0)
提交回复
热议问题