s-news
[Top] [All Lists]

Re: Is there an 'str' function for SPLUS 6?

To: james.holtman@convergys.com
Subject: Re: Is there an 'str' function for SPLUS 6?
From: Tim Hesterberg x319 <timh@insightful.com>
Date: Mon, 23 Apr 2001 08:36:32 -0700
Cc: s-news@wubios.wustl.edu
In-reply-to: <OF4A940756.08AB5DD9-ON85256A35.007F83C3@cbis.com> (james.holtman@convergys.com)
References: <OF4A940756.08AB5DD9-ON85256A35.007F83C3@cbis.com>
Reply-to: timh@insightful.com (Tim Hesterberg)
>I have just started to use SPLUS 6 on UNIX.  There was a function under
>SPLUS 3.4 (and under R) called 'str' that would print out the structure of
>an object.  

str is a function by Martin Maechler, on statlib
        http://lib.stat.cmu.edu/S/str
That version does not work on Splus6.  As of Sept 2000, Martin had
a version that ran on Splus5, but did not support objects with slots.

>Is there an equivalent function available under SPLUS 6?  This
>was very useful in debugging programs.

Here is a function I use to describe the structure of an object.
Comments or improvements are welcome.
Caveat - this is not official Insightful code, has gone through no
quality assurance here, no help file.
But I've been using it successfully for a year without finding objects
on which it failed.  It will sometimes describe the same part of
an object as both an attribute and a slot.

-- Tim Hesterberg

describe _ function(x, maxlen=20, describeAttributes=T, prefix="", attri=F){
  # Describe the structure of object x, recursively.
  # If x is a list of length longer than maxlen, only list the names of
  # the components.
  # If describeAttributes=F, then only give names of attributes.
  # Prefix and attri are intended for internal use in recursive calls.
  # If attri then attributes are being printed, use & instead of $ or @
  # Tim Hesterberg <timh@insightful.com>, 2/18/2000, comments welcome.

  # determine attributes to be shown
  ax _ attributes(x)
  ax$dim _ NULL
  ax$class _ NULL
  if(is.list(x) || class(x) != "named") ax$names _ NULL
  
  # Determine attributes to be shown separately 
  #  -- none if !describeAttributes,
  #  -- otherwise everything but names and dimnames
  if(describeAttributes){
    ax1 _ ax[match(c("names","dimnames"), names(ax), nomatch=0)]
    if(class(x) == "named") ax1$names _ NULL
    ax2 _ ax
    ax2$names _ NULL
    ax2$dimnames _ NULL
  }
  else {
    ax1 _ ax
    ax2 _ NULL
  }

  ux _ unclass(x)
  len _ length(ux)
  if(!attri){
    if(mode(x) == "numeric" && is.null(dim(x)) && len == 1)
      cat(prefix,"scalar",sep="")
    else if(is.null(x))
      cat(prefix,"NULL")
    else {
      cat(prefix, mode(x),"[",sep="")
      if(is.null(dim(x)))
        cat(" length", len)
      else
        cat(dim(x),sep=",")
      cat("]")
    }                                   # No end of line yet.

    # List class after name of object
    if(length(oldClass(x)))
      cat("  oldClass:", oldClass(x),"\n")
    else
      cat("  class:", class(x),"\n")
  }

  # Attributes
  if(length(ax1))
    cat(prefix, "attributes:", names(ax1), "\n")
  if(length(ax2))
    describe(ax2, maxlen, prefix = prefix, des=T, attri=T)

  nn _ slotNames(x)
  slotted _ length(nn) && !is.element(class(x), c("named", "structure"))
  if(!slotted){
    nn _ names(x)
    if(is.null(nn)) nn _ 1:len
  }

  if(is.list(ux) && len){
    if(len <= maxlen){
      nn[nn==""] _ (1:len)[nn==""]
      nn _ format.character(nn, justify="left")
      for(i in 1:len){
        cat(prefix,
            if(attri) " &" else if(slotted) " @" else " $",
            nn[i]," ",sep="")
        describe(ux[[i]], maxlen, prefix = paste(prefix,"  ",sep=""),
                 des=describeAttributes)
      }
    } else cat(prefix, "length > maxlen,", 
        if(!slotted && is.null(names(x))) "No names" else c("names= ",nn),
        "\n")
  }
  invisible(NULL)
}

<Prev in Thread] Current Thread [Next in Thread>