<s> from rpy2 import rinterface <EOL> libraries = [ ] <EOL> rinterface . initr ( ) <EOL> def importLibrary ( lib ) : <EOL> if not lib in libraries : <EOL> libraries . append ( lib ) <EOL> rinterface . globalenv . get ( '<STR_LIT>' ) ( rinterface . StrSexpVector ( [ lib ] ) ) <EOL> def R ( func ) : <EOL> rfunc = rinterface . globalenv . get ( func ) <EOL> def f ( * args , ** kwargs ) : <EOL> argList = [ ] <EOL> kwargDict = { } <EOL> for a in args : <EOL> argList . append ( rType ( a ) ) <EOL> for k , v in kwargs . iteritems ( ) : <EOL> kwargDict [ k ] = rType ( v ) <EOL> rResult = rfunc ( * argList , ** kwargDict ) <EOL> length = len ( rResult ) <EOL> results = [ ] <EOL> count = <NUM_LIT:0> <EOL> while count < length : <EOL> results . append ( rResult [ count ] ) <EOL> count += <NUM_LIT:1> <EOL> return results <EOL> """<STR_LIT>""" <EOL> return f <EOL> def rType ( value ) : <EOL> if not isinstance ( value , list ) : <EOL> value = [ value ] <EOL> t = commonType ( value ) <EOL> if t == list : <EOL> v = rinterface . SexpVector ( map ( rType , value ) , rinterface . LISTSXP ) <EOL> elif t == float : <EOL> v = rinterface . FloatSexpVector ( value ) <EOL> elif t == int : <EOL> v = rinterface . IntSexpVector ( value ) <EOL> elif t == bool : <EOL> v = rinterface . BoolSexpVector ( value ) <EOL> elif t == str : <EOL> v = rinterface . StrSexpVector ( value ) <EOL> else : <EOL> v = None <EOL> return v <EOL> def commonType ( elements ) : <EOL> if len ( elements ) == <NUM_LIT:0> : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> t = type ( elements [ <NUM_LIT:0> ] ) <EOL> for item in elements : <EOL> if ( t == float or t == int ) and t != type ( item ) : <EOL> t = float <EOL> elif t != type ( item ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> return t <EOL> """<STR_LIT>""" <EOL> def r_vector_string ( ob ) : <EOL> if type ( ob ) == list : <EOL> strings = [ ] <EOL> for o in ob : <EOL> strings . append ( r_vector_string ( o ) ) <EOL> return '<STR_LIT>' + '<STR_LIT:U+002C>' . join ( strings ) + '<STR_LIT:)>' <EOL> else : <EOL> return str ( ob ) </s>
