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 testNAIntegerRepr(self): na_int = ri.NAIntegerType() self.assertEqual("NA_integer_", repr(na_int))
def testNAIntegerInVector(self): na_int = ri.NAIntegerType() x = ri.IntSexpVector((1, na_int, 2)) self.assertTrue(x[1] is na_int) self.assertEqual(1, x[0]) self.assertEqual(2, x[2])
def testNAIntegerBinaryfunc(self): na_int = ri.NAIntegerType() self.assertTrue((na_int + 2) is na_int)
def testNAIntegertoR(self): na_int = ri.NAIntegerType() self.assertEqual(True, ri.baseenv["is.na"](na_int)[0])
def testRtoNAInteger(self): na_int = ri.NAIntegerType() r_na_int = evalr("NA_integer_")[0] self.assertTrue(r_na_int is na_int)