R4rb 1.1.1 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b3fd5ec7a4bb58571955e8cdf5e486f24b46fc6dde78ef4c45b3ead009896f0
4
- data.tar.gz: cb8138744d0696efd35ddc54c4a7270592ea140d623b4152c1a85276edc18dd0
3
+ metadata.gz: c3731d08caaa9d7e41fcf0938a4cc22ba27491bc0129f97721adeb1f795d9afb
4
+ data.tar.gz: 6a924c347d7921c47ca5c587c1acb34e9aace21e6d4133d8033850645b6fe31d
5
5
  SHA512:
6
- metadata.gz: 025c1736d4ed8242f123e63d7748141ee7c718ed9feda2fa08767ddb3b31ec897c6b5c219fb10ba04076eb9bb9697ed979a5c651fb1940740c1b192cc0314451
7
- data.tar.gz: df29b358f0849391e17cf8dc5c0c534e0e222a046d4e67b7344d8be24f6b119c5c730cd575b164674f7932d2e2fbb1f3f6b304d01fd98678aa2600ae0744de3e
6
+ metadata.gz: 7c253cd4a52e55a36fc120b167dcd20cc25e4e8cdc217879cf2ba64d68873c0baad9a6ea745a937891e10741bd548c9e2f89ea392b524dbac7e9cea713170e56
7
+ data.tar.gz: df6a3e1a688396a63be8ea5d6f28746e551879bc10ffbdb9f6cdae626e3137ed71b134ac71542cc68bf6a92c09322121ec2fb7d3280f018a1553297791f346a9
data/R4rb.gemspec CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rubygems/package_task'
3
3
 
4
4
  pkg_NAME='R4rb'
5
- pkg_VERSION='1.1.1'
5
+ pkg_VERSION='1.1.4'
6
6
  pkg_FILES=FileList[
7
7
  'Rakefile','R4rb.gemspec',
8
8
  'ext/R4rb/*.c',
@@ -18,6 +18,7 @@ spec = Gem::Specification.new do |s|
18
18
  s.summary = "R for ruby"
19
19
  s.name = pkg_NAME
20
20
  s.version = pkg_VERSION
21
+ s.licenses = ['MIT', 'GPL-2.0']
21
22
  s.requirements << 'none'
22
23
  s.require_paths = ["lib","ext/R4rb"]
23
24
  s.files = pkg_FILES.to_a
@@ -28,5 +29,4 @@ spec = Gem::Specification.new do |s|
28
29
  s.author = "CQLS"
29
30
  s.email= "rdrouilh@gmail.com"
30
31
  s.homepage = "http://cqls.upmf-grenoble.fr"
31
- s.rubyforge_project = nil
32
32
  end
data/Rakefile CHANGED
@@ -63,8 +63,8 @@ end
63
63
  ## clean task
64
64
  desc "Remove #{File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem')}"
65
65
  task :clean do |t|
66
- rm File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem') if File.exists? File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem')
67
- rm_rf File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION) if File.exists? File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION)
66
+ rm File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem') if File.exist? File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem')
67
+ rm_rf File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION) if File.exist? File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION)
68
68
  end
69
69
 
70
70
 
@@ -78,13 +78,13 @@ end
78
78
  desc "Quick install #{File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem')}"
79
79
  task :install => :package do |t|
80
80
  `gem install #{File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem')} --local --no-rdoc --no-ri --user-install`
81
- rm_rf File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION) if File.exists? File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION)
81
+ rm_rf File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION) if File.exist? File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION)
82
82
  end
83
83
 
84
84
  desc "Docker install #{File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem')}"
85
85
  task :docker => :package do |t|
86
86
  `gem install #{File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION+'.gem')} --local --no-rdoc --no-ri`
87
- rm_rf File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION) if File.exists? File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION)
87
+ rm_rf File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION) if File.exist? File.join(PKGDIR,PKG_NAME+'-'+PKG_VERSION)
88
88
  end
89
89
 
90
90
 
data/ext/R4rb/extconf.rb CHANGED
@@ -12,7 +12,7 @@ def find_installed_R
12
12
  $prefix_include=$prefix+"/include"
13
13
  $prefix_lib=nil
