r_bridge 0.5.0 → 0.5.1

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: cbeed05e728b4a713d765b889957fde99c7afa4a8379bbe5220ec39401c62de8
4
- data.tar.gz: 1fd7ebe821f80b039fd6ec84311fea02a4feebda0552c62ddb0673fb4999bbd3
3
+ metadata.gz: 678aaf66ad022546771461a682f5dee6efa065402c8890197ac3e809cb491cda
4
+ data.tar.gz: 6e4c24a1f8fc85599707ca0aa7799a8b7ef7bfbb0fcb7e1f1cc720f58f179046
5
5
  SHA512:
6
- metadata.gz: 22332956afff7dd86e4803b12912f5f349e04d390fb7d16b94f0bbefbd1fb74313d294bf9045c556ba6ee43c27d1e29a39c88c13877ac4a9d9cc60cec7ebf25a
7
- data.tar.gz: d9b515e48c8ee6df50b2f1d14d2f75623bac6b1f933e10e00f6ae1626788a7907d9d38093bda13bc8cfdb92ca34e58cd2bc7a8bf8945ad03deae3e7ad7c72ed7
6
+ metadata.gz: 2eabdfe3403067cd795090593deeb96ba2a7d70ace07e9e7540d598df234134bf5969143fcdc27695a09bf65692c24e6fb78db3f59a91b3ce8d741f43382b5c1
7
+ data.tar.gz: eb0e3cd227b22842584c686b9c93af7ca36434d2754132f58544fcc72b69b406630639abf2a021f2c2c160ecd3c6451d4c842517b806299198aaa5bf48a73a13
data/README.md CHANGED
@@ -41,12 +41,14 @@ Depending on installing via source or binary, necessary settings differ. If you
41
41
  + This path should have libR.pc file.
42
42
  5. For R to properly work, R_HOME variable is sometimes required. Set it to the root directory that contains R libraries.
43
43
  + (e.g.) export R_HOME=/usr/lib64/R
44
- 6. Install 'r_bridge' via gem
44
+ 6. Install 'r_bridge' gem
45
45
 
46
46
  ```
47
47
  gem install r_bridge
48
48
  ```
49
49
 
50
+
51
+
50
52
  * For Windows
51
53
 
52
54
  For windows, there is a known issue. Output from R contains unknown characters, which may relate to UTF16 conversion within Windows.
@@ -57,15 +59,13 @@ For windows, there is a known issue. Output from R contains unknown characters,
57
59
  2. Make sure that R.exe and R.dll can be seen from your system.
58
60
  + For Windows, add path for R.exe (e.g. C:\Program Files\R\R-4.0.2\bin ) to 'Path' system or user variable.
59
61
  + Also, create 'RUBY_DLL_PATH' system or user variable, which includes path for R.dll (e.g. C:\Program Files\R\R-4.0.2\bin\x64 ).
60
- 3. Install 'r_bridge' via gem
62
+ 3. Install 'r_bridge' gem
61
63
 
62
64
  ```
63
65
  gem install r_bridge
64
66
  ```
65
67
 
66
68
 
67
-
68
-
69
69
  ## Example
70
70
 
71
71
  * Example 1
