Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print all objects in a workspace

Tags:

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.

like image 638
Rico Avatar asked Apr 30 '12 11:04

Rico


People also ask

How do I list all objects in R?

ls() function in R Language is used to list the names of all the objects that are present in the working directory.

How do I print an object in R?

Most common method to print output in R program, there is a function called print() is used. Also if the program of R is written over the console line by line then the output is printed normally, no need to use any function for print that output.

How do I save an entire R workspace?

Saving the workspace in R is very easy. In case you want to save the full workspace in R, also known as workspace image (those objects that are displayed when you call the ls function), you can use the save. image function. The data will be saved in a file of type RData (also known as rda ).

Which R command list the variables in the workspace?

You can use ls() to list all variables that are created in the environment. Use ls() to display all variables.


2 Answers

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

like image 58
mathematical.coffee Avatar answered Mar 06 '23 08:03

mathematical.coffee


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.

like image 32
Cynclida Avatar answered Mar 06 '23 06:03

Cynclida