打印工作区中的所有对象

时间:2021-05-18 23:13:31

I cannot find out how to list and print all objects in a workspace. I'd like to see them all and understand what's going on. For example, ls() gives you 30 objects. How, besides typing them individually, is it possible to display them all. Seems so trivial, the solution will probably quite embarrasing. The closest I've come was ls.str() and the idea of looping over the objects.

我无法找到如何列出和打印工作区中的所有对象。我希望看到所有这些,并了解正在发生的事情。例如,ls()为您提供30个对象。除了单独输入外,还可以显示所有内容。看起来如此微不足道,解决方案可能会非常尴尬。我最接近的是ls.str()和循环对象的想法。

Edit: This is not for data frames. I have a workspace full of functions, without data, and like to understand which ones reference which etc.

编辑:这不适用于数据框。我有一个充满功能的工作区,没有数据,并且想要了解哪些参考哪些等等。

5 个解决方案

#1


24  

Do you mean 'display' in the sense of "for every object in ls(), I want to see what I would see if I typed it into the prompt"? What if you have some matrix that's 1000x10000 - you still want to print it? I personally like ls.str() - I think it gives a nice concise overview of everything, and handles the case I just mentioned nicely.

你的意思是“显示”意味着“对于ls()中的每个对象,我想看看如果我在提示符中键入它会看到什么”?如果你有一些1000x10000的矩阵怎么办 - 你还想打印吗?我个人喜欢ls.str() - 我认为它给出了一个非常简洁的概述,并处理了我刚刚提到的案例。

However if you want to basically "display" every object in the sense of typing each on the prompt, I'd suggest a loop:

但是,如果你想在提示符中输入每个对象的基本上“显示”每个对象,我建议一个循环:

for ( obj in ls() ) { print(get(obj)) }

Since ls() returns a character vector of variable names, I need to use get(obj) which gets the variable whose name is in obj.

由于ls()返回变量名的字符向量,我需要使用get(obj)来获取名称在obj中的变量。

You may wish to do a variation of this in order to print the variable name too, e.g.

您可能希望对此进行更改以便打印变量名称,例如

for ( obj in ls() ) { cat('---',obj,'---\n'); print(get(obj)) }

As an example:

举个例子:

> a <- 1
> b <- LETTERS[1:10]
> c <- data.frame(a=LETTERS[1:10],b=runif(10))
> for ( obj in ls() ) { cat('---',obj,'---\n'); print(get(obj)) }
--- a ---
[1] 1
--- b ---
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
--- c ---
   a         b
1  A 0.1087306
2  B 0.9577797
3  C 0.8995034
4  D 0.1434574
5  E 0.3548047
6  F 0.1950219
7  G 0.1453959
8  H 0.4071727
9  I 0.3324218
10 J 0.4342141

This does have a drawback though - next time you call ls() there's now an obj in there. I'm sure there's some workaround though.

这确实有一个缺点 - 下次你调用ls()时,现在有一个obj。我确定有一些解决方法。

