r_bridge 0.5.3 → 0.5.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: 4d7f74da4e1b6be0717eb1aad13197269106b65abd080922cf19e34f7ed58b5d
4
- data.tar.gz: e8b58e04c13954ed86ca44070861a7fb59f9b5d6fc66c0aa0268b631ebd56e23
3
+ metadata.gz: b2f3e3fc8242a4204e378f7f86bcc3ca120b163aca161e7b5f7bca12f66eecc3
4
+ data.tar.gz: 64d9be810cc38ed60bff9204c8dbdf5e1668d295f99d617f7bcfbcf91aea0041
5
5
  SHA512:
6
- metadata.gz: 98ef89344e79fd315d8bf05d80a74116747ad6d7e0faaafb38895dbb5b787626d0fc44113cd504d8f3b1537264725177822e6280fa5eeee3d96228a68a6d140a
7
- data.tar.gz: d89e882f5f0a3cc345345e0436da5848de0d6d257876debe6df64aae5bc5938162b831a535c97fb8791f844427555c096b2e9841d86cecbe2a7a24414105fc58
6
+ metadata.gz: 3af47902e433e3d7be48d472a28d6d84d6cb684f09fa321db31cf8d2ad007af682d8ad401aba3544158fb9853262f224e531a9c0e769c22aa27957b307a4015a
7
+ data.tar.gz: a43acbb159c37d3ac14ca2580090c810081977d44786812349d901d8ba450173b21ee6ac412a1e9962378fa075030e9b56b52b53e786804969c2fe7406d52257
data/README.md CHANGED
@@ -218,12 +218,15 @@ RBridge.create_extptr( ffi_ptr )
218
218
 
219
219
  create_ns_function_call, create_env_function_call and create_function_call create R's internal S expresion structures for function calls. create_ns_function_call can specify (package) namespace. create_env_function_call can specify environment name.
220
220
 
221
- Arguments are passed as Ruby Hash, and each Hash's value needs to point to R's object. You usually pass R vectors, but you can also pass another function call as an argument.
221
+ Arguments are passed as Ruby's Hash like objects, Hash or assocation Array, and their each element's value needs to be R's object. For these R objects You can assign not only R vectors but also other function calls.
222
+
223
+ Assocation Arrays are useful when you have more than one elements with the same key name, which Hash does not allow. This happens when you do not specify parameter name and you use empty Strings, "", as parameter names.
224
+
222
225
 
223
226
  ```
224
- RBridge.create_ns_function_call( ns, fname, hash )
225
- RBridge.create_env_function_call( env, fname, hash )
226
- RBridge.create_function_call( fname, hash )
227
+ RBridge.create_ns_function_call( ns, fname, hash_like )
228
+ RBridge.create_env_function_call( env, fname, hash_like )
229
+ RBridge.create_function_call( fname, hash_like )
227
230
 
228
231
  (e.g.)
229
232
  # pass vector to argument
@@ -248,6 +251,10 @@ RBridge.exec_function_no_return( RBridge.create_function_call( "print", {"" => c
248
251
  #
249
252
  RBridge.exec_function_no_return( RBridge.create_function_call("source", { "" => RBridge.create_strvec(["newenv.R"]) }))
250
253
  RBridge.exec_function_no_return( RBridge.create_env_function_call("newenv", "hello", { } ))
254
+
255
+ # pass association Array to argument
256
+ add = RBridge.create_function_call( "+", [["" , RBridge.create_intvec( [1,2,3] )],["", RBridge.create_intvec( [10,20,30] ) ]] )
257
+ RBridge.exec_function( RBridge.create_function_call("print", [["" , add ]]))
251
258
  ```
252
259
 
253
260
  The followings are utility functions. assign() and library() are frequently used in R, and easy ways to create them are provided.
@@ -303,20 +303,44 @@ module RBridge
303
303
  return extptr
304
304
  end
305
305
 
306
- def self.hash_to_lcons_args( hash )
307
- raise "hash_to_lcons_args should take Hash argument" if(hash.class != Hash)
308
- if(hash.size == 0)
306
+ def self.array_depth (ary)
307
+ return 0 unless ary.is_a?(Array)
308
+ return 1 + ary.map(){|elem| array_depth(elem) }.max()
309
+ end
310
+
311
+ def self.assoc_array?(ary)
312
+ if ary.map(){|elem| elem.size }.uniq == [2]
313
+ true
314
+ else
315
+ false
316
+ end
317
+ end
318
+
319
+ def self.hash_to_lcons_args( hash_like )
320
+ if hash_like.class == Hash
321
+ # hash
322
+ elsif hash_like.class == Array
323
+ if array_depth(hash_like) == 2 && assoc_array?(hash_like)
324
+ # association array
325
+ else
326
+ raise "hash_to_lcons_args should take Hash argument (or Association Array argument)"
327
+ end
328
+ else
329
+ raise "hash_to_lcons_args should take Hash argument (or Association Array argument)"
330
+ end
331
+
332
+ if(hash_like.size == 0)
309
333
  lcons_args = r_lang_nil()
310
- elsif(hash.size == 1)
311
- tag = hash.first[0]
312
- val = hash.first[1]
334
+ elsif(hash_like.size == 1)
335
+ tag = hash_like.first[0]
336
+ val = hash_like.first[1]
313
337
  lcons_args = lcons_gen( val )
314
338
  if( tag != "" )
315
339
  set_tag_to_lcons( lcons_args, tag )
316
340
  end
317
341
  else
318
342
  idx = 0
319
- hash.reverse_each(){|arg|
343
+ hash_like.reverse_each(){|arg|
320
344
  tag = arg[0]
321
345
  val = arg[1]
322
346
  if(idx == 0 )
@@ -333,32 +357,32 @@ module RBridge
333
357
  return lcons_args
334
358
  end
335
359
 
336
- def self.create_ns_function_call( ns, fname, hash )
360
+ def self.create_ns_function_call( ns, fname, hash_like )
337
361
  raise "create_ns_function_call should take String for namespace" if(ns.class != String)
338
362
  raise "create_ns_function_call should take String for function name" if(fname.class != String)
339
- raise "create_ns_function_call should take Hash for function arguments" if(hash.class != Hash)
340
- lcons_args = hash_to_lcons_args( hash )
363
+ raise "create_ns_function_call should take Hash like structure for function arguments" if(hash_like.class != Hash && hash_like.class != Array)
364
+ lcons_args = hash_to_lcons_args( hash_like )
341
365
 
342
366
  new_function_call = r_lang_create_ns_fcall(ns, fname, lcons_args)
343
367
  ptr_manager_add_ptr_to_current( new_function_call )
344
368
  return new_function_call
345
369
  end
346
370
 
347
- def self.create_env_function_call( env, fname, hash )
371
+ def self.create_env_function_call( env, fname, hash_like )
348
372
  raise "create_env_function_call should take String for env" if(env.class != String)
349
373
  raise "create_env_function_call should take String for function name" if(fname.class != String)
350
- raise "create_env_function_call should take Hash for function arguments" if(hash.class != Hash)
351
- lcons_args = hash_to_lcons_args( hash )
374
+ raise "create_env_function_call should take Hash like structure for function arguments" if(hash_like.class != Hash && hash_like.class != Array)
375
+ lcons_args = hash_to_lcons_args( hash_like )
352
376
 
353
377
  new_function_call = r_lang_create_env_fcall(env, fname, lcons_args)
354
378
  ptr_manager_add_ptr_to_current( new_function_call )
355
379
  return new_function_call
356
380
  end
357
381
 
358
- def self.create_function_call( fname, hash )
382
+ def self.create_function_call( fname, hash_like )
359
383
  raise "create_function_call should take String for function name" if(fname.class != String)
360
- raise "create_function_call should take Hash for function arguments" if(hash.class != Hash)
361
- lcons_args = hash_to_lcons_args( hash )
384
+ raise "create_function_call should take Hash like structure for function arguments" if(hash_like.class != Hash && hash_like.class != Array)
385
+ lcons_args = hash_to_lcons_args( hash_like )
362
386
 
363
387
  new_function_call = r_lang_create_fcall(fname, lcons_args)
364
388
  ptr_manager_add_ptr_to_current( new_function_call )
@@ -1,38 +1,36 @@
1
1
  require "r_bridge/r_bridge_ffi"
2
2
 
3
3
  module RBridge
4
- def self.create_ns_lazy_function( ns, fname, hash , param_manager)
4
+ def self.create_ns_lazy_function( ns, fname, hash_like , param_manager)
5
5
  raise "create_ns_lazy_function should take String for namespace" if(ns.class != String)
6
6
  raise "create_ns_lazy_function should take String for function name" if(fname.class != String)
7
- raise "create_ns_lazy_function should take Hash for function arguments" if(hash.class != Hash)
8
- return LazyFunc.new( ns, nil, fname, hash , param_manager)
7
+ raise "create_ns_lazy_function should take Hash like structure for function arguments" if(hash_like.class != Hash && hash_like.class != Array)
8
+ return LazyFunc.new( ns, nil, fname, hash_like, param_manager)
9
9
  end
10
10
 
11
- def self.create_env_lazy_function( env, fname, hash , param_manager)
11
+ def self.create_env_lazy_function( env, fname, hash_like , param_manager)
12
12
  raise "create_env_lazy_function should take String for environment" if(env.class != String)
13
13
  raise "create_env_lazy_function should take String for function name" if(fname.class != String)
14
- raise "create_env_lazy_function should take Hash for function arguments" if(hash.class != Hash)
15
- return LazyFunc.new( nil, env, fname, hash , param_manager)
14
+ raise "create_env_lazy_function should take Hash like structure for function arguments" if(hash_like.class != Hash && hash_like.class != Array)
15
+ return LazyFunc.new( nil, env, fname, hash_like, param_manager)
16
16
  end
17
17
 
18
- def self.create_lazy_function( fname, hash , param_manager)
18
+ def self.create_lazy_function( fname, hash_like , param_manager)
19
19
  raise "create_lazy_function should take String for function name" if(fname.class != String)
20
- raise "create_lazy_function should take Hash for function arguments" if(hash.class != Hash)
21
- return LazyFunc.new( nil, nil, fname, hash , param_manager)
20
+ raise "create_lazy_function should take Hash like structure for function arguments" if(hash_like.class != Hash && hash_like.class != Array)
21
+ return LazyFunc.new( nil, nil, fname, hash_like, param_manager)
22
22
  end
23
23
 
24
24
  def self.create_function_call_from_lazy_function_attrs( ns, env, fname, fargs, param_manager, result_manager)
25
- farg_keys = fargs.keys
26
25
 
27
- new_arg_hash = {}
28
- farg_keys.each(){|key|
29
- val = fargs[key]
26
+ new_arg_assoc_array = []
27
+ fargs.each(){|key, val|
30
28
 
31
29
  if val.is_a? RResultPrevious
32
30
  r_previous = result_manager.get_previous() # if r_nil (i.e. 1st instruction or no previous result-store instructions) we need to use default one.
33
31
  if ! RBridge::is_r_nil?(r_previous) # When previous result exists
34
- new_arg_hash[key] = r_previous
35
- break
32
+ new_arg_assoc_array << [key, r_previous]
33
+ next
36
34
  else # When previous result does not exist
37
35
  val = val.default
38
36
  end
@@ -40,11 +38,11 @@ module RBridge
40
38
 
41
39
  case val
42
40
  when LazyFunc then
43
- new_arg_hash[key] = create_function_call_from_lazy_function_attrs( val.ns, val.env, val.fname, val.args , param_manager, result_manager )
41
+ new_arg_assoc_array << [key, create_function_call_from_lazy_function_attrs( val.ns, val.env, val.fname, val.args , param_manager, result_manager )]
44
42
  when RResultName , RResultNameArray then
45
- new_arg_hash[key] = result_manager.get_last_for( val )
43
+ new_arg_assoc_array << [key, result_manager.get_last_for( val )]
46
44
  when RParamName then
47
- new_arg_hash[key] = param_manager.get_r_object( val )
45
+ new_arg_assoc_array << [key, param_manager.get_r_object( val )]
48
46
  when RNameContainer then
49
47
  idx = 0
50
48
  while idx < val.elems.size do
@@ -53,37 +51,37 @@ module RBridge
53
51
  when RResultName, RResultNameArray then
54
52
  result = result_manager.get_last_for( elem )
55
53
  if( ! RBridge::is_r_nil?(result) )
56
- new_arg_hash[key] = result
54
+ new_arg_assoc_array << [key, result]
57
55
  break
58
56
  end
59
57
  when RParamName then
60
58
  result = param_manager.get_r_object( elem )
61
59
  if( ! RBridge::is_r_nil?(result) )
62
- new_arg_hash[key] = result
60
+ new_arg_assoc_array << [key, result]
63
61
  break
64
62
  end
65
63
  else # R object
66
- new_arg_hash[key] = val
64
+ new_arg_assoc_array << [key, val]
67
65
  break
68
66
  end
69
67
  idx = idx + 1
70
68
  end
71
69
  if(idx == val.elems.size ) # Not found
72
- new_arg_hash[key] = RBridge::r_nil()
70
+ new_arg_assoc_array << [key, RBridge::r_nil()]
73
71
  end
74
72
  else # R object
75
- new_arg_hash[key] = val
73
+ new_arg_assoc_array << [key, val]
76
74
  end
77
75
  }
78
76
  if( ns.nil? )
79
77
  if( env.nil? )
80
- return create_function_call( fname, new_arg_hash )
78
+ return create_function_call( fname, new_arg_assoc_array )
81
79
  else
82
- return create_env_function_call( env, fname, new_arg_hash )
80
+ return create_env_function_call( env, fname, new_arg_assoc_array )
83
81
  end
84
82
  else
85
83
  if( env.nil? )
86
- return create_ns_function_call( ns, fname, new_arg_hash )
84
+ return create_ns_function_call( ns, fname, new_arg_assoc_array )
87
85
  else
88
86
  raise "namespace and environment are not allowed to be specified at the same time."
89
87
  end
@@ -243,7 +241,6 @@ module RBridge
243
241
  end
244
242
 
245
243
  def get_previous()
246
- p @results
247
244
  if @results.size > 0
248
245
  r_obj = @results.last[1]
249
246
  return r_obj
@@ -1,3 +1,3 @@
1
1
  module RBridge
2
- VERSION = "0.5.3"
2
+ VERSION = "0.5.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: r_bridge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshihiro Umehara
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-22 00:00:00.000000000 Z
11
+ date: 2021-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi