-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMatR.m
214 lines (181 loc) · 6.22 KB
/
MatR.m
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
% Basic class for interacting with R through RServe Java client
classdef MatR < handle
properties
host = 'localhost'
port = 6311
end
properties(Dependent = true)
isConnected
end
properties(SetAccess = private, Hidden = true)
connection
result
pid
end
methods
function self = MatR()
self.connect();
% Some R-side utilities
path = fileparts(which('MatR'));
self.source(['"' path filesep 'utils.R' '"']);
end
function isConnected = get.isConnected(self)
try
isConnected = self.connection.isConnected();
catch
isConnected = false;
end
end
function connect(self)
import org.rosuda.REngine.Rserve.*;
assert(exist('RConnection')==8,...
'RServe java components not installed. Run setupMatR.');
self.connection = RConnection(self.jstr(self.host),self.port);
fprintf(1,'%s\n',self.Rversion);
% Determine PID in case we need to kill
self.pid = self.connection.eval('Sys.getpid()').asInteger();
end
function self = close(self)
self.connection.close();
end
function ver = Rversion(self)
ver = self.connection.eval('R.version.string');
ver = char(ver.asString());
end
function library(self,lib)
if iscell(lib)
for i = 1:numel(lib)
self.connection.eval(['library(' lib{i} ')']);
end
elseif ischar(lib)
self.connection.eval(['library(' lib ')']);
else
error('Invalid library specification');
end
end
function list = packages(self)
%list = self.connection.eval('(.packages())');
list = self.connection.eval(['capture.output(' ...
'for (package_name in sort(loadedNamespaces())) {'...
'print(paste(package_name, packageVersion(package_name)))})']);
list = cell(list.asStrings);
end
function wd = pwd(self)
wd = char(self.connection.eval('getwd()').asString());
end
function list = ls(self)
list = cell(self.connection.eval('list.files()').asStrings());
end
function source(self,path)
assert(ischar(path),'Input must be a string');
self.eval(['source(' path ')']);
end
function whos(self)
msg = self.connection.eval('capture.output(lsos())');
disp(cell(msg.asStrings));
end
function self = eval(self,expression)
% Evaluate expression returning results to client
self.result = self.connection.eval(expression);
end
function self = voidEval(self,expression)
% Evaluate expression without returning results to client
self.connection.voidEval(expression);
end
function plot(self,expression)
id = char(java.util.UUID.randomUUID.toString);
self.eval(['jpeg("' [id '.jpg'] '",width=800,height=800,quality=100)']);
self.voidEval(expression);
self.voidEval('dev.off()');
figure;
img = imread([self.pwd filesep [id '.jpg']]);
imshow(img);
axis off;
axis image;
delete([self.pwd filesep [id '.jpg']]);
end
function assign(self,name,value)
self.connection.assign(MatR.jstr(name),value);
end
function shutdown(self)
self.connection.shutdown(); % Shutdown Rserve
self.close();
end
function delete(self)
self.kill();
self.close();
% TODO remove temp directories
end
function self = kill(self)
import org.rosuda.REngine.Rserve.*;
temp = RConnection();
% SIGTERM might not be understood everywhere: so using SIGKILL signal, as well.
temp.eval(['tools::pskill(' num2str(self.pid) ')']);
temp.eval(['tools::pskill(' num2str(self.pid) ', tools::SIGKILL)']);
temp.close();
end
end
methods(Static)
% Matlab to Java strings
function str = jstr(x)
if iscell(x)
n = numel(x);
str = javaArray('java.lang.String',n);
for j = 1:n
str(j) = java.lang.String(x{j});
end
elseif ischar(x)
str = java.lang.String(x);
else
error('Bad input');
end
end
% Construct Java represention of data.frame
function df = dataframe(x)
import org.rosuda.REngine.*;
l = RList;
fn = fieldnames(x);
switch class(x)
case 'struct'
% TODO: Check dimensions of each field
for i = 1:numel(fn)
val = x.(fn{i});
switch class(val)
case 'categorical'
jsa = MatR.jstr(cellstr(val));
val = REXPFactor(RFactor(jsa));
case 'char'
case {'double'}
val = REXPDouble(val);
end
l.put(java.lang.String(fn{i}),val);
end
end
df = REXP().createDataFrame(l);
end
% Convert Java representation to Matlab
function result = parse(x)
switch class(x)
case 'org.rosuda.REngine.REXPGenericVector'
if x.isList
val = x.asList();
fn = val.keys;
for i = 1:numel(fn)
str = strrep(char(fn(i)),'.','_');
result.(str) = MatR.parse(val.get(fn(i)));
end
else
%?
end
case 'org.rosuda.REngine.REXPDouble'
result = x.asDoubles();
case 'org.rosuda.REngine.REXPFactor'
result = categorical(cell(x.asStrings()));
case 'org.rosuda.REngine.REXPInteger'
result = x.asIntegers();
otherwise
error('Unrecognized class');
end
end
end
end