Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle passing deflation scheme encoding between R and C++ #60

Open
Banana1530 opened this issue Aug 23, 2019 · 1 comment
Open

Handle passing deflation scheme encoding between R and C++ #60

Banana1530 opened this issue Aug 23, 2019 · 1 comment

Comments

@Banana1530
Copy link
Collaborator

Hmmm.... it looks like the answer is "sort of." The following works, but isn't totally type-safe. We could probably do a more general solution in the future. Issue?

#include "Rcpp.h"

enum DeflationMethod {
  PCA = 0, 
  PLS = 1,  
  CCA = 2
};

namespace Rcpp {
  SEXP wrap(DeflationMethod dm){
    Rcpp::IntegerVector dm_int = Rcpp::wrap(static_cast<int>(dm));
    dm_int.attr("class") = "DeflationMethod"; 
    return dm_int;
  }

  template <> DeflationMethod as(SEXP dm_sexp){
    Rcpp::IntegerVector dm_iv(dm_sexp);
    int dm_int = dm_iv(0); 
    DeflationMethod dm = static_cast<DeflationMethod>(dm_int);
    return dm; 
  }
}

// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
DeflationMethod make_pls(){
  DeflationMethod pls = DeflationMethod::PLS;
  return pls;
}

// [[Rcpp::export]]
void take_pls(DeflationMethod x){
  Rcpp::Rcout << " x = " << x << std::endl; 
}

Originally posted by @michaelweylandt in #54

@Banana1530
Copy link
Collaborator Author

Banana1530 commented Aug 23, 2019

In the code C++ and R have their own enum-type structure to encode selection schemes and deflation schemes (DeflationScheme and SelectionScheme in moma_base.h, and DEFLATION_SCHEME and SELECTION_SCHEME in util.R). However communication between them is essentially passing integers.

To ensure consistency in encoding between R and C++ we need to manually maintain moma_base.h and util.R.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant