[R] Accessing just the value of an object but not the attributes

Martin Maechler maechler at stat.math.ethz.ch
Tue Oct 2 18:00:23 CEST 2001

>>>>> "Johann" == Johann Petrak <johann at ai.univie.ac.at> writes:

    Johann> I see the role of as.vector().  But even if as.vector() needs
    Johann> to keep its attribute-dropping side-effect for compatibility
    Johann> reasons, wouldn't a dropattr() still be a good idea?  For
    Johann> instance, I can assign attributes to closures, but wont get rid
    Johann> of them using as.vector() - and neither using as.function() by
    Johann> the way ...  So if I dont know in advance what kind of object
    Johann> it is and I do want to just access the "value" in any case I
    Johann> have to check the type ... ?

Continuing another example from R-help, where

  > foo <- function(x) { y <- x; class(y) <- "foo"; y }

Let's try

  > attr(foo,"myattr") <- "my attribute"
  > foo
  function(x) { y <- x; class(y) <- "foo"; y }
  [1] "my attribute"
  > as.vector(foo, mode="function")
  function (x) 
      y <- x
      class(y) <- "foo"

So, that *does* work for functions, and you would think

    dropAttributes <- function(x) as.vector(x, mode = mode(x))

should work in most cases {but not for environments() or stranger things
like promisses ..}. 

The reason:  "vector" is used -- and confused --
in S (the language, R being one implementation)
in at least too fundamentally different ways :

  1) an atomic vector {numeric, character,..}

  2) a "generic" vector.  List()s and expression()s being generic vectors, too.

However, as a matter of fact, as.vector() doesn't drop attributes for list()s.
{not caring if this could be considered a bug or not; as you know, "me thinks"
 particularly about as.vector() ..}

There, and in general you could use

    dropAttributesDANGER <- function(x) {attributes(x) <- NULL ; x}

the "DANGER" lying in the fact that you *do* lose all attributes, including
    dim, class, names, dimnames, ...
which might entail that  dropAttributesDANGER(obj) might print tons of lines
since print.default() is used instead of  print.<class of obj>().

If, instead, you'd use proper OO techniques, you'd have a class "foobar" and
a `value()' (say) generic with a method for your class, i.e.
   value.foobar <- function(x) {attributes(x) <- NULL ; x}

and in your code, you'd use  ` value(myspecialobject) '

BTW: Yes,   function(x) {attributes(x) <- NULL ; x}
 could probably made memory-efficient (no copying) by making it
 .Internal(); however, I don't know if it's worth it.

    Johann> Martin Maechler wrote:

    >> [...]
    >> "unfortunately" : IMO, a vector with attributes is still a vector,
    >> particularly if these are just names.. hence as.vector() should
    >> leave it alone, but -- alas -- it's for compatibility reasons too
    >> important to be changed.  So even if a new dropattributes() function
    >> would do what as.vector() does now, it would break too much code if
    >> as.vector() was changed...  Note that "unfortunately" is my personal
    >> opinion, not a general R core one.

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO D10	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list