Module: R2rb

Defined in:
lib/R4rb/init.rb,
lib/R4rb/R2rb_eval.rb,
lib/R4rb/R2rb_init.rb,
lib/R4rb/converter.rb,
ext/R4rb/R4rb.c,
ext/R4rb/R4rb.4.1.c

Overview

Attention “…n…” have to be replaced by “…\n…” example : ‘cat(“toton”)’ fails but not ‘cat(“toto\n”)’ more surprisingly, this fails even in comment ‘# cat(“toton”)’

Defined Under Namespace

Classes: RBuffer, RConsole, RVector, Server

Constant Summary collapse

@@out =
[]
@@pair =
["<<",">>"]

Class Method Summary collapse

Class Method Details

.<(rcode) ⇒ Object Also known as: output

@@out.rb2R=R2rb



80
81
82
83
84
85
# File 'lib/R4rb/R2rb_eval.rb', line 80

def R2rb.<(rcode)
  @@out.replace [] ##@@out=[] #important! it could otherwise remove
  @@out.rb2R=self
  @@out < rcode.to_s ##@@out.inR2rb rcode.to_s
  return (@@out.length<=1 ? @@out[0] : @@out) 
end

.<<(s) ⇒ Object



22
23
24
# File 'lib/R4rb/R2rb_eval.rb', line 22

def R2rb.<<(s)
  R2rb.eval(s)
end

.alive?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/R4rb/init.rb', line 11

def R2rb.alive?
  defined? @@initR
end

.eval(s, aff = nil) ⇒ Object



5
6
7
8
# File 'lib/R4rb/R2rb_eval.rb', line 5

def R2rb.eval(s,aff=nil)
  s=["{\n"+s+"}\n"]
  evalLines s,aff
end

.evalLines(cmd, print) ⇒ Object

*************** EVAL *********************



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
127
# File 'ext/R4rb/R4rb.c', line 73

VALUE R2rb_eval(VALUE obj, VALUE cmd, VALUE print)
{
  char *cmdString;
  int nbCmds;
  VALUE tmp;
  int errorOccurred,status, i;

  SEXP text, expr, ans=R_NilValue /* -Wall */;


  //printf("Avant parsing\n");

  nbCmds=RARRAY_LEN(cmd);

  //printf("nbCmds : %d\n",nbCmds);

  text = PROTECT(allocVector(STRSXP, nbCmds));
  for (i = 0 ; i < nbCmds ; i++) {
    tmp=rb_ary_entry(cmd,i);
    cmdString=StringValuePtr(tmp);
    SET_STRING_ELT(text, i, mkChar(cmdString));
  }
  expr = PROTECT(RR_ParseVector(text, -1, &status));

  if (status != PARSE_OK) {
    //printf("Parsing error (status=%d) in:\n",status);
    for (i = 0 ; i < nbCmds ; i++) {
      tmp=rb_ary_entry(cmd,i);
      cmdString=StringValuePtr(tmp);
      //printf("%s\n",cmdString);
    }
    UNPROTECT(2);
    return Qfalse;
  }
  
  /* Note that expr becomes an EXPRSXP and hence we need the loop
     below (a straight eval(expr, R_GlobalEnv) won't work) */
  {
    for(i = 0 ; i < nbCmds ; i++)
      ans = R_tryEval(VECTOR_ELT(expr, i),NULL, &errorOccurred);
      if(errorOccurred) {
        //fprintf(stderr, "Caught another error calling sqrt()\n");
        fflush(stderr);
        UNPROTECT(2);
        return Qfalse;
      }

      if (print != Qnil) {
        Rf_PrintValue(ans);
      }
  }

  UNPROTECT(2);
  return Qtrue;
}

.init(args = ["--save","--slave","--quiet"]) ⇒ Object



7
8
9
# File 'lib/R4rb/init.rb', line 7

def R2rb.init(args=["--save","--slave","--quiet"])
  @@initR=R2rb.initR(args) unless R2rb.alive?
end

