Skip to content

DataFrame converting to list on push_back with some c++ standards/compilers #1094

@waltersom

Description

@waltersom

I want to construct a data frame in Rcpp, with some variable number of columns. To do this, I am using push_back().

However, depending on compiler options, this results in the data frame converting into a list. Something similar was observed back in 2011 in https://stackoverflow.com/questions/8631197/constructing-a-data-frame-in-rcpp.

If I have CXX=g++ --std=gnu++11 in my ~/.R/Makevars (the default; alternatively, if I leave ~/.R/Makevars empty, or clang++ with any standard), I get the following

library(Rcpp)

A <- cppFunction(code='DataFrame make_df() {
NumericVector v(1);
DataFrame df = DataFrame::create(_["v"] = v);
return(df);}', rebuild = TRUE)

B <- cppFunction(code='DataFrame make_df() {
NumericVector v(1);
DataFrame df = DataFrame();
df.push_back(v, "v");
return(df);}', rebuild = TRUE)

A() # Constructed with create()
#>   v
#> 1 0
B() # Constructed with push_back()
#> $v
#> [1] 0

Created on 2020-06-25 by the reprex package (v0.3.0)

I would expect that A() and B() are the same.

If I specify any other standard to g++ (I tried gnu++14, gnu++17, gnu++2a, not specifying a standard), then I get the following, which is what I would expect

library(Rcpp)

A <- cppFunction(code='DataFrame make_df() {
NumericVector v(1);
DataFrame df = DataFrame::create(_["v"] = v);
return(df);}', rebuild = TRUE)

B <- cppFunction(code='DataFrame make_df() {
NumericVector v(1);
DataFrame df = DataFrame();
df.push_back(v, "v");
return(df);}', rebuild = TRUE)

A() # Constructed with create()
#>   v
#> 1 0
B() # Constructed with push_back()
#>   v
#> 1 0

Created on 2020-06-25 by the reprex package (v0.3.0)

I know that I can use as.data.frame() on the result, or Rcpp::as<Rcpp::DataFrame>(), to convert back to a data frame to get what I want, but this compiler-dependent behaviour seems like a bug.

sessionInfo()
#> R version 4.0.0 (2020-04-24)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Debian GNU/Linux bullseye/sid
#> 
#> Matrix products: default
#> BLAS:   /home/walter/R/lib/R/lib/libRblas.so
#> LAPACK: /home/walter/R/lib/R/lib/libRlapack.so
#> 
#> locale:
#>  [1] LC_CTYPE=en_NZ.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_NZ.UTF-8        LC_COLLATE=en_NZ.UTF-8    
#>  [5] LC_MONETARY=en_NZ.UTF-8    LC_MESSAGES=en_NZ.UTF-8   
#>  [7] LC_PAPER=en_NZ.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_NZ.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> loaded via a namespace (and not attached):
#>  [1] compiler_4.0.0  magrittr_1.5    tools_4.0.0     htmltools_0.4.0
#>  [5] yaml_2.2.1      Rcpp_1.0.4.6    stringi_1.4.6   rmarkdown_2.1  
#>  [9] highr_0.8       knitr_1.28      stringr_1.4.0   xfun_0.14      
#> [13] digest_0.6.25   rlang_0.4.6     evaluate_0.14

Created on 2020-06-25 by the reprex package (v0.3.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions