[Rprotobuf-commits] r331 - pkg/inst/examples/HighFrequencyFinance

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jul 17 20:01:32 CEST 2010


Author: edd
Date: 2010-07-17 20:01:31 +0200 (Sat, 17 Jul 2010)
New Revision: 331

Added:
   pkg/inst/examples/HighFrequencyFinance/protoModule.cpp
Modified:
   pkg/inst/examples/HighFrequencyFinance/Makefile
   pkg/inst/examples/HighFrequencyFinance/loadInR.r
Log:
rudimentary Modules support


Modified: pkg/inst/examples/HighFrequencyFinance/Makefile
===================================================================
--- pkg/inst/examples/HighFrequencyFinance/Makefile	2010-07-17 17:46:06 UTC (rev 330)
+++ pkg/inst/examples/HighFrequencyFinance/Makefile	2010-07-17 18:01:31 UTC (rev 331)
@@ -18,5 +18,9 @@
 			PKG_CXXFLAGS="$(CPPFLAGS) $(RCPPINC)" PKG_LIBS="$(LDLIBS) $(RCPPLIB)" \
 				R CMD SHLIB protoLoadForR.cpp TradeData.pb.cc
 
+protoModule.so:		protoModule.cpp TradeData.pb.cc
+			PKG_CXXFLAGS="$(CPPFLAGS) $(RCPPINC)" PKG_LIBS="$(LDLIBS) $(RCPPLIB)" \
+				R CMD SHLIB protoModule.cpp TradeData.pb.cc
+
 TradeData.pb.cc:	TradeData.proto
 			protoc --cpp_out=. TradeData.proto
\ No newline at end of file

Modified: pkg/inst/examples/HighFrequencyFinance/loadInR.r
===================================================================
--- pkg/inst/examples/HighFrequencyFinance/loadInR.r	2010-07-17 17:46:06 UTC (rev 330)
+++ pkg/inst/examples/HighFrequencyFinance/loadInR.r	2010-07-17 18:01:31 UTC (rev 331)
@@ -71,20 +71,19 @@
     suppressMessages(library(Rcpp))
 
     stopifnot(file.exists(file))
-    dyn.load("trade.mod.so")
+    dll <- dyn.load("protoModule.so")
 
-    yada <- new("Module")
-    NAMESPACE <- environment()
-    unlockBinding("yada", NAMESPACE)
-    assign("yada", Module("yada"), NAMESPACE)
-    ##yada <- Module("yada", PACKAGE=NULL)
-    ##print( yada$nbfills("trades.pb") )
+    yada <- Module("yada", dll)
+    yada$init("trades.pb")
+    print( yada$nbfills() )
     invisible(NULL)
 }
 
 suppressMessages(library(RProtoBuf))
 suppressMessages(library(rbenchmark))
 
+moduled()
+q()
 dyn.load("protoLoadForR.so")
 
 print(benchmark(basicRuse = basicUse(FALSE),

Added: pkg/inst/examples/HighFrequencyFinance/protoModule.cpp
===================================================================
--- pkg/inst/examples/HighFrequencyFinance/protoModule.cpp	                        (rev 0)
+++ pkg/inst/examples/HighFrequencyFinance/protoModule.cpp	2010-07-17 18:01:31 UTC (rev 331)
@@ -0,0 +1,37 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+
+#include "TradeData.pb.h"
+
+#include <Rcpp.h>
+#include <fstream>
+
+static TradeData::Trades tr;
+
+int init(const char *pbfile) {
+	std::fstream fs(pbfile, std::ios::in | std::ios::binary);
+
+	if (!tr.ParseFromIstream(&fs)) {
+		std::cerr << "Trouble parsing..." << std::cout;
+		return -1;
+	}
+	return 0;
+}
+	
+int nbfills(void) {
+	int n = tr.fill_size();
+	return n;
+}
+
+RCPP_MODULE(yada){
+	using namespace Rcpp ;
+	                  
+	class_<TradeData::Trades>( "Trades" )
+		.method( "fill_size", &TradeData::Trades::fill_size )
+		;
+	
+	function( "init",    &init);
+	function( "nbfills", &nbfills);
+
+	// Hmpf. So how do I return the data.frame via Modules?
+}                     
+



More information about the Rprotobuf-commits mailing list