14
14
  (RUBY_PLATFORM=~/64/ ? ["","x64"] : ["","i386"]).each do |arch|
15
- $prefix_lib=File.join($prefix,"bin",arch) if File.exists? File.join($prefix,"bin",arch,"R.dll")
15
+ $prefix_lib=File.join($prefix,"bin",arch) if File.exist? File.join($prefix,"bin",arch,"R.dll")
16
16
  #$versions=[arch]
17
17
  break if $prefix_lib
18
18
  end
@@ -47,19 +47,19 @@ def find_installed_R
47
47
 
48
48
  dirs.each do |dir|
49
49
  p dir
50
- if !$prefix and FileTest.exists?(dir)
50
+ if !$prefix and FileTest.exist?(dir)
51
51
  $prefix = dir[0..-3]
52
52
  end
53
53
 
54
- if !$prefix_include and FileTest.exists?(dir+"/include/R.h")
54
+ if !$prefix_include and FileTest.exist?(dir+"/include/R.h")
55
55
  $prefix_include=dir+"/include"
56
56
  end
57
57
 
58
- if !$prefix_include and FileTest.exists?(dir+"/R.h")
58
+ if !$prefix_include and FileTest.exist?(dir+"/R.h")
59
59
  $prefix_include=dir
60
60
  end
61
61
 
62
- if !$prefix_lib and FileTest.exists?(dir+"/lib/libR.so")
62
+ if !$prefix_lib and FileTest.exist?(dir+"/lib/libR.so")
63
63
  $prefix_lib=dir+"/lib"
64
64
  end
65
65
  end
@@ -73,7 +73,7 @@ end
73
73
 
74
74
 
75
75
  def r4rb_makefile(inc,lib,version=nil)
76
- $CFLAGS = "-I"+inc+" -I."
76
+ $CFLAGS = "-fdeclspec -I"+inc+" -I."
77
77
  $LDFLAGS = "-L"+lib if lib
78
78
  $libs = "-lR"
79
79
 
@@ -33,6 +33,7 @@ module R2rb
33
33
  @name=name
34
34
  @type="expr"
35
35
  end
36
+ ##p [:RVector, @name, @type]
36
37
  return self
37
38
  end
38
39
 
@@ -47,10 +48,11 @@ module R2rb
47
48
  return self
48
49
  end
49
50
 
50
- def >(arr)
51
+ def >(arr)
51
52
  res=self.get
53
+
52
54
  #puts "res";p @name;p res
53
- if res
55
+ unless res.nil?
54
56
  #puts "arr.class:";p arr.class
55
57
  #puts "res.class";p res.class
56
58
  res=[res] unless res.is_a? Array
@@ -94,7 +94,7 @@ def find_installed_R
94
94
  dirs=["/usr/lib/R","/usr/local/lib/R","/usr/lib64/R"]
95
95
 
96
96
  dirs.each do |dir|
97
- if FileTest.exists?(dir)
97
+ if FileTest.exist?(dir)
98
98
  return dir
99
99
  end
100
100
  end
data/lib/R4rb/init.rb CHANGED
@@ -112,7 +112,7 @@ def find_installed_R
112
112
  dirs=["/usr/lib/R","/usr/local/lib/R","/usr/lib64/R","/usr/local/lib64/R"]
113
113
 
114
114
  dirs.each do |dir|
115
- if FileTest.exists?(dir)
115
+ if FileTest.exist?(dir)
116
116
  return dir
117
117
  end
118
118
  end
data/lib/R4rb.rb CHANGED
@@ -7,7 +7,7 @@ if RUBY_PLATFORM=~/mingw32/ #because I did not manage to execute the same on min
7
7
  # cmd="Rscript.exe"
8
8
  # version=`#{cmd} -e "cat(R.version$major,R.version$minor,sep='.')"`
9
9
  # else
10
- # if File.exists? (jsonfile=File.join(ENV["HOME"],"dyndoc","studio","win32","dyndoc.json"))
10
+ # if File.exist? (jsonfile=File.join(ENV["HOME"],"dyndoc","studio","win32","dyndoc.json"))
11
11
  # require 'json'
12
12
  # version=JSON.parse(File.read(jsonfile))["Rversion"]
