sequence_to_vector does not accept empty sequences

Issue #388 resolved
Skip Montanaro
created an issue

If you call rpy2.robjects.sequence_to_vector with an empty list it raises an UnboundLocalError exception (can't paste into this window). I'm using rpy2 2.7.0 and 2.8.2. Both produce the error. I think curr_type should be initialized before the loop to one of the Vector types.

Comments (5)

  1. Laurent Gautier

    Thanks.

    It should probably raise a ValueError since the suitable type of the R vector corresponding to the elements in the Python list cannot be determined. However, this would make the handling of empty lists the caller's responsibility.

    It could also mimic R's behavior, although not the most intuitive:

    > c()
    NULL
    

    May be the case for a name parameter behavior (see below) ?

    def sequence_to_vector(lst, behavior='rpy2'):
        assert behavior in ('rpy2', 'R')
        ...
    
  2. Laurent Gautier

    The way it is (with and without the fix), testing whether a Python list is empty should be done before sequence_to_vector() is called.

    What should happen if the Python list is empty might be very specific to that code (skip calculation ? report an error ? etc...).

    The function sequence_to_vector() is very close to the R function base::c(...) and if what happens after Python code calling sequence_to_vector() is not very specified (e.g., dynamic interface to R written in Python), one might want to use the behavior with c() when the sequence is empty.

    if len(lst) > 0:
        v = sequence_to_vector(lst)
    else:
        v  = robjects.NULL
    
    # continue with `v` as an R object, assuming that the code downstream can handle
    # a `v` that is `NULL`
    

    I am noticing now that there is a mismatch in behavior between R's NULL and its counterpart in rpy2.

    In R, we have:

    length(NULL)
    [1] 0
    

    In rpy2:

     >>> len(robjects.NULL)
    TypeError: object of type 'rpy2.rinterface.RNULLType' has no len()
    
  3. Log in to comment