-
Notifications
You must be signed in to change notification settings - Fork 29
/
call_return.ado
335 lines (281 loc) · 10.5 KB
/
call_return.ado
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
*capture program drop call_return
program call_return , rclass
syntax using/ , [ debug load_mat_prefix(string) ]
// -------------------------------------------------------------------------
// Syntax processing
// =========================================================================
// if "stata_output" is created, then continue the process. Otherwise, return
// an error, because "rc" must be returned anyway...
/*
Point for future maintenance:
----------------------------
Stata has a limit in terms of number of arguments, number of vector's numbers,
literals, etc. Therefore, if the MATRIX returned from R has more than 550
numbers, break the matrix and combine it in Stata.
THIS IS NOT A MATSIZE LIMIT.
*/
capture confirm file "`using'"
if _rc == 0 & substr(trim(`"`macval(0)'"'),1,3) != "q()" {
tempname hitch
qui file open `hitch' using "`using'", read
file read `hitch' line
while r(eof) == 0 {
local jump // reset
if !missing("`debug'") di as err " rcall_synchronize_mode is : $rcall_synchronize_mode"
if !missing("`debug'") di as err "rcall_synchronize_mode3 is : $rcall_synchronize_mode3"
// NULL OBJECT
// ===========================
if substr(`"`macval(line)'"',1,7) == "//NULL " {
local line : subinstr local line "//NULL " ""
local line : subinstr local line "." "_", all //avoid "." in name
local name : di `"`macval(line)'"'
if "$rcall_synchronize_mode" == "on" | "$rcall_synchronize_mode3" == "on" {
scalar `name' = "NULL"
return local `name' "NULL"
}
else {
return local `name' "NULL"
}
}
// GLOBAL variables (and WARNING OBJECT)
// =====================================
if substr(`"`macval(line)'"',1,9) == "//GLOBAL " {
local line : subinstr local line "//GLOBAL " ""
local line : subinstr local line "." "_", all //avoid "." in name
local line : subinstr local line "-Inf" "." // Avoid -Inf error, return missing
local name : di `"`macval(line)'"'
file read `hitch' line
global `name' : di `"`macval(line)'"'
}
// SCALAR OBJECT
// ===========================
if substr(`"`macval(line)'"',1,9) == "//SCALAR " {
local line : subinstr local line "//SCALAR " ""
local line : subinstr local line "." "_", all //avoid "." in name
local line : subinstr local line "-Inf" "." // Avoid -Inf error, return missing
local name : di `"`macval(line)'"'
file read `hitch' line
if "`name'" != "rcall_counter" {
if "`name'" == "rc" & "`line'" == "1" local Rerror 1
if "$rcall_synchronize_mode" == "on" | "$rcall_synchronize_mode3" == "on" {
scalar `name' = `line'
return scalar `name' = `line'
}
else {
return scalar `name' = `line'
}
}
}
// NUMERIC OBJECT LENGTH > 1
// ===========================
if substr(`"`macval(line)'"',1,14) == "//NUMERICLIST " {
local line : subinstr local line "//NUMERICLIST " ""
local line : subinstr local line "." "_", all //avoid "." in name
local name : di `"`macval(line)'"'
file read `hitch' line
local content
while r(eof) == 0 & substr(`"`macval(line)'"',1,2) != "//" {
local content `content' `line'
file read `hitch' line
local jump 1
}
return local `name' = "`content'"
//CANNOT BE DEFINED IN STATA
}
// STRING OBJECT AND CLIST
// ===========================
if substr(`"`macval(line)'"',1,9) == "//STRING " | ///
substr(`"`macval(line)'"',1,8) == "//CLIST " {
local line : subinstr local line "//STRING " ""
local line : subinstr local line "//CLIST " ""
local line : subinstr local line "." "_", all //avoid "." in name
local line : subinstr local line "$" "_", all //avoid "$" in name
local name : di `"`macval(line)'"'
file read `hitch' line
local content
local multiline
while r(eof) == 0 & substr(`"`macval(line)'"',1,2) != "//" & trim(`"`macval(line)'"') != "" {
if missing(`"`content'"') {
local content 1
local multiline : di `"`macval(multiline)'"' `"`macval(line)'"'
}
else {
local multiline : di `"`macval(multiline){break}'"' `"`macval(line)'"'
}
file read `hitch' line
local jump 1
}
if "`name'" == "error" {
local multiline : subinstr local multiline "$" "\\$", all
local errorMessage = `"`macval(multiline)'"'
}
if "$rcall_synchronize_mode" == "on" | "$rcall_synchronize_mode3" == "on" {
local test
capture local test : di `multiline'
if !missing("`test'") scalar `name' = `multiline'
else scalar `name' = "`multiline'"
return local `name' `"`macval(multiline)'"'
}
else {
return local `name' `"`macval(multiline)'"'
}
}
// LIST OBJECT (NUMERIC)
// ===========================
if substr(`"`macval(line)'"',1,7) == "//LIST " {
local line : subinstr local line "//LIST " ""
local line : subinstr local line "." "_", all //avoid "." in name
local line : subinstr local line "$" "_", all //avoid "$" in name
local name : di `"`macval(line)'"'
file read `hitch' line
local content
while r(eof) == 0 & substr(`"`macval(line)'"',1,2) != "//" {
if missing(`"`content'"') local content : di `"`content'`line'"'
else local content : di `"`content'`line'"'
file read `hitch' line
local jump 1
}
return local `name' "`content'"
//CANNOT BE DEFINED IN STATA
}
// -----------------------------------------------------------------
// MATRIX OBJECT (NUMERIC)
// =================================================================
if substr(`"`macval(line)'"',1,9) == "//MATRIX " {
// get the name of the matrix
local line : subinstr local line "//MATRIX " ""
local line : subinstr local line "." "_", all //avoid "." in name
local line : subinstr local line "$" "_", all //avoid "$" in name
local name : di `"`macval(line)'"'
if "$rcall_synchronize_mode" != "on" {
// make sure that it exists
capture confirm file "`load_mat_prefix'_load.matrix.`name'.dta"
if _rc == 0 {
preserve
// load the matrix as data frame
qui use "`load_mat_prefix'_load.matrix.`name'.dta", clear
// extract the column names
quietly describe, fullname varlist
tokenize "`r(varlist)'"
if "`1'" == "rownames" {
local rownames "rownames"
macro shift
}
*foreach var of varlist `mylist' {
*local len = length("`var'")
*local head = substr("`var'",1,`len'-2)
*local tail = substr("`var'",`len'-1,`len')
*}
local variablelist
while !missing("`1'") {
local variablelist `variablelist' `1'
macro shift
}
mkmat `variablelist' , matrix(`name') rownames("`rownames'")
restore
}
// return an error
else {
return scalar rc = 1
display as err "{bf:`name'} was not transported properly"
}
}
else if "$rcall_synchronize_mode" == "on" {
//GET NUMBER OF ROWS
file read `hitch' line
if substr(`"`macval(line)'"',1,11) == "rownumber: " {
local line : subinstr local line "rownumber: " ""
local rownumber : di `"`macval(line)'"'
}
file read `hitch' line
//GET COLUMN NAMES
if substr(`"`macval(line)'"',1,9) == "colnames:" {
local line : subinstr local line "colnames:" ""
local colname : di `"`macval(line)'"'
file read `hitch' line
}
//GET ROW NAMES
if substr(`"`macval(line)'"',1,9) == "rownames:" {
local line : subinstr local line "rownames:" ""
local rowname : di `"`macval(line)'"'
file read `hitch' line
}
// READ THE MATRIX
local content
while r(eof) == 0 & substr(`"`macval(line)'"',1,2) != "//" {
local content `content' `line'
file read `hitch' line
local jump 1
}
// AVOID STATA LIMITS IN TERMS OF NUMBER OF ARGUMENTS
// ------------------------------------------------------------
if wordcount(`"`macval(content)'"') <= 500 {
matrix define `name' = (`content')
}
else {
tokenize "`content'"
while !missing("`1'") {
local n 0 //RESET
local content2 //RESET
while `n' <= 500 {
local n `++n'
local content2 `content2' `1'
macro shift
}
// append pieces of the long returned matrix
if missing("`name'") {
matrix define `name' = (`content2')
}
else {
matrix `name' = `name' , `content2'
}
macro shift
}
}
// CREATE THE MATRIX IN MATA
*mat list `name'
mata: `name' = st_matrix("`name'")
*mata: `name'
mata: st_matrix("`name'", rowshape(`name', `rownumber'))
// ADD COLUMN NAME
if !missing("`colname'") {
matrix colnames `name' = `colname'
}
// ADD ROW NAME
if !missing("`rowname'") {
matrix rownames `name' = `rowname'
}
}
if "$rcall_synchronize_mode" == "on" | "$rcall_synchronize_mode3" == "on" {
mat define `name'_copy = `name'
return matrix `name' = `name'
mat `name' = `name'_copy
mat drop `name'_copy
}
else {
return matrix `name' = `name'
}
// Erase the temporary matrix file generated by R
if missing("`debug'") capture erase "`load_mat_prefix'_load.matrix.`name'.dta"
}
if missing("`jump'") file read `hitch' line
}
*capture erase list.txt
*copy `using' list.txt, replace
if missing("`debug'") capture erase "`using'"
if missing("`debug'") capture erase rcall_synchronize
}
// return an error
else {
return scalar rc = 1
}
// generate error message
// -------------------------------------------------------------------------
// but do not stop rcall, because error message should be returned by rcall
// or the main caller program
if "`Rerror'" == "1" {
global RcallError 1
display as error `"{p}`macval(errorMessage)'"'
}
macro drop rcall_synchronize_mode3
end