Skip to content

Commit

Permalink
fixed r name bug
Browse files Browse the repository at this point in the history
  • Loading branch information
msupernaw committed Apr 9, 2024
1 parent c7aed67 commit 0116af1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
4 changes: 3 additions & 1 deletion inst/include/interface/rcpp/rcpp_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ RCPP_MODULE(growth) {
.field("a_min", &vonBertalanffyInterface::a_min)
.field("alpha", &vonBertalanffyInterface::alpha)
.field("beta", &vonBertalanffyInterface::beta)
.method("get_id", &vonBertalanffyInterface::get_id);
.field("id", &vonBertalanffyInterface::id)
.method("get_id", &vonBertalanffyInterface::get_id)
.method("show", &vonBertalanffyInterface::show);;
Rcpp::function("get_parameter_vector", get_parameter_vector);
Rcpp::function("clear", clear);
Rcpp::function("CreateModel", CreateModel);
Expand Down
45 changes: 35 additions & 10 deletions inst/include/interface/rcpp/rcpp_objects/rcpp_growth.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class GrowthInterfaceBase : public RcppInterfaceBase {

GrowthInterfaceBase() {
this->id = GrowthInterfaceBase::id_g++;
this->module_id = this->id;
GrowthInterfaceBase::growth_objects[this->id] = this;
RcppInterfaceBase::interface_objects.push_back(this);
}
Expand All @@ -48,6 +49,7 @@ class vonBertalanffyInterface : public GrowthInterfaceBase {
Variable a_min;
Variable alpha;
Variable beta;
uint32_t id;

vonBertalanffyInterface() : GrowthInterfaceBase() {
}
Expand All @@ -60,7 +62,7 @@ class vonBertalanffyInterface : public GrowthInterfaceBase {
}

virtual std::string get_module_name() {
return "vonBertalanffyInterface";
return "vonBertalanffy";
}

template<typename Type>
Expand Down Expand Up @@ -166,6 +168,27 @@ class vonBertalanffyInterface : public GrowthInterfaceBase {
#endif
return true;

}
/**
* Print model values.
*/
void show()const{

Rcout << "vonBertalanffy\n";
Rcout << "id:" << this->module_id << "\n";
// Rcout << "function value: " << this->objective_function_value << "\n";
//
// Rcout << std::setw(15) << "observed " << std::setw(15) << "predicted\n";
// for (int i = 0; i < this->predicted.size(); i++) {
// Rcout << std::setw(15) << this->obs[i] << std::setw(15) << this->predicted[i] << "\n";
// }
// Rcout << "k = " << exp(this->log_k_mean.value) << "\n";
// Rcout << "a_min = " << this->a_min.value << "\n";
// Rcout << "l_inf = " << exp(this->log_l_inf_mean.value) << "\n";
// Rcout << std::setw(15) << "log_k " << std::setw(15) << "log_l_inf\n";
// for (int i = 0; i < this->log_k.size(); i++) {
// Rcout << std::setw(15) << Rcpp::as<Variable>(this->log_k[i]).value << std::setw(15) << Rcpp::as<Variable>(this->log_l_inf[i]).value << "\n";
// }
}

/**
Expand All @@ -191,16 +214,18 @@ class vonBertalanffyInterface : public GrowthInterfaceBase {

}

/**
* Print model values.
*/
void show_() {
Rcpp::Rcout << "vonBertalanffy:\n";
Rcpp::Rcout << "k = " << this->k.value << "\n";
Rcpp::Rcout << "a_min = " << this->a_min.value << "\n";
Rcpp::Rcout << "l_inf = " << this->l_inf.value << "\n";
}
// /**
// * Print model values.
// */
// void show_() {
// Rcpp::Rcout << "vonBertalanffy:\n";
// Rcpp::Rcout << "k = " << this->k.value << "\n";
// Rcpp::Rcout << "a_min = " << this->a_min.value << "\n";
// Rcpp::Rcout << "l_inf = " << this->l_inf.value << "\n";
// }

};



#endif
18 changes: 18 additions & 0 deletions inst/include/interface/rcpp/rcpp_objects/rcpp_interface_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,20 @@ class RcppInterfaceBase {
T result;
return (ss >> result) ? result : 0;
}

virtual void show_() {}

bool StartsWith(const std::string &value1, const std::string &value2) {
return value1.find(value2) == 0;
}

bool contains(std::string s1, std::string s2) {
if (s1.find(s2) != std::string::npos) {
return true;
}
return false;
}

void set_r_name() {
Rcpp::Environment env = Rcpp::Environment::global_env();
Rcpp::List l = Rcpp::as<Rcpp::List>(env.ls(true));
Expand All @@ -97,6 +106,9 @@ class RcppInterfaceBase {
if (StartsWith(Rcpp::as<std::string>(l[i]), ".")) {
continue;
}

std::cout << Rcpp::as<std::string>(l[i]) << "*******" << std::endl;

SEXP expression, result;
ParseStatus status;

Expand All @@ -112,11 +124,17 @@ class RcppInterfaceBase {
if (TYPEOF(result) == STRSXP) {
for (int j = 0; j < LENGTH(result); j++) {
std::string str(CHAR(STRING_ELT(result, j)));
std::cout << str << "\n\n\n";
std::cout<<"---->"<<this->get_module_name()<<std::endl;

std::cout<< "contains "<<contains(str, this->get_module_name())<<std::endl;
if (str == this->get_module_name()) {

std::string line(CHAR(STRING_ELT(result, j + 1)));
std::vector<std::string> tokens;
std::cout<<"line = "<<line<<std::endl;
Tokenize(line, tokens, ":");
std::cout<<"module id "<<this->module_id<<" == " <<StringToNumber<size_t> (tokens[1])<<std::endl;
if (StringToNumber<size_t> (tokens[1]) == this->module_id) {
this->r_name = Rcpp::as<std::string>(l[i]);
}
Expand Down

0 comments on commit 0116af1

Please sign in to comment.