Changeset 8009:19dd578cffd0 in orange
 Timestamp:
 06/14/11 11:50:12 (3 years ago)
 Branch:
 default
 Convert:
 98e269979068c11304bc6f0d90d93eafdac4d501
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

orange/OrangeWidgets/Prototypes/OWRScript.py
r7095 r8009 9 9 from OWItemModels import PyListModel, ModelActionsWidget 10 10 11 import Orange.misc.r as r 12 13 globalenv = r.globalenv 14 11 15 import rpy2.robjects as robjects 12 16 import rpy2.rlike.container as rlc 13 17 14 if hasattr(robjects, "DataFrame"): # rpy2 version 2.115 DataFrame = robjects.DataFrame16 else: # rpy2 version 2.017 DataFrame = robjects.RDataFrame18 19 if hasattr(robjects, "Matrix"): # rpy2 version 2.120 Matrix = robjects.Matrix21 else: # rpy2 version 2.022 Matrix = robjects.RMatrix23 24 if hasattr(robjects, "globalenv"): # rpy2 version 2.125 globalenv = robjects.globalenv26 else: # rpy2 version 2.027 globalenv = robjects.globalEnv28 29 if hasattr(robjects, "NA_Real"):30 NA_Real = robjects.NA_Real31 else:32 NA_Real = robjects.r("NA")33 34 if hasattr(robjects, "NA_Integer"):35 NA_Real = robjects.NA_Integer36 else:37 NA_Real = robjects.r("NA")38 39 if hasattr(robjects, "NA_Character"):40 NA_Real = robjects.NA_Character41 else:42 NA_Real = robjects.r("NA")43 44 45 def ExampleTable_to_DataFrame(examples):46 attrs = [ attr for attr in examples.domain.variables if attr.varType in \47 [orange.VarTypes.Continuous, orange.VarTypes.Discrete, orange.VarTypes.String]]48 def float_or_NA(value):49 if value.isSpecial():50 return NA_Real51 else:52 return float(value)53 54 def int_or_NA(value):55 if value.isSpecial():56 return NA_Integer57 else:58 return int(value)59 60 def str_or_NA(value):61 if value.isSpecial():62 return NA_Character63 else:64 return str(value)65 66 data = []67 for attr in attrs:68 if attr.varType == orange.VarTypes.Continuous:69 data.append((attr.name, robjects.FloatVector([float_or_NA(ex[attr]) for ex in examples])))70 elif attr.varType == orange.VarTypes.Discrete:71 intvec = robjects.IntVector([int_or_NA(ex[attr]) for ex in examples])72 # factor.levels = robjects.StrVector(list(attr.values))73 data.append((attr.name, intvec))74 elif attr.varType == orange.VarTypes.String:75 data.append((attr.name, robjects.StrVector([str_or_NA(ex[attr]) for ex in examples])))76 77 r_obj = DataFrame(rlc.TaggedList([v for _, v in data], [t for t,_ in data]))78 return r_obj79 80 81 def SymMatrix_to_Matrix(matrix):82 83 v = robjects.FloatVector([e for row in matrix for e in row])84 85 r_obj = robjects.r['matrix'](v, nrow=matrix.dim)86 return r_obj87 88 89 18 class RPy2Console(PythonConsole): 90 19 … … 115 44 for key, val in locals.items(): 116 45 if isinstance(val, orange.ExampleTable): 117 dataframe = ExampleTable_to_DataFrame(val)118 globalenv[key] = 46 dataframe = r.dataframe(val) 47 globalenv[key] = dataframe 119 48 elif isinstance(val, orange.SymMatrix): 120 matrix = SymMatrix_to_Matrix(val)49 matrix = r.matrix(val) 121 50 globalenv[key] = matrix 122 51
Note: See TracChangeset
for help on using the changeset viewer.