Background Color:
 
Background Pattern:
Reset
Tuesday, September 26, 2017
menu click
Search

R Var

using System;
using System.Collections;

namespace RNET {
public class RVar:ArrayList {

public RVar(R r, string callname, string name) {
this.Name=name;
this.Call=callname;
this.R=r;
}

public RVar(R r, string callname) {
this.Name=callname;
this.Call=callname;
this.R=r;
}

private R _r;
public R R{
get{return _r;}
set{_r=value;}
}

private string _cardinality;
public string Cardinality{
get{
if(_cardinality==null){
string cmd="is.ordered("+Call+")";
object o=R.Evaluate(cmd);
if((bool)o){
_cardinality=
"Ordered";
}
else{
cmd=
"is.factor("+Call+")";
o=R.Evaluate(cmd);
if((bool)o){
_cardinality=
"Factor";
}
else{
_cardinality=
"Numeric";
}
}
}
return _cardinality;
}
}

private string _name;
public string Name{
get{
if(_name==null && Names!=null){
_name=
String.Join(",",(string[])Names);
}
return _name;
}
set{_name=value;}
}

private string[] _names;
public string[] Names{
get{
if(_names==null){
try{
object names=R.Evaluate("names("+Call+")");
if(names!=null){
if (names.GetType()==typeof(string[])){
_names=(
string[])names;
}
else{
_names=
new string[1];
_names[0]=names.ToString();
}
}
}
catch(Exception exc){
_names=
new string[1];
_names[0]=
"Error:"+exc.Message;
}
}
return _names;
}
set{_names=value;}
}

private string _type;
public string Type{
get{
if(_type==null){
_type=(
string)R.Evaluate("mode("+Call+")");
}
return _type;
}
set{_type=value;}
}
private string _call;
public string Call{
get{return _call;}
set{_call=value;}
}

private RVar _attributes;
public RVar Atrributes{
get{
if(_attributes==null){
_attributes=
new RVar(R,"attributes("+Call+")",null);
}
return _attributes;
}
set{_attributes=value;}
}

private string _summary;
public string Summary{
get{return _summary;}
set{_summary=value;}
}
public override string ToString() {
string res=ToString(0);
return res.StartsWith("\n")?res.Substring(1):res;
}

public string ToString(int ident) {
string nwln="\n"+new string('_',ident);
string res=nwln+"Call:"+this.Call;
res+=nwln+
"Name:"+this.Name;
res+=nwln+
"Type:"+this.Type;
res+=nwln+
"Attributes:"+((Atrributes!=null)?Atrributes.ToString(ident+1):"");
res+=nwln+
"Summary:"+this.Summary;
foreach(object o in this){
if(o==null){
res+=nwln+
"NULL";
}
else if(o.GetType()==typeof(RVar)){
res+=nwln+((
RVar)o).ToString(ident+1);
}
else if(o.GetType().IsArray){
res+=nwln;
foreach(object o2 in (Array)o){
res+=o2.ToString()+
" ";
}
}
else{
res+=nwln+o.ToString();
}
}
return res;
}

public void init(){
int len;
object eval;
try{
switch(Type){
case "list":
len=(
int)R.Evaluate("length("+Call+")");
for(int i=1;i<=len;i++){
if (Names !=null){
Add(
new RVar(R,Call+"[["+i+"]]",((string[])Names)[i-1]));
}
else{
Add(
new RVar(R,Call+"[["+i+"]]"));
}
}
break;
case "NULL":
break;
//return null;
case "environment":
case "parlist":
case "call":
case "numeric":
case "character":
eval=R.Evaluate(Call);
if(eval!=null){
Add(eval);
}
break;
default:
eval=R.Evaluate(Call);
if(eval!=null){
Add(eval);
}
break;
}
}
catch(Exception exc){
Name+=
"-Error:"+exc.Message;
}
}
}
}