Skip to content

Feature request: extend functionality of 'unlist()' by args 'delim=c("/", "_", etc.)' and 'keep.special=TRUE/FALSE'

4 messages · Janko Thyson, Duncan Murdoch

#
On 19/05/2011 8:15 AM, Janko Thyson wrote:
This is a good place to post.
The suggestions seem reasonable, but are difficult to implement.  The 
problem is that unlist() is a generic function, but there's no 
unlist.default() in R:  the default and method dispatch are implemented 
at the C level.  Normally adding arguments to the default method doesn't 
cause problems elsewhere, because methods only need to be compatible 
with the generic.  But since there's no way to modify the argument list 
of the default method in this case, the generic function would need to 
be modified, and that means every unlist method would need to be 
modified too.

So I wouldn't want to take this on.

In case someone else does, I'd suggest a different change than the 
"keep.special" argument.  I think a "coerce=TRUE" argument would be 
better:  If TRUE, you get the current behaviour, which coerces 
components according to the hierarchy listed on the help page.  If 
FALSE, then no coercion is done, and unlist() just flattens the list 
into a new one, e.g.

unlist( list(1, 2, NULL, list("A", "B")), coerce=FALSE)

would return list(1, 2, NULL, "A", "B") instead of c("1", "2", "A", "B").

One workaround I thought of was to add an element to the list that 
couldn't be coerced, but this doesn't work.  For example:

e <- environment() # can't be coerced
x <- list(1, 2, NULL, list("A", "B"), e)
unlist(x)

# Returns list(1,2,"A","B",e)

I think it would be reasonable for this version to retain the NULL, 
since it is not doing any coercion.

Duncan Murdoch