From eb4d9b062e44ea1962d83fc2f22a75b25192c082 Mon Sep 17 00:00:00 2001 From: ian fellows Date: Sun, 22 Apr 2018 18:46:45 -0700 Subject: [PATCH] changed way extension package exposed stat registration --- .../LologExtension/R/RcppExports.R | 4 ++ inst/examplePackage/LologExtension/R/zzz.R | 2 +- .../LologExtension/src/MinDegree.cpp | 18 +++------ .../LologExtension/src/MinDegree.h | 38 +++++++++---------- .../LologExtension/src/RcppExports.cpp | 13 +++++-- .../LologExtension/src/lolog_hello_world.cpp | 5 +-- 6 files changed, 41 insertions(+), 39 deletions(-) diff --git a/inst/examplePackage/LologExtension/R/RcppExports.R b/inst/examplePackage/LologExtension/R/RcppExports.R index 1dcea55..7c7ad5d 100644 --- a/inst/examplePackage/LologExtension/R/RcppExports.R +++ b/inst/examplePackage/LologExtension/R/RcppExports.R @@ -17,3 +17,7 @@ lolog_hello_world <- function() { .Call('_LologExtension_lolog_hello_world', PACKAGE = 'LologExtension') } +registerMinDegree <- function() { + invisible(.Call('_LologExtension_registerMinDegree', PACKAGE = 'LologExtension')) +} + diff --git a/inst/examplePackage/LologExtension/R/zzz.R b/inst/examplePackage/LologExtension/R/zzz.R index ea50b15..8873bb8 100644 --- a/inst/examplePackage/LologExtension/R/zzz.R +++ b/inst/examplePackage/LologExtension/R/zzz.R @@ -1,7 +1,7 @@ .onLoad <- function(libname, pkgname){ # Run registerMinDegree in MinDegree.cpp to allow # MinDegree to be used in formula - .Call("registerMinDegree", PACKAGE = 'LologExtension') + registerMinDegree() } # #You can now use the new MinDegree term e.g.: diff --git a/inst/examplePackage/LologExtension/src/MinDegree.cpp b/inst/examplePackage/LologExtension/src/MinDegree.cpp index 6bafa4f..c207a7c 100644 --- a/inst/examplePackage/LologExtension/src/MinDegree.cpp +++ b/inst/examplePackage/LologExtension/src/MinDegree.cpp @@ -1,18 +1,10 @@ // [[Rcpp::depends(lolog)]] #include "MinDegree.h" -#include "lolog.h" +#include -/** -* This function registers the new MinDegree statistic so that -* it can be used in lolog formula. -* -* RcppExport means this function can be called from R using -* .C("registerMinDegree") -* see: .onLoad in zzz.R -*/ -RcppExport void registerMinDegree(){ - using namespace lolog; - Rcpp::XPtr< AbstractStat > ps1(new UndirectedMinDegree()); - REGISTER_UNDIRECTED_STATISTIC(ps1); +//[[Rcpp::export()]] +void registerMinDegree(){ + Rcpp::XPtr< lolog::AbstractStat > ps1(new lologext::UndirectedMinDegree()); + REGISTER_UNDIRECTED_STATISTIC(ps1); } diff --git a/inst/examplePackage/LologExtension/src/MinDegree.h b/inst/examplePackage/LologExtension/src/MinDegree.h index c80ac12..f16dd70 100644 --- a/inst/examplePackage/LologExtension/src/MinDegree.h +++ b/inst/examplePackage/LologExtension/src/MinDegree.h @@ -3,11 +3,12 @@ #include -#include -#include -#include - -using namespace lolog; +#include +#include +#include + +namespace lologext{ + using namespace Rcpp; using namespace std; @@ -17,7 +18,7 @@ using namespace std; * with degree greater than or equal to "degree" */ template -class MinDegree : public BaseStat< Engine > { +class MinDegree : public lolog::BaseStat< Engine > { public: int degree; //the minimum degree @@ -42,7 +43,7 @@ class MinDegree : public BaseStat< Engine > { } //Calculate the statistic - virtual void calculate(const BinaryNet& net){ + virtual void calculate(const lolog::BinaryNet& net){ vector v(1,0); this->stats=v; this->lastStats = std::vector(1,0.0); @@ -54,8 +55,8 @@ class MinDegree : public BaseStat< Engine > { } //Update the statistic given a dyad toggle - virtual void dyadUpdate(const BinaryNet& net,const int &from,const int &to,const std::vector &order,const int &actorIndex){ - BaseOffset::resetLastStats(); + virtual void dyadUpdate(const lolog::BinaryNet& net,const int &from,const int &to,const std::vector &order,const int &actorIndex){ + lolog::BaseOffset::resetLastStats(); if(!net.hasEdge(from,to)){ if(net.degree(from)==degree-1) this->stats[0]++; @@ -81,16 +82,15 @@ class MinDegree : public BaseStat< Engine > { }; -typedef Stat > UndirectedMinDegree; +typedef lolog::Stat > UndirectedMinDegree; -/** -* This function registers the new MinDegree statistic so that -* it can be used in lolog formula. -* -* RcppExport means this function can be called from R using -* .C("registerMinDegree") -* see: .onLoad in zzz.R -*/ -RcppExport void registerMinDegree(); + + + +} + + + +void registerMinDegree(); #endif diff --git a/inst/examplePackage/LologExtension/src/RcppExports.cpp b/inst/examplePackage/LologExtension/src/RcppExports.cpp index 67bcde1..5cf2cff 100644 --- a/inst/examplePackage/LologExtension/src/RcppExports.cpp +++ b/inst/examplePackage/LologExtension/src/RcppExports.cpp @@ -15,12 +15,19 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } - -RcppExport SEXP registerMinDegree(); +// registerMinDegree +void registerMinDegree(); +RcppExport SEXP _LologExtension_registerMinDegree() { +BEGIN_RCPP + Rcpp::RNGScope rcpp_rngScope_gen; + registerMinDegree(); + return R_NilValue; +END_RCPP +} static const R_CallMethodDef CallEntries[] = { {"_LologExtension_lolog_hello_world", (DL_FUNC) &_LologExtension_lolog_hello_world, 0}, - {"registerMinDegree", (DL_FUNC) ®isterMinDegree, 0}, + {"_LologExtension_registerMinDegree", (DL_FUNC) &_LologExtension_registerMinDegree, 0}, {NULL, NULL, 0} }; diff --git a/inst/examplePackage/LologExtension/src/lolog_hello_world.cpp b/inst/examplePackage/LologExtension/src/lolog_hello_world.cpp index 0efa351..aa71ecd 100644 --- a/inst/examplePackage/LologExtension/src/lolog_hello_world.cpp +++ b/inst/examplePackage/LologExtension/src/lolog_hello_world.cpp @@ -1,6 +1,6 @@ // [[Rcpp::depends(lolog)]] #include "lolog_hello_world.h" -#include "lolog.h" +#include //' An basic example of a function in C++ using lolog //' @return a list of a character vector, a numeric vector, and an lolog DirectedNet @@ -17,10 +17,9 @@ // [[Rcpp::export]] Rcpp::List lolog_hello_world(){ using namespace Rcpp; - using namespace lolog; IntegerMatrix tmp(0,2); - DirectedNet net(tmp,20); + lolog::DirectedNet net(tmp,20); CharacterVector x = CharacterVector::create( "foo", "bar" ) ; NumericVector y = NumericVector::create( 0.0, 1.0 ) ; List z = List::create( x, y, wrap(net) ) ;