.initR(args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'ext/R4rb/R4rb.c', line 50

VALUE R2rb_init(VALUE obj, VALUE args)
{
  char **argv;//={"REmbed","--save","--slave","--quiet"};
  int i,argc;//=sizeof(argv)/sizeof(argv[0]);
  VALUE tmp;

  argc=RARRAY_LEN(args) + 1;
  //printf("argc=%d\n",argc);
  argv=malloc(sizeof(char*)*argc);
  argv[0]="REmbed";
  for (i = 1 ; i < argc ; i++) {
    tmp=rb_ary_entry(args,i-1);
    argv[i]=StringValuePtr(tmp);
    //printf("argv[%d]=%s\n",i,argv[i]);
  }
  //printf("argc=%d\n",argc);
  Rf_initEmbeddedR(argc,argv);
  R_Interactive = FALSE;
  return Qtrue;
}

.multilang_parser(str) ⇒ Object



33
34
35
36
37
38
39
40
41
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
# File 'lib/R4rb/converter.rb', line 33

def R2rb.multilang_parser(str)
#p @@pair
  lang="r|R|rb|Rb"
  s=str
  if @@pair[0].empty?
    mask = /((?:#{lang}).*)/
  else
    mask = /#{Regexp.escape(@@pair[0])}((?:#{lang})[^#{Regexp.escape(@@pair[0])}]*)#{Regexp.escape(@@pair[1])}/
  end
  str=str.split(mask,-1)
  return s if str.length<=1
  vals=[]
  res=[]
  inds=0..(str.length / 2 - 1)
  ".tmp<-NULL".to_R 
  inds.each {|i|
    res << str[2*i]
    if str[2*i+1] =~ /^(?:r|R):(.*)/
      (".tmp<-cbind(.tmp,"+$1+")").to_R
    end
    if str[2*i+1] =~ /^(?:rb|Rb):(.*)/
      a=eval($1)
      a=[a] unless a.is_a? Array
      a > :tmp
      (".tmp<-cbind(.tmp,tmp)").to_R
   end
  }
  res << str[-1]
#p res
  cmd="apply(.tmp,1,function(e) paste("
  inds.each{|i| cmd += "'"+res[i]+"',e["+i.to_s+"+1]," }
  cmd += "'"+res[-1]+"',sep=''))"
#"print(.tmp)".to_R
#p cmd
  cmd.to_R
end

.pairObject



16
17
18
# File 'lib/R4rb/converter.rb', line 16

def R2rb.pair
  @@pair
end

.pair=(pair) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/R4rb/converter.rb', line 20

def R2rb.pair=(pair)
  if pair.is_a? String
    pairs={"{"=>"}","("=>")","<"=>">","["=>"]"}
    pair2=pair.reverse.gsub(/[#{Regexp.escape(pairs.keys.join)}]/) {|e| Regexp.escape(pairs[e])}
    pair=[pair,pair2]
  end
#p "ICI";p pair
  if (pair.is_a? Array) and pair.length==2
    @@pair=pair
  end
#p @@pair
end

.parse(s, aff = nil) ⇒ Object



10
11
12
13
# File 'lib/R4rb/R2rb_eval.rb', line 10

def R2rb.parse(s,aff=nil)
  s=["{\n"+s+"}\n"]
  parseLines s,aff
end

.parseLines(cmd, print) ⇒ Object

*************** PARSE *********************



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'ext/R4rb/R4rb.c', line 131

VALUE R2rb_parse(VALUE obj, VALUE cmd,VALUE print)
{
  char *cmdString;
  int nbCmds;
  VALUE tmp;
  int status,i;

  SEXP text, expr, ans=R_NilValue /* -Wall */;


  //printf("Avant parsing\n");

  nbCmds=RARRAY_LEN(cmd);

  //printf("nbCmds : %d\n",nbCmds);

  text = PROTECT(allocVector(STRSXP, nbCmds));
  for (i = 0 ; i < nbCmds ; i++) {
    tmp=rb_ary_entry(cmd,i);
    cmdString=StringValuePtr(tmp);
    SET_STRING_ELT(text, i, mkChar(cmdString));
  }
  expr = PROTECT(RR_ParseVector(text, -1, &status));

  if (status != PARSE_OK) {
    if (print != Qnil) printf("Parsing error (status=%d) in:\n",status);
    for (i = 0 ; i < nbCmds ; i++) {
      tmp=rb_ary_entry(cmd,i);
      cmdString=StringValuePtr(tmp);
      if (print != Qnil) printf("%s\n",cmdString);
    }
    //UNPROTECT(2);
    //return Qfalse;
  }
  UNPROTECT(2);
  //return Qtrue;
  return INT2FIX(status);
}

.try_eval(code) ⇒ Object



15
16
17
18
19
# File 'lib/R4rb/R2rb_eval.rb', line 15

def R2rb.try_eval(code)
  try_code=".result_try_code<-try({\n"+code+"\n},silent=TRUE)\n.result_try_code"
  R2rb << try_code
  puts ".result_try_code".to_R if "inherits(.result_try_code,'try-error')".to_R
end