[Rcpp-devel] trouble creating a dataframe

Dirk Eddelbuettel edd at debian.org
Tue Jun 5 20:35:13 CEST 2012


On 5 June 2012 at 10:32, Pratibha Rana wrote:
| I tried the code that you suggested on The R prompt and it worked as expected.
| However the same piece of code doesn't work in my C++ app.
| 
| try{
|         IntegerVector v = IntegerVector::create(1,2,3);
|         std::vector<std::string> s(3);
|         s[0] = "a";
|         s[1] = "a";
|         s[2] = "a";
|         DataFrame df = DataFrame::create(Named("a")=v, Named("b")=s);
|         return df;
| 
|     }
|     catch(std::exception &e) {
|         throw;
|     }
| 
| again I get 
| 
| (gdb) p  R_PV(df)
| $1 = 0

With all due respect, I provided a working, complete and reproducible example.

You respond with an cut-and-pasted section and one of line of output saying
"It ain't working".  

There is really nothing more I can do for you here. You need to debug this at
your end.

Sorry, Dirk


| 
| On Mon, Jun 4, 2012 at 6:02 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
| 
| 
|     On 4 June 2012 at 17:28, Pratibha Rana wrote:
|     | Something seems to go wrong in Vector_create::create__dispatch.
|     |
|     | (gdb) p t1
|     | $14 = (const Rcpp::traits::named_object<std::vector<std::basic_string
|     <char,
|     | std::char_traits<char>, std::allocator<char> >, std::allocator
|     | <std::basic_string<char, std::char_traits<char>, std::allocator<char> > >
|     > >
|     | &) @0x7fff5d321780: {name = "datatype", object = std::vector of length 2,
|     | capacity 2 = {"varchar", "varchar"}}
|     |
|     | (gdb) p t2
|     | $18 = (const Rcpp::traits::named_object<std::vector<int, std::allocator
|     <int> >
|     | > &) @0x7fff5d321760: {name = "length", object = std::vector of length 2,
|     | capacity 2 = {10, 10}}
|     |
|     | (gdb) p t3
|     | $19 = (const Rcpp::traits::named_object<std::vector<int, std::allocator
|     <int> >
|     | > &) @0x7fff5d321740: {name = "scale", object = std::vector of length 2,
|     | capacity 2 = {0, 0}}
|     |
|     | (gdb) p res
|     | $17 = {<Rcpp::RObject> = {<No data fields>}, <Rcpp::VectorBase<19, true,
|     | Rcpp::Vector<19> >> = {<Rcpp::traits::expands_to_logical__impl<19>> =
|     {<No data
|     | fields>}, <No data fields>}, <Rcpp::internal::eval_methods<19>> = {<No
|     data
|     | fields>}, cache = {<Rcpp::traits::proxy_cache<19>> = {p = 0x0}, <No data
|     | fields>}}
|     |
|     |
|     | The value of res was printed at line 159 in Vector__create.h . Using Wrap
|     ()
|     | makes it worse. I really need help. I have been trying to do this for 2
|     days
|     | now.
| 
|     What about the working example I just to the list?
| 
|     What about the following adapted from the unit test file for dataframes:
| 
| 
|     R> library(inline)
|     R>
|     R> fx <- cxxfunction(signature(), plugin="Rcpp", body='
|     +     IntegerVector v = IntegerVector::create(1,2,3);
|     +     std::vector<std::string> s(3);
|     +     s[0] = "a";
|     +     s[1] = "b";
|     +     s[2] = "c";
|     +     return DataFrame::create(Named("a")=v, Named("b")=s);
|     + ')
|     R>
|     R> fx()
|      a b
|     1 1 a
|     2 2 b
|     3 3 c
|     R>
| 
| 
|     I usually start from self-contained working examples and then try to
|     generalise to the specific issue at hand.
| 
|     Hope this helps,  Dirk
| 
| 
|     | On Mon, Jun 4, 2012 at 4:58 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
|     |
|     |
|     |     Hi Pratibha,
|     |
|     |     On 4 June 2012 at 16:12, Pratibha Rana wrote:
|     |     | I am at my wits end trying to create  a dataframe from a list of
|     values
|     |     that I
|     |     | have. here's the code
|     |     |
|     |     | try{
|     |     |        int size =  argTypes.getColumnCount();
|     |     |        std::vector<std::string> nameVec(size);
|     |     |        std::vector<int> lenVec(size);
|     |     |        std::vector<int> scaleVec(size);
|     |
|     |     Good. Three STL vectors which should get autmatic conversion via
|     wrap().
|     |
|     |     [...]
|     |
|     |     |        //create the dataframe
|     |     |        Rcpp::List df = Rcpp::List::create(Rcpp::Named
|     |     ("datatype",nameVec),
|     |     |                                          
|     |     Rcpp::Named("length",lenVec),
|     |     |                                          
|     |     Rcpp::Named("scale",scaleVec));
|     |     |        Rcpp::DataFrame df_final = Rcpp::DataFrame::create
|     (df);
|     |     |        return df_final;
|     |
|     |     [...]
|     |
|     |     | The std::vectors are created fine but the list is not created
|     |     | (gdb) p R_PV(df)
|     |     | $3 = 0
|     |     |
|     |     |
|     |     | I have tried a lot of variation like creating the dataframe
|     directly
|     |     without
|     |     | first creating the list. Nothing seems to be working.
|     |
|     |     It should work. I believe we posted example on the list, blog,
|     possibly in
|     |     RcppExamples, ...
|     |
|     |
|     |     In fact, the latter one may be the best key.  Consider this
|     example taken
|     |     straight from the RcppExamples packages (which never became "the"
|     |     collection
|     |     of examples [ contributions welcome ] but has this ...)
|     |
|     |     RcppExport SEXP RcppDataFrame(SEXP Dsexp) {
|     |
|     |        try {                                      
|     // or
|     |     use BEGIN_RCPP macro
|     |
|     |          // construct the data.frame object
|     |          Rcpp::DataFrame DF = Rcpp::DataFrame(Dsexp);
|     |
|     |          // and access each column by name
|     |          Rcpp::IntegerVector a = DF["a"];
|     |          Rcpp::CharacterVector b = DF["b"];
|     |          Rcpp::DateVector c = DF["c"];
|     |
|     |          // do something
|     |          a[2] = 42;
|     |          b[1] = "foo";
|     |          c[0] = c[0] + 7;                      // move up
|     a week
|     |
|     |          // create a new data frame
|     |          Rcpp::DataFrame NDF =
|     |              Rcpp::DataFrame::create(Rcpp::Named("a")=a,
|     |                                      Rcpp::Named("b")=
|     b,
|     |                                      Rcpp::Named("c")=
|     c);
|     |
|     |          // and return old and new in list
|     |          return(Rcpp::List::create(Rcpp::Named("origDataFrame")=DF,
|     |                                    Rcpp::Named
|     ("newDataFrame")=
|     |     NDF));
|     |
|     |        } catch( std::exception &ex ) {             // or use
|     END_RCPP
|     |     macro
|     |            forward_exception_to_r( ex );
|     |        } catch(...) {
|     |            ::Rf_error( "c++ exception (unknown reason)" );
|     |        }
|     |        return R_NilValue; // -Wall
|     |     }
|     |
|     |
|     |     The main difference is that we use Rcpp vectors.  So you could try
|     |
|     |      -- wrap() in the create() call
|     |      -- converting the Rcpp vectors to STL vectors
|     |      -- debugging why STL vectors don't pass through
|     |
|     |     Please keep the list posted on your progress.
|     |
|     |     Dirk
|     |
|     |     --
|     |     Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
|     |
|     |
|     |
|     | ----------------------------------------------------------------------
|     | _______________________________________________
|     | Rcpp-devel mailing list
|     | Rcpp-devel at lists.r-forge.r-project.org
|     | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
|     --
|     Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
| 
| 
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com  


More information about the Rcpp-devel mailing list