def __value_py_to_r(value, ri): """Returns the R equivalent of a python value""" val = value.value if not isinstance(val, (list, tuple)): # Is this an iterable ? if hasattr(val, '__iter__') and not isinstance(val, (str, unicode)): val = [v for v in val] else: # In R scalar values are vectors with one element # So here we put the scalar value into a list val = [val] na_value = None if value.type == 'bool': na_value = ri.NALogicalType() elif value.type == 'byte': na_value = ri.NAIntegerType( ) # I guess that's correct ? That should probably be tested though elif value.type == 'float': na_value = ri.NARealType() elif value.type == 'int': na_value = ri.NAIntegerType() elif value.type == 'string': na_value = ri.NACharacterType() # Scan the elements to replace Nones by NA val = [(na_value if v is None else v) for v in val] if value.type == 'bool': return ri.BoolSexpVector(val) if value.type == 'byte': return ri.ByteSexpVector(val) if value.type == 'float': return ri.FloatSexpVector(val) if value.type == 'int': return ri.IntSexpVector(val) if value.type == 'string': return ri.StrSexpVector(val) raise PredictionValueTypeException(value.type)
def testNALogicalInVector(self): na_bool = ri.NALogicalType() x = ri.BoolSexpVector((True, na_bool, False)) self.assertTrue(x[1] is na_bool) self.assertEqual(True, x[0]) self.assertEqual(False, x[2])
def testNALogicaltoR(self): na_lgl = ri.NALogicalType() self.assertEqual(True, ri.baseenv["is.na"](na_lgl)[0])
def testRtoNALogical(self): na_lgl = ri.NALogicalType() r_na_lgl = evalr("NA")[0] self.assertTrue(r_na_lgl is na_lgl)
def testNAIntegerRepr(self): na_bool = ri.NALogicalType() self.assertEqual("NA", repr(na_bool))