检索变量声明

Dom*_*bey 1 variables r

当我从第一次声明它的几百行时,我怎么能找到我如何首先声明某个变量.例如,我已声明如下:

a <- c(vectorA,vectorB,vectorC) 
Run Code Online (Sandbox Code Playgroud)

现在我想知道我是如何宣布它的.我怎样才能做到这一点?谢谢.

jor*_*ran 8

您可以尝试使用该history命令:

history(pattern = "a <-")
Run Code Online (Sandbox Code Playgroud)

尝试在历史记录中找到您为变量指定内容的行a.我认为这完全匹配,所以你可能需要留意空间.

实际上,如果您history在命令行输入,它似乎没有比在tempfile中保存当前历史记录,在使用中重新加载readLines然后使用它进行搜索更有效grep.修改该函数以包含更多功能应该相当简单...例如,此修改将使其返回匹配行,以便您可以将其存储在变量中:

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]
}
Run Code Online (Sandbox Code Playgroud)