Class: R2rb::RVector

Inherits:
Object show all
Defined in:
lib/R4rb/R2rb_eval.rb,
ext/R4rb/R4rb.c

Direct Known Subclasses

Rserve::RVector

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Object



348
349
350
351
352
353
354
# File 'ext/R4rb/R4rb.c', line 348

VALUE RVect_initialize(VALUE self, VALUE name)
{
  rb_iv_set(self,"@name",name);
  rb_iv_set(self,"@type",rb_str_new2("var"));
  rb_iv_set(self,"@arg",rb_str_new2(""));
  return self;
}

Instance Attribute Details

#nameObject

[]= iter !!!

#typeObject

Class Method Details

.assign(name, arr) ⇒ Object



530
531
532
533
534
535
536
537
538
539
540
541
# File 'ext/R4rb/R4rb.c', line 530

VALUE RVect_assign(VALUE obj, VALUE name,VALUE arr)
{
  SEXP ans;
  char *tmp;

  ans=util_VALUE2SEXP(arr);

  tmp = StringValuePtr(name);
  defineVar(install(tmp),ans,R_GlobalEnv);

  return Qnil; 
}

Instance Method Details

#<<(name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/R4rb/R2rb_eval.rb', line 28

def <<(name)
  if name.is_a? Symbol
    @name=name.to_s
    @type="var"
  else
    @name=name
    @type="expr"
  end
  return self
end

#>(arr) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/R4rb/R2rb_eval.rb', line 50

def >(arr)
  res=self.get
#puts "res";p @name;p res
  if res
#puts "arr.class:";p arr.class
#puts "res.class";p res.class
    res=[res] unless res.is_a? Array
    arr.replace(res)
  else
    arr.clear
  end
  return self
end

#[](index) ⇒ Object

faster than self.to_a



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'ext/R4rb/R4rb.c', line 460

VALUE RVect_aref(VALUE self, VALUE index)
{
  SEXP ans;
  VALUE res;
  char *name;
  int n,i;
  Rcomplex cpl;
#ifdef cqls
  VALUE tmp;
#endif
  i = FIX2INT(index);
  
#ifdef cqls
  if(!RVect_isValid(self)) return Qnil;
  tmp=rb_iv_get(self,"@name");
  name = StringValuePtr(tmp);
  ans = findVar(install(name),R_GlobalEnv); //currently in  R_GlobalEnv!!!
#else
  ans = util_getVar(self);
#endif
  n=length(ans);
  //printf("i=%d and n=%d\n",i,n);
  if(i<n) {
    switch(TYPEOF(ans)) {
    case REALSXP:
      res=rb_float_new(REAL(ans)[i]);
      break;
    case INTSXP:
      res=INT2FIX(INTEGER(ans)[i]);
      break;
    case LGLSXP:
      res=(INTEGER(ans)[i] ? Qtrue : Qfalse);
      break;
    case STRSXP:
      res=rb_str_new2(CHAR(STRING_ELT(ans,i)));
      break;
    case CPLXSXP:
      rb_require("complex");
      cpl=COMPLEX(ans)[i];
      res = rb_eval_string("Complex.new(0,0)");
      rb_iv_set(res,"@real",rb_float_new(cpl.r));
      rb_iv_set(res,"@image",rb_float_new(cpl.i));
      break;
    }
  } else {
    res = Qnil;
  }
  return res;
}

#arg=(arg) ⇒ Object



39
40
41
# File 'lib/R4rb/R2rb_eval.rb', line 39

def arg=(arg)
  @arg=arg
end

#getObject Also known as: to_a, value



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'ext/R4rb/R4rb.c', line 402

VALUE RVect_get(VALUE self)
{
  SEXP ans;
  VALUE res;
  char *name;
  int n,i;
  Rcomplex cpl;
  VALUE res2; 

  //#define cqls
#ifdef cqls 
  VALUE tmp;
  if(!RVect_isValid(self)) return Qnil;
#else  
  ans = util_getVar(self);

  if(ans==R_NilValue) {
    //printf("Sortie de get avec nil\n");
    return Qnil;
  }
#endif
#ifdef cqls 
  tmp=rb_iv_get(self,"@name");
  name = StringValuePtr(tmp);
  ans = findVar(install(name),R_GlobalEnv); 
#endif

  res=util_SEXP2VALUE(ans);
  if(length(ans)==1) res=rb_ary_entry(res,0);
  return res; 
}