Anyhow, I think I still prefer ls.str() for the way it handles big objects (but I work with a lot of huge (millions of elements) matrices, so that's my preference).

无论如何,我认为我仍然更喜欢ls.str()处理大对象的方式(但我使用了很多巨大的(数百万个元素)矩阵,所以这是我的偏好)。

#2


8  

I find that using RStudio allows me a view on all objects in the environment and direct interaction with each. I am sure that a good IDE will allow the sort of exploration that your question seems to require. This would especially be useful to give you a view on a large number of objects.

我发现使用RStudio可以查看环境中的所有对象并直接与每个对象进行交互。我确信一个好的IDE将允许您的问题似乎需要的那种探索。这对于为您提供大量对象的视图尤其有用。

#3


6  

Trust me: you really don't want to print all the contents of all your objects. Just imagine printing out matrix(1:1e5,100,1000) :-( . There are some useful R tools like summary , table, and str which generally tell you enough about a data object for you to know what it is and what you want to do with it. If you have more specific concerns, e.g., "Which of my dataframes have NA values?" , you can write commands or mini-functions to do the looking.
I wrote some for myself with names like lstype(objtype='closure') , which lists all objects of the designated kind.

相信我:你真的不想打印所有对象的所有内容。想象一下打印出矩阵(1:1e5,100,1000):-(。有一些有用的R工具,如summary,table和str,它们通常会告诉你足够的数据对象,让你知道它是什么以及你是什么如果您有更具体的问题,例如“我的哪个数据帧有NA值?”,您可以编写命令或迷你函数来查找。我为自己写了一些名字,如lstype(objtype) ='closure'),列出指定种类的所有对象。

#4


1  

The command

命令

mget(ls())

should do what you need.

应该做你需要的。

For a fresh-opened workspace with some matrices and some vectors it works well for me.

对于一个带有一些矩阵和一些向量的新打开的工作空间,它对我很有用。

#5


0  

If you have one or more large objects in your workspace (e.g. vectors, matrices, and/or lists) the following bit of code should give you compact output.

如果工作区中有一个或多个大对象(例如向量,矩阵和/或列表),则下面的代码应该为您提供紧凑的输出。

for ( obj in ls() ) { 
  cat('---',obj,'---\n');
  if ( class(get(obj)) == 'matrix'  ){
    print( get(obj)[1:min(ncol(get(obj)),10),1:min(ncol(get(obj)),10)] ) 
  }else if ( class(get(obj)) == 'numeric' |  class(get(obj)) == 'integer'  ){
    print( get(obj)[1:min(length(get(obj)),10)] )
  }else if( class(get(obj)) == 'list'){
    for (i in 1:length(get(obj))){
      if ( class(get(obj)) == 'matrix'  ){
        print( get(obj)[[i]][1:min(ncol(get(obj)[[i]]),10),1:min(ncol(get(obj)[[i]]),10)] ) 
      }else if ( class(get(obj)[[i]]) == 'numeric' |  class(get(obj)[[i]]) == 'integer'  ){
        print( get(obj)[[i]][1:min(length(get(obj)[[i]]),10)] )
      }else{
        print( get(obj)[[i]] ) 
      } 
    }
  }else{
    print( get(obj) ) 
  }
}

#1


24  

Do you mean 'display' in the sense of "for every object in ls(), I want to see what I would see if I typed it into the prompt"? What if you have some matrix that's 1000x10000 - you still want to print it? I personally like ls.str() - I think it gives a nice concise overview of everything, and handles the case I just mentioned nicely.

你的意思是“显示”意味着“对于ls()中的每个对象,我想看看如果我在提示符中键入它会看到什么”?如果你有一些1000x10000的矩阵怎么办 - 你还想打印吗?我个人喜欢ls.str() - 我认为它给出了一个非常简洁的概述,并处理了我刚刚提到的案例。

However if you want to basically "display" every object in the sense of typing each on the prompt, I'd suggest a loop:

但是,如果你想在提示符中输入每个对象的基本上“显示”每个对象,我建议一个循环:

for ( obj in ls() ) { print(get(obj)) }

Since ls() returns a character vector of variable names, I need to use get(obj) which gets the variable whose name is in obj.

由于ls()返回变量名的字符向量,我需要使用get(obj)来获取名称在obj中的变量。

You may wish to do a variation of this in order to print the variable name too, e.g.

您可能希望对此进行更改以便打印变量名称,例如

for ( obj in ls() ) { cat('---',obj,'---\n'); print(get(obj)) }

As an example:

举个例子:

> a <- 1
> b <- LETTERS[1:10]
> c <- data.frame(a=LETTERS[1:10],b=runif(10))
> for ( obj in ls() ) { cat('---',obj,'---\n'); print(get(obj)) }
--- a ---
[1] 1
--- b ---
 [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
--- c ---
   a         b
1  A 0.1087306
2  B 0.9577797
3  C 0.8995034
4  D 0.1434574
5  E 0.3548047
6  F 0.1950219
7  G 0.1453959
8  H 0.4071727
9  I 0.3324218
10 J 0.4342141

This does have a drawback though - next time you call ls() there's now an obj in there. I'm sure there's some workaround though.

这确实有一个缺点 - 下次你调用ls()时,现在有一个obj。我确定有一些解决方法。

Anyhow, I think I still prefer ls.str() for the way it handles big objects (but I work with a lot of huge (millions of elements) matrices, so that's my preference).

无论如何,我认为我仍然更喜欢ls.str()处理大对象的方式(但我使用了很多巨大的(数百万个元素)矩阵,所以这是我的偏好)。

#2


8  

I find that using RStudio allows me a view on all objects in the environment and direct interaction with each. I am sure that a good IDE will allow the sort of exploration that your question seems to require. This would especially be useful to give you a view on a large number of objects.

我发现使用RStudio可以查看环境中的所有对象并直接与每个对象进行交互。我确信一个好的IDE将允许您的问题似乎需要的那种探索。这对于为您提供大量对象的视图尤其有用。

#3


6  

Trust me: you really don't want to print all the contents of all your objects. Just imagine printing out matrix(1:1e5,100,1000) :-( . There are some useful R tools like summary , table, and str which generally tell you enough about a data object for you to know what it is and what you want to do with it. If you have more specific concerns, e.g., "Which of my dataframes have NA values?" , you can write commands or mini-functions to do the looking.
I wrote some for myself with names like lstype(objtype='closure') , which lists all objects of the designated kind.

相信我:你真的不想打印所有对象的所有内容。想象一下打印出矩阵(1:1e5,100,1000):-(。有一些有用的R工具,如summary,table和str,它们通常会告诉你足够的数据对象,让你知道它是什么以及你是什么如果您有更具体的问题,例如“我的哪个数据帧有NA值?”,您可以编写命令或迷你函数来查找。我为自己写了一些名字,如lstype(objtype) ='closure'),列出指定种类的所有对象。

#4


1  

The command

命令

mget(ls())

should do what you need.

应该做你需要的。

For a fresh-opened workspace with some matrices and some vectors it works well for me.

对于一个带有一些矩阵和一些向量的新打开的工作空间,它对我很有用。

#5


0  

If you have one or more large objects in your workspace (e.g. vectors, matrices, and/or lists) the following bit of code should give you compact output.

如果工作区中有一个或多个大对象(例如向量,矩阵和/或列表),则下面的代码应该为您提供紧凑的输出。

for ( obj in ls() ) { 
  cat('---',obj,'---\n');
  if ( class(get(obj)) == 'matrix'  ){
    print( get(obj)[1:min(ncol(get(obj)),10),1:min(ncol(get(obj)),10)] ) 
  }else if ( class(get(obj)) == 'numeric' |  class(get(obj)) == 'integer'  ){
    print( get(obj)[1:min(length(get(obj)),10)] )
  }else if( class(get(obj)) == 'list'){
    for (i in 1:length(get(obj))){
      if ( class(get(obj)) == 'matrix'  ){
        print( get(obj)[[i]][1:min(ncol(get(obj)[[i]]),10),1:min(ncol(get(obj)[[i]]),10)] ) 
      }else if ( class(get(obj)[[i]]) == 'numeric' |  class(get(obj)[[i]]) == 'integer'  ){
        print( get(obj)[[i]][1:min(length(get(obj)[[i]]),10)] )
      }else{
        print( get(obj)[[i]] ) 
      } 
    }
  }else{
    print( get(obj) ) 
  }
}