From murdoch@dunc@n @ending from gm@il@com  Tue May  8 20:58:43 2018
From: murdoch@dunc@n @ending from gm@il@com (Duncan Murdoch)
Date: Tue, 8 May 2018 14:58:43 -0400
Subject: [Rd] unlist errors on a nested list of empty lists
In-Reply-To: <CAJMBSp1UX5z1Ou3bPdc0Nk4GT1Q1oqdqWEfs3wHH_r130ettrw@mail.gmail.com>
References: <CAJMBSp1UX5z1Ou3bPdc0Nk4GT1Q1oqdqWEfs3wHH_r130ettrw@mail.gmail.com>
Message-ID: <5947f0e5-ab4e-4ded-2605-01f2c1733ddc@gmail.com>

On 08/05/2018 1:48 PM, Steven Nydick wrote:
> Reproducible example:
> 
> x <- list(list(list(), list()))
> unlist(x)
> 
> *> Error in as.character.factor(x) : malformed factor*

The error comes from the line

structure(res, levels = lv, names = nm, class = "factor")

which is called because unlist() thinks that some entry is a factor, 
with NULL levels and NULL names.  It's not legal for a factor to have 
NULL levels.  Probably it should never get here; the earlier test

if (.Internal(islistfactor(x, recursive))) {

should have been false, and then the result would have been

.Internal(unlist(x, recursive, use.names))

(with both recursive and use.names being TRUE), which returns NULL.

Duncan Murdoch


> 
> What should happen:
> 
> unlist(x)
>> NULL
> 
> R.version
> platform       x86_64-apple-darwin15.6.0
> arch           x86_64
> os             darwin15.6.0
> system         x86_64, darwin15.6.0
> status
> major          3
> minor          5.0
> year           2018
> month          04
> day            23
> svn rev        74626
> language       R
> version.string R version 3.5.0 (2018-04-23)
> nickname       Joy in Playing
>



[Rd] unlist errors on a nested list of empty lists
Duncan Murdoch