13
13
  # end
@@ -23,7 +23,7 @@ found=nil
23
23
  [".bundle",".so"].each do |ext|
24
24
  ["","../ext/R4rb"].each do |path| #sometimes in lib sometimes in ext/R4rb/
25
25
  lib=File.join(File.dirname(__FILE__),path,'R4rb'+(version ? '.'+version+ext : ext ) )
26
- if File.exists? lib
26
+ if File.exist? lib
27
27
  puts "#{lib} found"
28
28
  require lib
29
29
  puts "#{lib} loaded"
@@ -38,7 +38,7 @@ end
38
38
  # ["i386","x64"].each do |version|
39
39
  # ["","../ext/R4rb"].each do |path|
40
40
  # lib=File.join(File.dirname(__FILE__),path,'R4rb.'+version+'.so')
41
- # if File.exists? lib
41
+ # if File.exist? lib
42
42
  # puts "#{lib} found"
43
43
  # require lib
44
44
  # found=true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: R4rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - CQLS
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-07 00:00:00.000000000 Z
11
+ date: 2023-01-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: " R is embedded in ruby with some communication support .\n"
14
14
  email: rdrouilh@gmail.com
@@ -20,7 +20,6 @@ files:
20
20
  - R4rb.gemspec
21
21
  - Rakefile
22
22
  - ext/R4rb/MANIFEST
23
- - ext/R4rb/R4rb.4.1.c
24
23
  - ext/R4rb/R4rb.c
25
24
  - ext/R4rb/extconf.rb
26
25
  - lib/R2rb.rb
@@ -47,7 +46,9 @@ files:
47
46
  - test/testRserve.rb
48
47
  - test/test_error.rb
49
48
  homepage: http://cqls.upmf-grenoble.fr
50
- licenses: []
49
+ licenses:
50
+ - MIT
51
+ - GPL-2.0
51
52
  metadata: {}
52
53
  post_install_message:
53
54
  rdoc_options: []
@@ -66,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
67
  version: '0'
67
68
  requirements:
68
69
  - none
69
- rubygems_version: 3.2.22
70
+ rubygems_version: 3.4.5
70
71
  signing_key:
71
72
  specification_version: 4
72
73
  summary: R for ruby
data/ext/R4rb/R4rb.4.1.c DELETED
@@ -1,600 +0,0 @@
1
- /**********************************************************************
2
-
3
- R4rb.c
4
-
5
- **********************************************************************/
6
- #include <stdio.h>
7
- #include <string.h>
8
-
9
- #include "ruby.h"
10
- #include "ruby/version.h"
11
- #include <R.h>
12
- #include <Rinternals.h>
13
- #include <Rdefines.h>
14
- #include <Rversion.h>
15
-
16
-
17
- /* From Parse.h -- must find better solution: */
18
- #define PARSE_NULL 0
19
- #define PARSE_OK 1
20
- #define PARSE_INCOMPLETE 2
21
- #define PARSE_ERROR 3
22
- #define PARSE_EOF 4
23
-
24
-
25
- #define Need_Integer(x) (x) = rb_Integer(x)
26
- #define Need_Float(x) (x) = rb_Float(x)
27
- #define Need_Float2(x,y) {\
28
- Need_Float(x);\
29
- Need_Float(y);\
30
- }
31
- #define Need_Float3(x,y,z) {\
32
- Need_Float(x);\
33
- Need_Float(y);\
34
- Need_Float(z);\
35
- }
36
-
37
- #if (R_VERSION < 132352) /* before 2.5 to check!*/
38
- SEXP R_ParseVector(SEXP, int, int *);
39
- #define RR_ParseVector(x,y,z) R_ParseVector(x, y, z)
40
- #else
41
- SEXP R_ParseVector(SEXP, int, int *,SEXP);
42
- #define RR_ParseVector(x,y,z) R_ParseVector(x, y, z, R_NilValue)
43
- #endif
44
-
45
- /************* INIT *********************/
46
-
47
- extern Rboolean R_Interactive;
48
- extern int Rf_initEmbeddedR(int argc, char *argv[]);
49
-
50
- VALUE R2rb_init(VALUE obj, VALUE args)
51
- {
52
- char **argv;//={"REmbed","--save","--slave","--quiet"};
53
- int i,argc;//=sizeof(argv)/sizeof(argv[0]);
54
- VALUE tmp;
55
-
56
- argc=RARRAY_LEN(args) + 1;
57
- //printf("argc=%d\n",argc);
58
- argv=malloc(sizeof(char*)*argc);
59
- argv[0]="REmbed";
60
- for (i = 1 ; i < argc ; i++) {
61
- tmp=rb_ary_entry(args,i-1);
62
- argv[i]=StringValuePtr(tmp);
63
- //printf("argv[%d]=%s\n",i,argv[i]);
64
- }
65
- //printf("argc=%d\n",argc);
66
- Rf_initEmbeddedR(argc,argv);
67
- R_Interactive = FALSE;
68
- return Qtrue;
69
- }
70
-
71
- /***************** EVAL **********************/
72
-
73
- VALUE R2rb_eval(VALUE obj, VALUE cmd, VALUE print)
74
- {
75
- char *cmdString;
76
- int nbCmds;
77
- VALUE tmp;
78
- int errorOccurred,status, i;
79
-
80
- SEXP text, expr, ans=R_NilValue /* -Wall */;
81
-
82
-
83
- //printf("Avant parsing\n");
84
-
85
- nbCmds=RARRAY_LEN(cmd);
86
-
87
- //printf("nbCmds : %d\n",nbCmds);
88
-
89
- text = PROTECT(allocVector(STRSXP, nbCmds));
90
- for (i = 0 ; i < nbCmds ; i++) {
91
- tmp=rb_ary_entry(cmd,i);
92
- cmdString=StringValuePtr(tmp);
93
- SET_STRING_ELT(text, i, mkChar(cmdString));
94
- }
95
- expr = PROTECT(RR_ParseVector(text, -1, &status));
96
-
97
- if (status != PARSE_OK) {
98
- //printf("Parsing error (status=%d) in:\n",status);
99
- for (i = 0 ; i < nbCmds ; i++) {
100
- tmp=rb_ary_entry(cmd,i);
101
- cmdString=StringValuePtr(tmp);
102
- //printf("%s\n",cmdString);
103
- }
104
- UNPROTECT(2);
105
- return Qfalse;
106
- }
107
-
108
- /* Note that expr becomes an EXPRSXP and hence we need the loop
109
- below (a straight eval(expr, R_GlobalEnv) won't work) */
110
- {
111
- for(i = 0 ; i < nbCmds ; i++)
112
- ans = R_tryEval(VECTOR_ELT(expr, i),NULL, &errorOccurred);
113
- if(errorOccurred) {
114
- //fprintf(stderr, "Caught another error calling sqrt()\n");
115
- fflush(stderr);
116
- UNPROTECT(2);
117
- return Qfalse;
118
- }
119
-
120
- if (print != Qnil) {
121
- Rf_PrintValue(ans);
122
- }
123
- }
124
-
125
- UNPROTECT(2);
126
- return Qtrue;
127
- }
128
-
129
- /***************** PARSE **********************/
130
-
131
- VALUE R2rb_parse(VALUE obj, VALUE cmd,VALUE print)
132
- {
133
- char *cmdString;
134
- int nbCmds;
135
- VALUE tmp;
136
- int status,i;
137
-
138
- SEXP text, expr, ans=R_NilValue /* -Wall */;
139
-
140
-
141
- //printf("Avant parsing\n");
142
-
143
- nbCmds=RARRAY_LEN(cmd);
144
-
145
- //printf("nbCmds : %d\n",nbCmds);
146
-
147
- text = PROTECT(allocVector(STRSXP, nbCmds));
148
- for (i = 0 ; i < nbCmds ; i++) {
149
- tmp=rb_ary_entry(cmd,i);
150
- cmdString=StringValuePtr(tmp);
151
- SET_STRING_ELT(text, i, mkChar(cmdString));
152
- }
153
- expr = PROTECT(RR_ParseVector(text, -1, &status));
154
-
155
- if (status != PARSE_OK) {
156
- if (print != Qnil) printf("Parsing error (status=%d) in:\n",status);
157
- for (i = 0 ; i < nbCmds ; i++) {
158
- tmp=rb_ary_entry(cmd,i);
159
- cmdString=StringValuePtr(tmp);
160
- if (print != Qnil) printf("%s\n",cmdString);
161
- }
162
- //UNPROTECT(2);
163
- //return Qfalse;
164
- }
165
- UNPROTECT(2);
166
- //return Qtrue;
167
- return INT2FIX(status);
168
- }
169
-
170
-
171
- /*****************************************
172
-
173
- Interface to get values of RObj from Ruby
174
- The basic idea : no copy of the R Vector
175
- just methods to extract value !!!
176
-
177
- ******************************************/
178
-
179
- // used internally !!! -> eval only one string line
180
- SEXP util_eval1string(VALUE cmd)
181
- {
182
- char *cmdString;
183
- int errorOccurred,status, i;
184
-
185
- SEXP text, expr, ans=R_NilValue /* -Wall */;
186
-
187
- text = PROTECT(allocVector(STRSXP, 1));
188
- cmdString=StringValuePtr(cmd);
189
- //printf("cmd: %s\n",cmdString);
190
- SET_STRING_ELT(text, 0, mkChar(cmdString));
191
- expr = PROTECT(RR_ParseVector(text, -1, &status));
192
- if (status != PARSE_OK) {
193
- printf("Parsing error in: %s\n",cmdString);
194
- UNPROTECT(2);
195
- return R_NilValue;
196
- }
197
- /* Note that expr becomes an EXPRSXP and hence we need the loop
198
- below (a straight eval(expr, R_GlobalEnv) won't work) */
199
- ans = R_tryEval(VECTOR_ELT(expr, 0),R_GlobalEnv,&errorOccurred);
200
- //ans = eval(VECTOR_ELT(expr, 0),R_GlobalEnv);
201
- if(errorOccurred) {
202
- //fflush(stderr);
203
- printf("Exec error in: %s\n",cmdString);
204
- UNPROTECT(2);
205
- return R_NilValue;
206
- }
207
- UNPROTECT(2);
208
- return ans;
209
- }
210
-
211
- int util_isVector(SEXP ans)
212
- {
213
- return ((!isNewList(ans)) & isVector(ans));
214
- }
215
-
216
- int util_isVariable(VALUE self)
217
- {
218
- VALUE tmp;
219
- tmp=rb_iv_get(self,"@type");
220
- return strcmp(StringValuePtr(tmp),"var")==0;
221
- }
222
-
223
- SEXP util_getVar(VALUE self)
224
- {
225
- SEXP ans;
226
- char *name;
227
- VALUE tmp;
228
-
229
- tmp=rb_iv_get(self,"@name");
230
- name=StringValuePtr(tmp);
231
- if(util_isVariable(self)) {
232
- ans = findVar(install(name),R_GlobalEnv); //currently in R_GlobalEnv!!!
233
- } else {
234
- //printf("getVar:%s\n",name);
235
- ans=util_eval1string(rb_iv_get(self,"@name"));
236
- if(ans==R_NilValue) return ans;
237
- }
238
- if(!util_isVector(ans)) return R_NilValue;
239
- return ans;
240
- }
241
-
242
- //with argument!! necessarily an expression and not a variable
243
- SEXP util_getExpr_with_arg(VALUE self)
244
- {
245
- SEXP ans;
246
- VALUE tmp;
247
-
248
- //printf("getVar:%s\n",name);
249
- tmp=rb_str_dup(rb_iv_get(self,"@arg"));
250
- ans=util_eval1string(rb_str_cat2(rb_str_dup(rb_iv_get(self,"@name")),StringValuePtr(tmp)));
251
- if(ans==R_NilValue) return ans;
252
- if(!util_isVector(ans)) return R_NilValue;
253
- return ans;
254
- }
255
-
256
-
257
- VALUE util_SEXP2VALUE(SEXP ans)
258
- {
259
- VALUE res;
260
- int n,i;
261
- Rcomplex cpl;
262
- VALUE res2;
263
-
264
- n=length(ans);
265
- res = rb_ary_new2(n);
266
- switch(TYPEOF(ans)) {
267
- case REALSXP:
268
- for(i=0;i<n;i++) {
269
- rb_ary_store(res,i,rb_float_new(REAL(ans)[i]));
270
- }
271
- break;
272
- case INTSXP:
273
- for(i=0;i<n;i++) {
274
- rb_ary_store(res,i,INT2FIX(INTEGER(ans)[i]));
275
- }
276
- break;
277
- case LGLSXP:
278
- for(i=0;i<n;i++) {
279
- rb_ary_store(res,i,(INTEGER(ans)[i] ? Qtrue : Qfalse));
280
- }
281
- break;
282
- case STRSXP:
283
- for(i=0;i<n;i++) {
284
- rb_ary_store(res,i,rb_str_new2(CHAR(STRING_ELT(ans,i))));
285
- }
286
- break;
287
- case CPLXSXP:
288
- rb_require("complex");
289
- for(i=0;i<n;i++) {
290
- cpl=COMPLEX(ans)[i];
291
- res2 = rb_eval_string("Complex.new(0,0)");
292
- rb_iv_set(res2,"@real",rb_float_new(cpl.r));
293
- rb_iv_set(res2,"@image",rb_float_new(cpl.i));
294
- rb_ary_store(res,i,res2);
295
- }
296
- break;
297
- }
298
-
299
- return res;
300
- }
301
-
302
-
303
- SEXP util_VALUE2SEXP(VALUE arr)
304
- {
305
- SEXP ans;
306
- VALUE res,class,tmp;
307
- int i,n=0;
308
-
309
- if(!rb_obj_is_kind_of(arr,rb_cArray)) {
310
- n=1;
311
- res = rb_ary_new2(1);
312
- rb_ary_push(res,arr);
313
- arr=res;
314
- } else {
315
- n=RARRAY_LEN(arr);
316
- }
317
-
318
- class=rb_class_of(rb_ary_entry(arr,0));
319
-
320
- if(class==rb_cFloat) {
321
- PROTECT(ans=allocVector(REALSXP,n));
322
- for(i=0;i<n;i++) {
323
- REAL(ans)[i]=NUM2DBL(rb_ary_entry(arr,i));
324
- }
325
- #if RUBY_API_VERSION_CODE >= 20400
326
- } else if(class==rb_cInteger) {
327
- #else
328
- } else if(class==rb_cFixnum || class==rb_cBignum) {
329
- #endif
330
- PROTECT(ans=allocVector(INTSXP,n));
331
- for(i=0;i<n;i++) {
332
- INTEGER(ans)[i]=NUM2INT(rb_ary_entry(arr,i));
333
- }
334
- } else if(class==rb_cTrueClass || class==rb_cFalseClass) {
335
- PROTECT(ans=allocVector(LGLSXP,n));
336
- for(i=0;i<n;i++) {
337
- LOGICAL(ans)[i]=(rb_class_of(rb_ary_entry(arr,i))==rb_cFalseClass ? FALSE : TRUE);
338
- }
339
- } else if(class==rb_cString) {
340
- PROTECT(ans=allocVector(STRSXP,n));
341
- for(i=0;i<n;i++) {
342
- tmp=rb_ary_entry(arr,i);
343
- SET_STRING_ELT(ans,i,mkChar(StringValuePtr(tmp)));
344
- }
345
- } else ans=R_NilValue;
346
-
347
- if(n>0) UNPROTECT(1);
348
- return ans;
349
- }
350
-
351
-
352
-
353
- VALUE RVect_initialize(VALUE self, VALUE name)
354
- {
355
- rb_iv_set(self,"@name",name);
356
- rb_iv_set(self,"@type",rb_str_new2("var"));
357
- rb_iv_set(self,"@arg",rb_str_new2(""));
358
- return self;
359
- }
360
-
361
- VALUE RVect_isValid(VALUE self)
362
- {
363
- SEXP ans;
364
- char *name;
365
-
366
- #ifdef cqls
367
- VALUE tmp;
368
- tmp=rb_iv_get(self,"@name");
369
- name = StringValuePtr(tmp);
370
- ans = findVar(install(name),R_GlobalEnv); //currently in R_GlobalEnv!!!
371
- #else
372
- ans = util_getVar(self);
373
- #endif
374
- if(!util_isVector(ans)) {
375
- #ifndef cqls
376
- VALUE tmp;
377
- tmp=rb_iv_get(self,"@name");
378
- name = StringValuePtr(tmp);
379
- #endif
380
- rb_warn("%s is not a R vector !!!",name); //TODO name not defined
381
- return Qfalse;
382
- }
383
- return Qtrue;
384
- }
385
-
386
- VALUE RVect_length(VALUE self)
387
- {
388
- SEXP ans;
389
- char *name;
390
- #ifdef cqls
391
- VALUE tmp;
392
- tmp=rb_iv_get(self,"@name");
393
- if(!RVect_isValid(self)) return Qnil;
394
- name = StringValuePtr(tmp);
395
- ans = findVar(install(name),R_GlobalEnv); //currently in R_GlobalEnv!!!
396
- #else
397
- ans = util_getVar(self);
398
-
399
- if(ans==R_NilValue) {
400
- //printf("Sortie de length avec nil\n");
401
- return Qnil;
402
- }
403
- #endif
404
- return INT2NUM(length(ans));
405
- }
406
-
407
- VALUE RVect_get(VALUE self)
408
- {
409
- SEXP ans;
410
- VALUE res;
411
- char *name;
412
- int n,i;
413
- Rcomplex cpl;
414
- VALUE res2;
415
-
416
- //#define cqls
417
- #ifdef cqls
418
- VALUE tmp;
419
- if(!RVect_isValid(self)) return Qnil;
420
- #else
421
- ans = util_getVar(self);
422
-
423
- if(ans==R_NilValue) {
424
- //printf("Sortie de get avec nil\n");
425
- return Qnil;
426
- }
427
- #endif
428
- #ifdef cqls
429
- tmp=rb_iv_get(self,"@name");
430
- name = StringValuePtr(tmp);
431
- ans = findVar(install(name),R_GlobalEnv);
432
- #endif
433
-
434
- res=util_SEXP2VALUE(ans);
435
- if(length(ans)==1) res=rb_ary_entry(res,0);
436
- return res;
437
- }
438
-
439
- VALUE RVect_get_with_arg(VALUE self)
440
- {
441
- SEXP ans;
442
- VALUE res;
443
- char *name;
444
- int n,i;
445
- Rcomplex cpl;
446
- VALUE res2;
447
-
448
- ans = util_getExpr_with_arg(self);
449
-
450
- if(ans==R_NilValue) {
451
- //printf("Sortie de get avec nil\n");
452
- return Qnil;
453
- }
454
- res=util_SEXP2VALUE(ans);
455
-
456
- //printf("RVect_get_with_arg: length(ans)=%d\n",length(ans));
457
- if (length(ans)==1) res=rb_ary_entry(res,0);
458
-
459
- return res;
460
- }
461
-
462
-
463
-
464
- // faster than self.to_a[index]
465
- VALUE RVect_aref(VALUE self, VALUE index)
466
- {
467
- SEXP ans;
468
- VALUE res;
469
- char *name;
470
- int n,i;
471
- Rcomplex cpl;
472
- #ifdef cqls
473
- VALUE tmp;
474
- #endif
475
- i = FIX2INT(index);
476
-
477
- #ifdef cqls
478
- if(!RVect_isValid(self)) return Qnil;
479
- tmp=rb_iv_get(self,"@name");
480
- name = StringValuePtr(tmp);
481
- ans = findVar(install(name),R_GlobalEnv); //currently in R_GlobalEnv!!!
482
- #else
483
- ans = util_getVar(self);
484
- #endif
485
- n=length(ans);
486
- //printf("i=%d and n=%d\n",i,n);
487
- if(i<n) {
488
- switch(TYPEOF(ans)) {
489
- case REALSXP:
490
- res=rb_float_new(REAL(ans)[i]);
491
- break;
492
- case INTSXP:
493
- res=INT2FIX(INTEGER(ans)[i]);
494
- break;
495
- case LGLSXP:
496
- res=(INTEGER(ans)[i] ? Qtrue : Qfalse);
497
- break;
498
- case STRSXP:
499
- res=rb_str_new2(CHAR(STRING_ELT(ans,i)));
500
- break;
501
- case CPLXSXP:
502
- rb_require("complex");
503
- cpl=COMPLEX(ans)[i];
504
- res = rb_eval_string("Complex.new(0,0)");
505
- rb_iv_set(res,"@real",rb_float_new(cpl.r));
506
- rb_iv_set(res,"@image",rb_float_new(cpl.i));
507
- break;
508
- }
509
- } else {
510
- res = Qnil;
511
- }
512
- return res;
513
- }
514
-
515
- VALUE RVect_set(VALUE self,VALUE arr)
516
- {
517
- SEXP ans;
518
- char *name;
519
- VALUE tmp;
520
-
521
- ans=util_VALUE2SEXP(arr);
522
-
523
- tmp=rb_iv_get(self,"@name");
524
- name = StringValuePtr(tmp);
525
- if(util_isVariable(self)) {
526
- defineVar(install(name),ans,R_GlobalEnv); //currently in R_GlobalEnv!!!
527
- } else {
528
- defineVar(install(".rubyExport"),ans,R_GlobalEnv);
529
- util_eval1string(rb_str_cat2(rb_str_dup(rb_iv_get(self,"@name")),"<-.rubyExport"));
530
- }
531
-
532
- return self;
533
- }
534
-
535
- VALUE RVect_assign(VALUE obj, VALUE name,VALUE arr)
536
- {
537
- SEXP ans;
538
- char *tmp;
539
-
540
- ans=util_VALUE2SEXP(arr);
541
-
542
- tmp = StringValuePtr(name);
543
- defineVar(install(tmp),ans,R_GlobalEnv);
544
-
545
- return Qnil;
546
- }
547
-
548
- VALUE RVect_set_with_arg(VALUE self,VALUE arr)
549
- {
550
- VALUE tmp;
551
- defineVar(install(".rubyExport"),util_VALUE2SEXP(arr),R_GlobalEnv);
552
- tmp=rb_iv_get(self,"@arg");
553
- util_eval1string(rb_str_cat2(rb_str_cat2(rb_str_dup(rb_iv_get(self,"@name")),StringValuePtr(tmp)),"<-.rubyExport"));
554
- return self;
555
- }
556
-
557
-
558
-
559
- void
560
- Init_R4rb()
561
- {
562
- VALUE mR2rb;
563
-
564
- mR2rb = rb_define_module("R2rb");
565
-
566
- rb_define_module_function(mR2rb, "initR", R2rb_init, 1);
567
-
568
- rb_define_module_function(mR2rb, "evalLines", R2rb_eval, 2);
569
-
570
- rb_define_module_function(mR2rb, "parseLines", R2rb_parse, 2);
571
-
572
- VALUE cRVect;
573
-
574
- cRVect = rb_define_class_under(mR2rb,"RVector",rb_cObject);
575
-
576
- rb_define_module_function(cRVect, "assign", RVect_assign, 2);
577
-
578
- rb_define_method(cRVect,"initialize",RVect_initialize,1);
579
-
580
- rb_define_method(cRVect,"get",RVect_get,0);
581
- rb_define_alias(cRVect,"to_a","get");
582
- rb_define_alias(cRVect,"value","get");
583
-
584
- rb_define_method(cRVect,"set",RVect_set,1);
585
- rb_define_alias(cRVect,"<","set");
586
- rb_define_alias(cRVect,"value=","set");
587
-
588
- //method "arg=" defined in eval.rb!! @arg initialized in method "initialize"
589
- rb_define_method(cRVect,"get_with_arg",RVect_get_with_arg,0);
590
- rb_define_alias(cRVect,"value_with_arg","get_with_arg");
591
- rb_define_method(cRVect,"set_with_arg",RVect_set_with_arg,1);
592
- rb_define_alias(cRVect,"value_with_arg=","set_with_arg");
593
-
594
- rb_define_method(cRVect,"valid?",RVect_isValid,0);
595
- rb_define_method(cRVect,"length",RVect_length,0);
596
- rb_define_method(cRVect,"[]",RVect_aref,1);
597
- //[]= iter !!!
598
- rb_define_attr(cRVect,"name",1,1);
599
- rb_define_attr(cRVect,"type",1,1);
600
- }