-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunr.m
More file actions
305 lines (277 loc) · 8.45 KB
/
runr.m
File metadata and controls
305 lines (277 loc) · 8.45 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
function output = runr(filename,varargin)
% output = runr
% output = runr(filename, ...)
%
% Scan the currently executing file (or the file provided in filename) for
% the string "% rchunk" copy the following commented lines (hereafter named
% chunk) to an R script run it and return the output as a cell array of
% strings
%
% inputs:
% filename: a filename (usually the mfile that runr is called from).
% if no filename is provided, runr will try to guess where
% it's being called from...
% pairs of 'parameter', value arguments:
% num: If there are several R chunks, they can be run by number
% name: If there are several R chunks, they can be run by name.
% the name of the R chunk to run is defined inline with
% % rchunk name
% trimbefore: Exclude all lines of the output until the first
% occurrence of the string in trimbefore (default '>' at
% the beginning of a line)
% trimafter: Exclude all lines of the output after the last
% occurrence of the string in trimafter (default '>' at the
% beginning of a line).
% dir: set working directory prior to running chunk
%
% example: in mfile "testit.m"
%
% load('fisheriris')
% t = table(meas(:,1),meas(:,2),meas(:,3),meas(:,4),species);
% writetable(t,'test.csv')
% % rchunk
% %
% % a <- read.csv('test.csv')
% % fit <- lm(Var1 ~ Var2, data=a)
% % summary(fit)
% runr
%
% example: in mfile "testitagain.m"
%
% load('fisheriris')
% t = table(meas(:,1),meas(:,2),meas(:,3),meas(:,4),species);
% writetable(t,'test.csv')
% % rchunk
% %
% % a <- read.csv('test.csv')
% % fit <- lm(Var1 ~ Var2, data=a)
% % summary(fit)
% runr('testitagain.m','trimbefore','','trimafter','')
%
if ~exist('filename','var') || isempty(filename)
s = dbstack('-completenames');
if numel(s) == 1
error('Please run this from within an mfile or provide filename')
end
filename = s(2).file;
end
def = [];
def.name = '';
def.num = [];
def.trimbefore = '^>';
def.trimafter = '^>';
def.dir = cd;
cfg = vararg2cfg(varargin,def,1);
txt = readtext(filename,'\n',[],[],'textual');
starts = find(cellfun(@(x)~isempty(x),regexp(txt,'% *rchunk')));
if isempty(starts)
error(sprintf(['No R code found in ' filename '\nThere should be a line containing ''rscript starthere'' somewhere in that file']));
end
names = regexp(txt(starts),'% *rchunk(.*)','tokens');
names = cellfun(@(x)strtrim(x{1}{1}),names,'uniformoutput',0);
if not(isempty(cfg.name))% run by name
torun = regexpcell(names,cfg.name);
elseif not(isempty(cfg.num))% run by num
torun = cfg.num;
else
torun = 1;
end
iline = starts(torun)+1;
start = iline;
txt{iline} = strtrim(txt{iline});
while ~isempty(txt{iline}) && strcmp(txt{iline}(1),'%')
iline = iline+1;
txt{iline} = strtrim(txt{iline});
end
stop = iline-1;
script = txt(start:stop);
script = cellfun(@(x) x(2:end),script,'uniformoutput',0);
fid = fopen('tmp.R','wt');
fprintf(fid,'setwd("%s")\n',cfg.dir);
for i = 1:numel(script)
fprintf(fid,'%s\n',script{i});
end
fclose(fid);
!R CMD BATCH --no-save --no-restore tmp.R
output = readtext('tmp.Rout','\n',[],[],'textual');
if ~isempty(cfg.trimbefore)
starts = find(cellfun(@(x)~isempty(x),regexp(output,cfg.trimbefore)));
if not(isempty(starts))
output = output(starts(1):end);
end
end
if ~isempty(cfg.trimafter)
stop = find(cellfun(@(x)~isempty(x),regexp(output,cfg.trimafter)));
if not(isempty(stop))
output = output(1:stop(end));
end
end
function cfg = vararg2cfg(vararg, def,keepempty)
% cfg = vararg2cfg(vararg, def)
% cfg = vararg2cfg(vararg, def,keepempty)
% convert cellarray vararg to a cfg structure, setting fields of cfg with
% default values defined in structure def.
% vararg should either be a 1x1 cell with a cfg structure or a cell array
% of N elements with N/2 pairs of 'param',value pairs.
% if keepempty is provided and true, then empty fields in s will be left
% empty. otherwise they are populated with default values. default is
% false.
if not(exist('keepempty','var'))
keepempty = 0;
end
if numel(vararg) == 1
cfg = setdef(vararg{1},def,keepempty);
elseif ~rem(numel(vararg),2)
cfg = setdef(vararg2struct(vararg),def,keepempty);
else
error('arguments in vararg should come in pairs')
end
function idx = regexpcell(c,pat, cmds)
% idx = regexpcell(c,pat, cmds)
%
% Return indices idx of cells in c that match pattern(s) pat (regular expression).
% Pattern pat can be char or cellstr. In the later case regexpcell returns
% indexes of cells that match any pattern in pat.
%
% cmds is a string that can contain one or several of these commands:
% 'inv' return indexes that do not match the pattern.
% 'ignorecase' will use regexpi instead of regexp
% 'exact' performs an exact match (regular expression should match the whole strings in c).
% 'all' (default) returns all indices, including repeats (if several pat match a single cell in c).
% 'unique' will return unique sorted indices.
% 'intersect' will return only indices in c that match ALL the patterns in pat.
%
% v1 Maximilien Chaumon 01/05/09
% v1.1 Maximilien Chaumon 24/05/09 - added ignorecase
% v2 Maximilien Chaumon 02/03/2010 changed input method.
% inv,ignorecase,exact,combine are replaced by cmds
narginchk(2,3)
if not(iscellstr(c))
error('input c must be a cell array of strings');
end
if nargin == 2
cmds = '';
end
if not(isempty(regexpi(cmds,'inv', 'once' )))
inv = true;
else
inv = false;
end
if not(isempty(regexpi(cmds,'ignorecase', 'once' )))
ignorecase = true;
else
ignorecase = false;
end
if not(isempty(regexpi(cmds,'exact', 'once' )))
exact = true;
else
exact = false;
end
if not(isempty(regexpi(cmds,'unique', 'once' )))
combine = 2;
elseif not(isempty(regexpi(cmds,'intersect', 'once' )))
combine = 3;
else
combine = 1;
end
if ischar(pat)
pat = cellstr(pat);
end
if exact
for i_pat = 1:numel(pat)
pat{i_pat} = ['^' pat{i_pat} '$'];
end
end
for i_pat = 1:length(pat)
if ignorecase
trouv = regexpi(c,pat{i_pat}); % apply regexp on each pattern
else
trouv = regexp(c,pat{i_pat}); % apply regexp on each pattern
end
idx{i_pat} = find(not(cellfun('isempty',trouv)));
end
if isempty(pat)
idx = {};
end
makevector = @(x)(x(:));
switch combine
case 1
idx = makevector([idx{:}]);
case 2
idx = unique([idx{:}]);
case 3
for i_pat = 2:length(pat)
idx{1} = intersect(idx{1},idx{i_pat});
end
idx = idx{1};
end
if inv % if we want to invert result, then do so.
others = 1:numel(trouv);
others(idx) = [];
idx = others;
end
function s = vararg2struct(v,tag)
% s = vararg2struct(v,tag)
%
% translate a sequence of varargin 'name', value pairs into a structure.
% substructure fields can be defined by using underscores (if tag is
% provided, another character can be used)
%
% ex: v = {'name','toto','size',55,'hair_style','cool','hair_color','blue'}
% s = vararg2struct(v)
% s =
% name: 'toto'
% size: 55
% hair: [1x1 struct]
% s.hair
% ans =
% style: 'cool'
% color: 'blue'
%
if not(exist('tag','var'))
tag = '_';
end
s = struct;
f = regexp(v(1:2:end),['[^' regexptranslate('escape',tag) ']*'],'match');
for i_f = 1:numel(f)
str = 's';
for i_ff = 1:numel(f{i_f})
str = [str '.' f{i_f}{i_ff}];
end
str = [str ' = v{i_f*2};'];
eval(str);
end
function s = setdef(s,d,keepempty)
% s = setdef(s,d)
% s = setdef(s,d,keepempty)
% Merges the two structures s and d recursively.
% Adding the default field values from d into s when not present or empty.
% Keeping order of fields same as in d
% if keepempty is provided and true, then empty fields in s will be left
% empty. otherwise they are populated with default values. default is
% false.
if not(exist('keepempty','var'))
keepempty = 0;
end
if isstruct(s) && not(isempty(s))
if not(isstruct(d))
fields = [];
else
fields = fieldnames(d);
end
for i_f = 1:numel(fields)
if isfield(s,fields{i_f})
s.(fields{i_f}) = setdef(s.(fields{i_f}),d.(fields{i_f}),keepempty);
else
[s.(fields{i_f})] = d.(fields{i_f});
end
end
if not(isempty(fields))
fieldsorig = setdiff(fieldnames(s),fields);
s = orderfields(s,[fields; fieldsorig]);
end
elseif not(isempty(s)) || keepempty
s = s;
elseif isempty(s)
s = d;
end