@@ -203,6 +203,16 @@ ary = [ RBridge::SymbolR.new("y"),
203
203
  formula = RBridge::create_formula_from_syms( ary ) # y ~ x
204
204
  ```
205
205
 
206
+ * Create EXTPTRSXP type object
207
+
208
+ If you need to deal with external pointers in R, you can hold them with EXTPTRSXP type objects.
209
+
210
+ create_extptr takes FFI::Pointer as its argument, and creates EXTPTRSXP object. Internally R_MakeExternalPtr() C function is used.
211
+
212
+ ```
213
+ RBridge.create_extptr( ffi_ptr )
214
+ ```
215
+
206
216
 
207
217
  * Create R function call
208
218
 
data/Rakefile CHANGED
@@ -1,10 +1,44 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rake/testtask"
3
+ require 'rake/clean'
3
4
 
5
+ message_for_rake_compiler = "This gem requires native extension compilation. rake-compiler gem should be installed and 'rake compile' before 'rake test'."
6
+
7
+ begin
8
+ require "rake/extensiontask"
9
+ rescue LoadError => e
10
+ puts "ERROR:" + message_for_rake_compiler
11
+ raise
12
+ end
13
+ raise ("ERROR:" + message_for_rake_compiler) unless defined? Rake::ExtensionTask
14
+
15
+ # rake compile
16
+ Rake::ExtensionTask.new do |ext|
17
+ ext.name = "librbridge" # This should be same as shared library name specified in create_makefile of extconf.rb
18
+ ext.ext_dir = "ext/r_bridge"
19
+ ext.lib_dir = "lib/r_bridge"
20
+ end
21
+
22
+ # rake test
4
23
  Rake::TestTask.new(:test) do |t|
5
24
  t.libs << "test"
6
25
  t.libs << "lib"
7
26
  t.test_files = FileList["test/**/*_test.rb"]
8
27
  end
9
28
 
29
+ # rake example
30
+ task :example do
31
+ file_list = Dir.glob("example/example*.rb")
32
+ file_list.each(){|path|
33
+ puts "###### Running script #{path} #####"
34
+ ruby path
35
+ puts ""
36
+ }
37
+ end
38
+
39
+ # rake clean
40
+ CLEAN.include(["tmp/", "lib/r_bridge/librbridge.so"])
41
+
42
+
10
43
  task :default => :test
44
+ task :test => :compile
@@ -0,0 +1,56 @@
1
+ require("r_bridge")
2
+
3
+ RBridge.init_embedded_r()
4
+
5
+ fun = RBridge.create_function_call("getwd", {} )
6
+ path = RBridge.exec_function(fun)
7
+ RBridge.exec_function( RBridge.create_function_call( "print", { "x" => path } ))
8
+
9
+ RBridge.exec_function( RBridge.create_function_call( "print", { "x" => RBridge.create_function_call("getwd", {} ) } ))
10
+
11
+ RBridge.ptr_manager_open ("mean_function") {
12
+ i_v = RBridge.create_vec([0,1,2,3,4,5,6,7,8,9,10,50])
13
+ r_v = RBridge.create_vec( [0.2] )
14
+ func = RBridge.create_function_call( "mean", {"trim" => r_v , "x" => i_v })
15
+ result = RBridge.exec_function(func)
16
+
17
+ # print RBridge.confirm_type( result, :REALSXP )
18
+ func2 = RBridge.create_function_call( "print", { "x" => result } )
19
+ RBridge.exec_function(func2)
20
+ }
21
+
22
+ RBridge.ptr_manager_open("cat hello world"){
23
+ s_v = RBridge.create_vec( ["Hello", "World", "from", "Ruby+R", "\n"] )
24
+ func3 = RBridge.create_function_call( "cat", { "" => s_v } )
25
+ RBridge.exec_function_no_return(func3) # Here exec_function() raises error b/c return value is nil.
26
+ }
27
+
28
+ RBridge.ptr_manager_open("prepare regression analysis"){
29
+ ary = [ RBridge::SymbolR.new("y"),
30
+ RBridge::SymbolR.new("~"),
31
+ RBridge::SymbolR.new("x")]
32
+ formula = RBridge::create_formula_from_syms( ary )
33
+
34
+ df = RBridge::create_dataframe( { "y" => [12, 13, 14, 18 , 17, 20, 20 ] , "x" => [ 1, 2, 3, 4, 5, 6, 7]} )
35
+
36
+ # assign to R variable
37
+ RBridge::exec_function( RBridge::create_assign_function( "formula" , formula ) )
38
+ RBridge::exec_function( RBridge::create_assign_function( "df" , df ))
39
+ }
40
+
41
+ RBridge.ptr_manager_open("run regression analysis. lm(formula, data) "){
42
+ # obtain from R variable
43
+ formula = RBridge::SymbolR.new("formula").to_r_symbol
44
+ df = RBridge::SymbolR.new("df").to_r_symbol
45
+
46
+ func4 = RBridge.create_function_call( "lm", { "formula" => formula , "data" => df } )
47
+ model = RBridge.exec_function(func4)
48
+ func5 = RBridge.create_function_call( "summary", { "object" => model })
49
+ summary = RBridge.exec_function(func5)
50
+ func6 = RBridge.create_function_call( "print", { "x" => summary })
51
+ RBridge.exec_function(func6)
52
+ }
53
+
54
+ RBridge.gc_all()
55
+ RBridge.end_embedded_r()
56
+
@@ -0,0 +1,23 @@
1
+ require "r_bridge"
2
+ RBridge.init_embedded_r()
3
+
4
+ RBridge.ptr_manager_open("prepare regression analysis"){
5
+ ary = [ RBridge::SymbolR.new("y"),
6
+ RBridge::SymbolR.new("~"),
7
+ RBridge::SymbolR.new("x")]
8
+ formula = RBridge::create_formula_from_syms( ary )
9
+
10
+ df = RBridge::create_dataframe( { "y" => [12, 13, 14, 18 , 17, 20, 20 ] , "x" => [ 1, 2, 3, 4, 5, 6, 7]} )
11
+
12
+ RBridge.ptr_manager_open("a little break"){
13
+ RBridge::exec_function_no_return(RBridge::create_function_call("print", {"x" => RBridge::create_vec( ["(^^)" , "<" , "Hello"] ) }))
14
+ }
15
+
16
+ # assign to R variable
17
+ reg = RBridge::exec_function( RBridge::create_function_call( "lm" , {"data" => df , "formula" => formula } ) )
18
+ summary = RBridge::exec_function( RBridge::create_function_call( "summary" , {"object" => reg } ) )
19
+ RBridge::exec_function_no_return(RBridge::create_function_call( "print" , {"x" => summary }))
20
+ }
21
+
22
+ RBridge.gc_all() # In this case, not necesary. ( When there are objects that are not managed by ptr_manager, this is necessary.)
23
+ RBridge.end_embedded_r()
@@ -0,0 +1,10 @@
1
+ require "r_bridge"
2
+ RBridge.init_embedded_r()
3
+
4
+ RBridge.ptr_manager_open("pm1")
5
+ new_vec = RBridge.create_vec(["Hello", "World"])
6
+ RBridge.exec_function_no_return( RBridge.create_function_call("print", {"x" => new_vec }))
7
+ RBridge.ptr_manager_close("pm1")
8
+
9
+ RBridge.gc_all() # In this case, not necesary. ( When there are objects that are not managed by ptr_manager, this is necessary.)
10
+ RBridge.end_embedded_r()
@@ -0,0 +1,14 @@
1
+ require "r_bridge"
2
+
3
+ RBridge.init_embedded_r()
4
+
5
+ getwd = RBridge.create_function_call( "print", { "x" => RBridge.create_function_call( "getwd", {} ) } )
6
+ RBridge.exec_function_no_return(getwd)
7
+
8
+ lcons1 = RBridge.lcons_gen( RBridge.create_vec(["Hello", "World"]) )
9
+ RBridge.set_tag_to_lcons( lcons1, "x")
10
+ lcons2 = RBridge.lcons(RBridge.r_lang_symbol("print") , lcons1)
11
+ RBridge.exec_function_no_return(lcons2)
12
+
13
+ RBridge.gc_all()
14
+ RBridge.end_embedded_r()
@@ -43,6 +43,14 @@ r_lang_symbol( const char* symbol_name)
43
43
  return r_symbol;
44
44
  }
45
45
 
46
+ EXPORT SEXP
47
+ r_lang_create_extptr( void* ptr )
48
+ {
49
+ SEXP extptr;
50
+ PROTECT( extptr = R_MakeExternalPtr( ptr, R_NilValue, R_NilValue ));
51
+ return extptr;
52
+ }
53
+
46
54
  EXPORT SEXP
47
55
  r_lang_nil()
48
56
  {
@@ -30,6 +30,8 @@ module RBridge
30
30
  attach_function :r_lang_set_tag, [:pointer, :string], :void
31
31
  attach_function :r_lang_symbol, [:string], :pointer
32
32
 
33
+ attach_function :r_lang_create_extptr, [:pointer], :pointer
34
+
33
35
  attach_function :r_eval, [:pointer], :pointer
34
36
  attach_function :r_eval_no_return, [:pointer], :pointer
35
37
 
@@ -185,7 +187,6 @@ module RBridge
185
187
  when FalseClass
186
188
  1
187
189
  else
188
- nil
189
190
  raise " The current elemnt is not supported to convert to R vector : " + elem
190
191
  end
191
192
  }
@@ -208,6 +209,7 @@ module RBridge
208
209
  when 1
209
210
  raise "All the elements shold be true/false. Current value: " + ary[idx]
210
211
  end
212
+ converted
211
213
  else
212
214
  ary[idx]
213
215
  end
@@ -283,6 +285,15 @@ module RBridge
283
285
  r_lang_set_tag( lcons, tag_name )
284
286
  end
285
287
 
288
+ def self.create_extptr( ffi_pointer )
289
+ raise "create_extptr should take a FFI::Pointer argument" if(ffi_pointer.class != FFI::Pointer)
290
+
291
+ extptr = r_lang_create_extptr(ffi_pointer)
292
+
293
+ ptr_manager_add_ptr_to_current( extptr )
294
+ return extptr
295
+ end
296
+
286
297
  def self.create_function_call( fname, hash )
287
298
  raise "create_function_call should take String for function name" if(fname.class != String)
288
299
  raise "create_function_call should take Hash for function arguments" if(hash.class != Hash)
@@ -7,7 +7,7 @@ module RBridge
7
7
  return LazyFunc.new( fname, hash , param_manager)
8
8
  end
9
9
 
10
- def self.create_function_call_from_lazy_function ( fname, fargs, param_manager, result_manager)
10
+ def self.create_function_call_from_lazy_function( fname, fargs, param_manager, result_manager)
11
11
  farg_keys = fargs.keys
12
12
 
13
13
  new_arg_hash = {}
@@ -1,3 +1,3 @@
1
1
  module RBridge
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -6,8 +6,8 @@ Gem::Specification.new do |spec|
6
6
  spec.authors = ["Toshihiro Umehara"]
7
7
  spec.email = ["toshi@niceume.com"]
8
8
 
9
- spec.summary = %q{Enables Ruby to access and construct R internal objects}
10
- spec.description = %q{R (language) provides C interface. This package utilize the interface and allow Ruby to manipulate and evaluate R's internal S expressions }
9
+ spec.summary = %q{Enables Ruby to construct and evaluate R internal objects}
10
+ spec.description = %q{R (language) provides C interface. This package utilize the interface and allow Ruby to construct and evaluate R's internal S expressions }
11
11
  spec.homepage = "https://github.com/niceume/r_bridge"
12
12
  spec.license = "GPL-3.0"
13
13
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
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.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshihiro Umehara
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-28 00:00:00.000000000 Z
11
+ date: 2020-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -31,7 +31,7 @@ dependencies:
31
31
  - !ruby/object:Gem::Version
32
32
  version: 1.13.0
33
33
  description: 'R (language) provides C interface. This package utilize the interface
34
- and allow Ruby to manipulate and evaluate R''s internal S expressions '
34
+ and allow Ruby to construct and evaluate R''s internal S expressions '
35
35
  email:
36
36
  - toshi@niceume.com
37
37
  executables: []
@@ -47,6 +47,10 @@ files:
47
47
  - Rakefile
48
48
  - bin/console
49
49
  - bin/setup
50
+ - example/example.rb
51
+ - example/example_ptr_manager_block.rb
52
+ - example/example_ptr_manager_explicit_use.rb
53
+ - example/example_using_lcons.rb
50
54
  - ext/r_bridge/extconf.rb
51
55
  - ext/r_bridge/r_bridge.c
52
56
  - ext/r_bridge/r_embed.c
@@ -87,5 +91,5 @@ requirements: []
87
91
  rubygems_version: 3.1.2
88
92
  signing_key:
89
93
  specification_version: 4
90
- summary: Enables Ruby to access and construct R internal objects
94
+ summary: Enables Ruby to construct and evaluate R internal objects
91
95
  test_files: []