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

Unable to give different names to R class and Cpp Class #909

Open
nico-schmid opened this issue Oct 5, 2018 · 3 comments
Open

Unable to give different names to R class and Cpp Class #909

nico-schmid opened this issue Oct 5, 2018 · 3 comments

Comments

@nico-schmid
Copy link

@nico-schmid nico-schmid commented Oct 5, 2018

Since the latest changes of the package it is no more possible to name the R Class differently from the Cpp Class.

I'm wirting a package and use the Rcpp package.
In particular I used the had

#include <Rcpp.h>
#include "PkgFile.h"
using namespace PkgNamespace;

RCPP_MODULE(PkgModuleName) {
     using namespace Rcpp;
	class_<PkgNamespace::ClassA>("ClassA")
		.constructor()
		.constructor< std::vector<double>, std::vector<double>, int >()
		.property("prop1",  &PkgNamespace::ClassA::prop1, "Returns prop1 vector")
		.property("prop2", &PkgNamespace::ClassA::prop2, "la le lu info")
		.method("getValue", &PkgNamespace::ClassA::value, "la le lu info")
	;
	}

on the Cpp side and

Rcpp::loadModule("PkgModuleName", what = "ClassA")
.ClassA <- setRcppClass(
  Class = ".ClassA",
  CppClass = "ClassA",
  module = "PkgModuleName",
  fields = c(
     remark = "character"
  )
)
ClassA <- function(prop1,prop2){
  tmpObj <- .ClassA()
  tmpObj$doSomething()
  return(tmpObj)
}

evalqOnLoad({
.ClassA$methods(
  doSomething = function(){
  # do something
  })
})

on the R side.
That worked fine, at least until Rcpp version 0.12.17.
But since version 0.12.19 I get a error when trying to build my package, saying the ".ClassA" can't be found in modul "PkgModuleName"
The only fix I found is to change the setRcppClass property "class" form ".ClassA" to "ClassA" and hence to the same name as the cpp class.
Did I miss something or why was this changed?

Best regards
Nico Schmid

@eddelbuettel
Copy link
Member

@eddelbuettel eddelbuettel commented Oct 5, 2018

I think that maybe be related to registration from NAMESPACE et al and encouraged by R, though those changes were made a year or so ago and not since 0.12.17. The actual Modules code has not changed -- but the registration we now provide discourages the leading dot. You could try an underscore which is symbol understood by both R and C++.

(And, if I may: We test all changes against all (or, essentially all) CRAN packages. The best defence against surprises of this type is to have your code out there as CRAN would not let us upgrade it side effects are found. Hence, surprises tend to be few. I still suspect this may be due to the change brought upon by R.)

@nico-schmid
Copy link
Author

@nico-schmid nico-schmid commented Oct 7, 2018

Thanks for the quick response.
...and yes of course you may... :)
I currently working on publishing the package on CRAN, which forced me to check the package with the latest version of dependencies. That was where the problem started... ;)

I tried to figure out what changes caused the problem and my best guess is, that it was the
bugfixes for Rcpp::exposeClass #886 which was done in Juli this year.
Since than the environment is now searched for CppClass instead of Class...
does nobody else reported problems with this change in behavior?

On the other hand I'm quite certain that it does not relate to the registration but I will try to double check that. In addition thanks for the hint with the leading underscore, but does this also makes the R object invisible to the user?

Again thanks for the answer!

@eddelbuettel
Copy link
Member

@eddelbuettel eddelbuettel commented Oct 7, 2018

does nobody else reported problems with this change in behavior?

Correct. And I believe we have a unit test for RcppClasses too.

does this also makes the R object invisible to the user

Of course not. But Rcpp Attributes switches to a leading underscore and 1000+ CRAN packages use that (when they updated their RcppExports.{cpp,R}.

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.