Skip to content

as.list cannot handle an environment

1 message · Wacek Kusnierczyk

#
?as.list states that as.list can be used with environments as arguments
(what is referred to there as the '"environment" method for as.list). 
indeed,

    e = globalenv()
    as.list(e)
    # $e
    # <environment: R_GlobalEnv>

however, it seems to have problems with environment objects with a class
attribute:

    e = structure(globalenv(), class='foo')
    as.list(e)
    # Error in as.vector(x, "list") :
    #  cannot coerce type 'environment' to vector of type 'list'

the error message is misleading.  instead of complaining about the class
'foo', it says that as.list cannot coerce an environment to a list,
which is nonsense, since this is precisely what it does in the first
example above, and what is also clear from ?as.list:

"
 The '"environment"' method for 'as.list' copies the name-value
     pairs (for names not beginning with a dot) from an environment to
     a named list.
"

a different error message might be more appropriate.

vQ