#get_with_argObject Also known as: value_with_arg

method “arg=” defined in eval.rb!! @arg initialized in method “initialize”



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'ext/R4rb/R4rb.c', line 434

VALUE RVect_get_with_arg(VALUE self)
{
  SEXP ans;
  VALUE res;
  char *name;
  int n,i;
  Rcomplex cpl;
  VALUE res2; 

  ans = util_getExpr_with_arg(self);
 
  if(ans==R_NilValue) {
    //printf("Sortie de get avec nil\n");
    return Qnil;
  }
  res=util_SEXP2VALUE(ans);
 
//printf("RVect_get_with_arg: length(ans)=%d\n",length(ans));
 if (length(ans)==1) res=rb_ary_entry(res,0);

  return res;
}

#lengthObject



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'ext/R4rb/R4rb.c', line 381

VALUE RVect_length(VALUE self)
{
  SEXP ans;
  char *name;
#ifdef cqls
  VALUE tmp;
  tmp=rb_iv_get(self,"@name");
  if(!RVect_isValid(self)) return Qnil;
  name = StringValuePtr(tmp);
  ans = findVar(install(name),R_GlobalEnv); //currently in  R_GlobalEnv!!!
#else
  ans = util_getVar(self);

  if(ans==R_NilValue) {
    //printf("Sortie de length avec nil\n");
    return Qnil;
  }
#endif
  return INT2NUM(length(ans));
}

#set(arr) ⇒ Object Also known as: <, value=



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'ext/R4rb/R4rb.c', line 510

VALUE RVect_set(VALUE self,VALUE arr)
{
  SEXP ans;
  char *name;
  VALUE tmp;

  ans=util_VALUE2SEXP(arr);
  
  tmp=rb_iv_get(self,"@name");
  name = StringValuePtr(tmp);
  if(util_isVariable(self)) {
    defineVar(install(name),ans,R_GlobalEnv); //currently in R_GlobalEnv!!!
  } else {
    defineVar(install(".rubyExport"),ans,R_GlobalEnv);
    util_eval1string(rb_str_cat2(rb_str_dup(rb_iv_get(self,"@name")),"<-.rubyExport"));
  }

  return self; 
}

#set_arg(arg) ⇒ Object

this method is the same as the previous one but return self! Let us notice that even by adding return self in the previous one I could not manage to execute (rvect.arg=“”).value_with_arg but fortunately rvect.set_arg(“”).value_with_arg is working!



45
46
47
48
# File 'lib/R4rb/R2rb_eval.rb', line 45

def set_arg(arg)
  @arg=arg
  return self
end

#set_with_arg(arr) ⇒ Object Also known as: value_with_arg=



543
544
545
546
547
548
549
550
# File 'ext/R4rb/R4rb.c', line 543

VALUE RVect_set_with_arg(VALUE self,VALUE arr)
{
  VALUE tmp;
  defineVar(install(".rubyExport"),util_VALUE2SEXP(arr),R_GlobalEnv);
  tmp=rb_iv_get(self,"@arg"); 
  util_eval1string(rb_str_cat2(rb_str_cat2(rb_str_dup(rb_iv_get(self,"@name")),StringValuePtr(tmp)),"<-.rubyExport"));
  return self;
}

#valid?Boolean

Returns:

  • (Boolean)


356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'ext/R4rb/R4rb.c', line 356

VALUE RVect_isValid(VALUE self)
{
  SEXP ans;
  char *name;

#ifdef cqls
  VALUE tmp;
  tmp=rb_iv_get(self,"@name");
  name = StringValuePtr(tmp);
  ans = findVar(install(name),R_GlobalEnv); //currently in  R_GlobalEnv!!!
#else
  ans = util_getVar(self);
#endif
  if(!util_isVector(ans)) {
#ifndef cqls
    VALUE tmp;
    tmp=rb_iv_get(self,"@name");
    name = StringValuePtr(tmp);
#endif
    rb_warn("%s is not a R vector !!!",name); //TODO name not defined
    return Qfalse;
  }
  return Qtrue;
}