Suppose I have the following two classes in R:
setClass("Person", representation(name = "character", age = "numeric"))
setClass("Employee", representation(boss = "Person"), contains = "Person")
I can easily check inheritance in base R:
employee <- new("Employee", name = "Jack", age = 25)
inherits(employee, "Employee")
#> [1] TRUE
inherits(employee, "Person")
#> [1] TRUE
I could not achieve the same in Rcpp:
Rcpp::cppFunction('
void check_class(Rcpp::S4 x) {
Rcout << "Class: " << as<std::string>(x.attr("class")) << std::endl;
Rcout << "x.inherits(\\"Employee\\") : " << x.inherits("Employee") << std::endl;
Rcout << "x.inherits(\\"Person\\") : " << x.inherits("Person") << std::endl;
Rcout << "Rf_inherits(x, \\"Employee\\") : " << Rf_inherits(x, "Employee") << std::endl;
Rcout << "Rf_inherits(x, \\"Person\\") : " << Rf_inherits(x, "Person") << std::endl;
Rcout << "R_extends : " << R_extends(x, Rf_mkString("Person"), R_GlobalEnv) << std::endl;
}')
check_class(employee)
#> Class: Employee
#> x.inherits("Employee") : 1
#> x.inherits("Person") : 0
#> Rf_inherits(x, "Employee") : 1
#> Rf_inherits(x, "Person") : 0
#> R_extends :
#> Error in extends(new("Employee", boss = new("Person", name = character(0), : 'class1' must be the name of a class or a class definition
I expected x.inherits("Person") to be true as well.
How can I check whether an object inherits from parent classes in Rcpp?