Take the 2-minute tour ×

Is there a way to show the complete internal structure of an R object in an unambiguous and human readable way?

The str function doesn't exactly do this, as it shows custom representations. For example, applying it to an igraph object gives something like

IGRAPH U--- 3 3 -- Full graph
+ attr: name (g/c), loops (g/x)
+ edges:
[1] 1--2 1--3 2--3

This is nice and readable, but it's specific to igraph objects (it's clear that it uses a custom formatting for them). I'm looking for something general. I found dput, and for a while I thought that this provides complete information. The same igraph object is shown as

structure(list(3, FALSE, c(1, 2, 2), c(0, 0, 1), c(0, 1, 2), 
    c(0, 1, 2), c(0, 0, 1, 3), c(0, 2, 3, 3), list(c(1, 0, 1), 
        structure(list(name = "Full graph", loops = FALSE), .Names = c("name", 
        "loops")), list(), list())), class = "igraph")

But then I read about pairlists in the R Language Definition and I noticed that dput(pairlist(1,2)) gives list(1,2). The information that we started with a pairlist is gone.

So is there something similar to dput that shows the internal structure of an R object and gives complete information about it? (The main reason I want this is that it would help me understand the structure of R objects better.)

If there isn't, how would I query an R object to get enough information about it (in a human readable way---not machine readable) to be able to reconstruct it completely?

share|improve this question
How about .Internal(inspect(foo))? –  Joshua Ulrich May 3 '13 at 20:16
I think dput is what you want and the pairlist exception is just that - an exception. As far as I understand you're not supposed to use pairlist's, so the issue of dput'ing it is a bit moot. –  eddi May 3 '13 at 20:18
Thanks for the reply @eddi, why don't you put that as an answer? –  Szabolcs May 3 '13 at 20:31
I read in your dput output in and str-ed it. I got the standard output for a list with an extra class. I guess you're getting something different because igraphs come from some package. I wonder if there's something like base::str (which doesn't exist) that you can use here. –  Frank May 3 '13 at 20:45
add comment

2 Answers

up vote 8 down vote accepted

I don't have much more to add besides my comment, so this is just for completeness for future generations :)

dput is doing what you want. With rare exceptions, one of them being pairlist (I assume there might be other exceptions as well, but I don't actually know what they are), it will not be exactly the same object, but, at least in the case of pairlist there is a reason for that. Since pairlist is not supposed to be used outside of internal code, the output of dput can be considered to be doing the user a favor by converting an internal object into the equivalent external one.

share|improve this answer
There are more exceptions. Environments are dput'ed simply as "<environment>", even if they have attributes. Closures lose srcref attributes, the enclosing environment and the bytecode (if present). –  Ferdinand.kraft May 4 '13 at 1:33
add comment

dput is a somewhat disappointing solution, as the result is very messy. (It is, however, better than what I was doing before.)

I'd suggest (1) dput, (2) make a copy, (3) assign the base class to the result (as seen in the dput) and (4) str it.

In this case:

> x<-dput(my_graph)
> class(x)<-"list"
> str(x)
List of 9
 $ : num 3
 $ : logi FALSE
 $ : num [1:3] 1 2 2
 $ : num [1:3] 0 0 1
 $ : num [1:3] 0 1 2
 $ : num [1:3] 0 1 2
 $ : num [1:4] 0 0 1 3
 $ : num [1:4] 0 2 3 3
 $ :List of 4
  ..$ : num [1:3] 1 0 1
  ..$ :List of 2
  .. ..$ name : chr "Full graph"
  .. ..$ loops: logi FALSE
  ..$ : list()
  ..$ : list()
share|improve this answer
I haven't used dput before, so I don't know how general this approach is. This is basically the same solution @eddi gave, but with some new steps. I learned a lot from his answer and the comments above. What I've written here was just too long to be a comment on his answer, which is already good enough. I hope someone comes along with a more satisfying answer, though. (I starred the question.) .Internal(inspect()) (which @Joshua Ulrich mentioned) might be the next best thing, but I'm not yet programmer enough to read that output at a glance. –  Frank May 4 '13 at 2:37
add comment

Your Answer


