Changeset: e5696122db98 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=e5696122db98
Modified Files:
        monetdb5/extras/rapi/Tests/rapi02.mal
        monetdb5/extras/rapi/Tests/rapi02.stable.out
        monetdb5/extras/rapi/rapi.c
Branch: RIntegration
Log Message:

R API: support for factor vectors in output


diffs (80 lines):

diff --git a/monetdb5/extras/rapi/Tests/rapi02.mal 
b/monetdb5/extras/rapi/Tests/rapi02.mal
--- a/monetdb5/extras/rapi/Tests/rapi02.mal
+++ b/monetdb5/extras/rapi/Tests/rapi02.mal
@@ -110,3 +110,6 @@ io.print(rintbd);
 rintbs:bat[:oid,:str] := rapi.eval("as.character(arg1)",binto);
 io.print(rintbs);
 
+# factors should be strings
+rintbf:bat[:oid,:str] := rapi.eval("as.factor(arg1)",binto);
+io.print(rintbf);
diff --git a/monetdb5/extras/rapi/Tests/rapi02.stable.out 
b/monetdb5/extras/rapi/Tests/rapi02.stable.out
--- a/monetdb5/extras/rapi/Tests/rapi02.stable.out
+++ b/monetdb5/extras/rapi/Tests/rapi02.stable.out
@@ -108,6 +108,8 @@ function user.main():void;
     io.print(rintbd);
     rintbs:bat[:oid,:str]  := rapi.eval("as.character(arg1)",binto);
     io.print(rintbs);
+    rintbf:bat[:oid,:str]  := rapi.eval("as.factor(arg1)",binto);
+    io.print(rintbf);
 end main;
 #Running R expression Re(fft(na.omit(arg1)))
 #--------------------------#
@@ -249,6 +251,18 @@ end main;
 [ 2@0, "1681692777"  ]
 [ 3@0, "1714636915"  ]
 [ 4@0, nil  ]
+# User R expression: as.factor(arg1)
+# Executed R expression ret <- 
as.data.frame((function(arg1){as.factor(arg1)})(arg1),nm=NA,stringsAsFactors=F)
+
+#--------------------------#
+# h    t  # name
+# void str  # type
+#--------------------------#
+[ 0@0, "1804289383"  ]
+[ 1@0, "846930886"  ]
+[ 2@0, "1681692777"  ]
+[ 3@0, "1714636915"  ]
+[ 4@0, nil  ]
 
 # 15:45:25 >  
 # 15:45:25 >  "Done."
diff --git a/monetdb5/extras/rapi/rapi.c b/monetdb5/extras/rapi/rapi.c
--- a/monetdb5/extras/rapi/rapi.c
+++ b/monetdb5/extras/rapi/rapi.c
@@ -300,17 +300,31 @@ str RAPIeval(Client cntxt, MalBlkPtr mb,
                        break;
                }
                case TYPE_str:  {
-                       if (!IS_CHARACTER(ret_col)) {
+                       SEXP levels;
+                       if (!IS_CHARACTER(ret_col) && !isFactor(ret_col)) {
                                msg = createException(MAL, "rapi.eval",
-                                               "wrong R column type for column 
%d, expected character",i);
+                                               "wrong R column type for column 
%d, expected character or factor",i);
                                goto wrapup;
                        }
                        b = BATnew(TYPE_void, TYPE_str, cnt);
                        BATseqbase(b, 0); b->T->nil = 0; b->T->nonil = 1; 
b->tkey = 0;
                        b->tsorted = 0; b->trevsorted = 0; b->tdense = 1;
+                       /* get levels once, since this is a function call */
+                       levels = GET_LEVELS(ret_col);
                        for( i =0; i< (int) cnt; i++){
-                               SEXP rse = STRING_ELT(ret_col, i);
-                               if (rse == NA_STRING ) {
+                               SEXP rse;
+                               if (isFactor(ret_col)) {
+                                       int ii = INTEGER(ret_col)[i];
+                                       if  (ii == NA_INTEGER) {
+                                               rse = NA_STRING;
+                                       } else {
+                                               rse =  STRING_ELT(levels, ii - 
1);
+                                       }
+                               }
+                               else {
+                                       rse = STRING_ELT(ret_col, i);
+                               }
+                               if (rse == NA_STRING) {
                                        b->T->nil = 1;
                                        b->T->nonil = 0;
                                        BUNappend(b,  str_nil, FALSE);
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to