Print all objects in a workspace

拟墨画扇 提交于 2019-12-21 03:23:28

问题


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.

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.


回答1:


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.

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.

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.

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).




回答2:


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.




回答3:


If you just want the names of the variables you use:

ls()

If you want to print your variables along with the contents as well then use the command:

mget(ls())

This should do what you need.

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




回答4:


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.




回答5:


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


来源:https://stackoverflow.com/questions/10382916/print-all-objects-in-a-workspace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!