检索变量声明 [英] Retrieving Variable Declaration

查看:33
本文介绍了检索变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能找到几百的时候我是怎么第一次声明某个变量的从我第一次声明的地方开始.例如,我声明了以下内容:

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:

a <- c(vectorA,vectorB,vectorC) 

现在我想看看我是如何声明的.我怎样才能做到这一点?谢谢.

and now I want to see how I declared it. How can I do that? Thanks.

推荐答案

您可以尝试使用 history 命令:

You could try using the history command:

history(pattern = "a <-")

尝试在历史记录中查找您将某些内容分配给变量 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.

确实,如果您在命令行中输入 history,它似乎并没有做任何比将当前历史记录保存在临时文件中、使用 readLines<重新加载它更有趣的事情/code> 然后使用 grep 搜索它.修改该函数以包含更多功能应该相当简单……例如,此修改将导致它返回匹配的行,以便您可以将其存储在变量中:

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]
}

这篇关于检索变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