Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show internal structure of an R object

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?

like image 693
Szabolcs Avatar asked May 03 '13 20:05

Szabolcs


2 Answers

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.

like image 50
eddi Avatar answered Sep 21 '22 07:09

eddi


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)
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")
> 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()
like image 32
Frank Avatar answered Sep 22 '22 07:09

Frank