-
Notifications
You must be signed in to change notification settings - Fork 13
/
R.py
71 lines (62 loc) · 1.62 KB
/
R.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import rpy2.robjects as robjects
import numpy as np
import rpy2
def source(R_file):
'''
This function read in the R source file
input:
R_file: the name of the file
'''
robjects.r.source(R_file)
def getFunction(function_name):
'''
grab the R function from the R source file
input:
function_name: the name of the fucntion on the R script
output:
The R function
Example:
#get the R function named 'train'
train = getFunction('train')
'''
return robjects.globalenv[function_name]
def vecterize(value_list):
'''
Vecterize a python list into a R vector
input:
value_list: a python list
output:
An R vector
'''
return robjects.Vector(value_list)
def matrix(r_vector, nrow, ncol):
'''
Generate an R matrix
input:
r_vector: an R vector that we want to convert to matrix
nrow: the row that we want
output:
a R matrix
'''
return robjects.r.matrix(r_vector, nrow=nrow, ncol=ncol)
def null():
'''
output a R null value
'''
return rpy2.rinterface.NULL
def functionSource(function):
'''
print the source R function
'''
print (function.r_repr())
#def r2py(r_list, new_list):
#class_func = getFunction('class')
#_class = class_func(r_list)
#if _class[0] != 'list':
#_list = np.asarray(r_list)
#return _list
#else:
#record = []
#for i in r_list:
#_list = r2py(i)
#record.append(_list)