Class: RObj

Inherits:
Object
  • Object
show all
Defined in:
lib/rsruby/robj.rb

Overview

Synopsis

This class represents a reference to an object in the R interpreter. It also holds a conversion mode used if the RObj represents a callable function. RObj objects can be passed to R functions called from Ruby and are the default return type if RSRuby cannot convert the returned results of an R function.

Author

Alex Gutteridge

Copyright © 2006 Alex Gutteridge

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ++

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#conversionObject

Returns the value of attribute conversion.



33
34
35
# File 'lib/rsruby/robj.rb', line 33

def conversion
  @conversion
end

#wrapObject

Returns the value of attribute wrap.



33
34
35
# File 'lib/rsruby/robj.rb', line 33

def wrap
  @wrap
end

Instance Method Details

#__init_lcall__(args) ⇒ Object

lcall method that is safe to call during RSRuby initialisation



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
# File 'ext/robj.c', line 91

VALUE RObj_init_lcall(VALUE self, VALUE args){
  SEXP  exp, e, res;
  SEXP  r_obj;
  VALUE obj;

  //Ensure we have an array
  args = rb_check_array_type(args);

  // A SEXP with the function to call and the arguments
  PROTECT(exp = allocVector(LANGSXP, RARRAY_LEN(args)+1));
  e = exp;

  Data_Get_Struct(self, struct SEXPREC, r_obj);

  SETCAR(e, r_obj);
  e = CDR(e);

  // Add the arguments to the SEXP
  if (!make_argl(args, &e)) {
    UNPROTECT(1);
    return Qnil;
  }

  // Evaluate
  PROTECT(res = do_eval_expr(exp));
  if (!res) {
    UNPROTECT(2);
    return Qnil;
  }

  obj = to_ruby_with_mode(res, BASIC_CONVERSION);

  UNPROTECT(2);

  return obj;
}

#as_rObject



35
36
37
# File 'lib/rsruby/robj.rb', line 35

def as_r
  self
end

#autoconvert(m = false) ⇒ Object

Sets the conversion mode for this RObj (only relevant if the RObj represents a function). See the constants in RSRuby for valid modes. Returns the current conversion mode if called with no argument.



57
58
59
60
61
62
63
# File 'lib/rsruby/robj.rb', line 57

def autoconvert(m=false)
  if m
    raise ArgumentError if m < -1 or m > RSRuby::TOP_CONVERSION
    @conversion = m
  end
  @conversion      
end

#call(*args) ⇒ Object

Attempts to call the RObj with the arguments given. Returns the result of calling the R object. Only use this method if the RObj represents an R function.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rsruby/robj.rb', line 42

def call(*args)
  if @wrap
    e = RSRuby.get_default_mode
    RSRuby.set_default_mode(@wrap)
    ret = self.lcall(RSRuby.convert_args_to_lcall(args))
    RSRuby.set_default_mode(e)
  else
    ret = self.lcall(RSRuby.convert_args_to_lcall(args))
  end
  return ret
end

#lcall(args) ⇒ Object

Explicitly call an R object with a list containing (name, value) * argument pairs. ‘name’ can be None or ” to provide unnamed arguments. This function is necessary when the order of named arguments needs to be preserved.



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
# File 'ext/robj.c', line 42

VALUE RObj_lcall(VALUE self, VALUE args){
  SEXP  exp, e, res;
  SEXP  r_obj;
  int conv, default_mode;
  VALUE obj;

  //Ensure we have an array
  args = rb_check_array_type(args);

  // A SEXP with the function to call and the arguments
  PROTECT(exp = allocVector(LANGSXP, RARRAY_LEN(args)+1));
  e = exp;

  Data_Get_Struct(self, struct SEXPREC, r_obj);

  SETCAR(e, r_obj);
  e = CDR(e);

  // Add the arguments to the SEXP
  if (!make_argl(args, &e)) {
    UNPROTECT(1);
    return Qnil;
  }

  // Evaluate
  PROTECT(res = do_eval_expr(exp));
  if (!res) {
    UNPROTECT(2);
    return Qnil;
  }

  default_mode = NUM2INT(rb_iv_get(RSRUBY,"@default_mode"));

  // Convert
  if (default_mode < 0){
    conv = NUM2INT(rb_iv_get(self,"@conversion"));
  } else {
    conv = default_mode;
  }

  obj = to_ruby_with_mode(res, conv);

  UNPROTECT(2);

  return obj;
}

#to_ruby(args) ⇒ Object



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
# File 'ext/robj.c', line 172

VALUE RObj_to_ruby(VALUE self, VALUE args){

  int conv;
  VALUE obj;
  SEXP robj;

  args = rb_check_array_type(args);

  if (RARRAY_LEN(args) > 1){
    rb_raise(rb_eArgError,"Too many arguments in to_ruby\n");
  }

  if (RARRAY_LEN(args) == 0){
    conv = NUM2INT(rb_iv_get(RSRUBY,"@default_mode"));
  } else {
    conv = NUM2INT(rb_ary_entry(args,0));
  }

  if (conv <= -2 || conv > TOP_MODE) {
    rb_raise(rb_eArgError, "Wrong mode\n");
    return Qnil;
  }

  if (conv < 0)
    conv = TOP_MODE;

  Data_Get_Struct(self, struct SEXPREC, robj);

  obj = to_ruby_with_mode(robj, conv);
  return obj;

}