Skip to content

Commit 766474e

Browse files
committed
Put serialization into separate file
1 parent 282ffc5 commit 766474e

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

serialization.Rmd

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
output:
3+
md_document:
4+
variant: markdown_github
5+
---
6+
```{r setup, include = FALSE}
7+
knitr::opts_chunk$set(echo = TRUE)
8+
options(digits = 4)
9+
```
10+
11+
# Setup on OSX
12+
13+
```bash
14+
## To install Homebrew:
15+
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
16+
## Then:
17+
brew tap homebrew/versions && brew tap homebrew/science && brew update
18+
# brew install gcc --enable-cxx && brew link --overwrite gcc && brew link cmake
19+
brew install boost --c++11
20+
# Installing cmake may require: sudo chown -R `whoami` /usr/local/share/man/man7
21+
brew install mlpack shark dlib
22+
```
23+
24+
```R
25+
install.packages(c(
26+
"BH", # Header files for 'Boost' C++ library
27+
"Rcpp", # R and C++ integration
28+
"RcppArmadillo", # Rcpp integration for 'Armadillo' linear algebra library
29+
"Rcereal", # header files of 'cereal', a C++11 library for serialization
30+
"microbenchmark", # For benchmarking performance
31+
"devtools", # For installing packages from GitHub
32+
"magrittr", # For piping
33+
"knitr" # For printing tables & data.frames as Markdown
34+
), repos = "https://cran.rstudio.com/")
35+
devtools::install_github("yihui/printr") # Prettier table printing
36+
```
37+
38+
If you get "ld: library not found for -lgfortran" error when trying to install RcppArmadillo, run:
39+
40+
```bash
41+
curl -O http://r.research.att.com/libs/gfortran-4.8.2-darwin13.tar.bz2
42+
sudo tar fvxz gfortran-4.8.2-darwin13.tar.bz2 -C /
43+
```
44+
45+
See "[Rcpp, RcppArmadillo and OS X Mavericks "-lgfortran" and "-lquadmath" error](http://thecoatlessprofessor.com/programming/rcpp-rcpparmadillo-and-os-x-mavericks-lgfortran-and-lquadmath-error/)"" for more info.
46+
47+
# Rcpp
48+
49+
See [this section](http://rmarkdown.rstudio.com/authoring_knitr_engines.html#rcpp) in [RMarkdown documentation](http://rmarkdown.rstudio.com/) for details on Rcpp chunks.
50+
51+
```{r load_pkg}
52+
library(magrittr)
53+
library(Rcpp)
54+
library(RcppArmadillo)
55+
library(Rcereal)
56+
```
57+
58+
# Serialization via Boost
59+
60+
Taking the GPS example from Boost documentation's [tutorial on serialization](http://www.boost.org/doc/libs/1_62_0/libs/serialization/doc/tutorial.html), using some of the code from [Rcereal's README](https://github.com/wush978/Rcereal/blob/master/README.md#getting-started).
61+
62+
```{Rcpp boost}
63+
// [[Rcpp::plugins(cpp11)]]
64+
// [[Rcpp::depends(BH)]]
65+
#include <Rcpp.h>
66+
using namespace Rcpp;
67+
#include <boost/archive/archive_exception.hpp>
68+
#include <boost/archive/binary_oarchive.hpp>
69+
70+
class gps_position
71+
{
72+
public:
73+
int degrees;
74+
int minutes;
75+
float seconds;
76+
gps_position(){};
77+
gps_position(int d, int m, float s) :
78+
degrees(d), minutes(m), seconds(s)
79+
{}
80+
};
81+
82+
namespace boost {
83+
namespace serialization {
84+
85+
template<class Archive>
86+
void serialize(Archive & ar, gps_position & g, const unsigned int version)
87+
{
88+
ar & g.degrees;
89+
ar & g.minutes;
90+
ar & g.seconds;
91+
}
92+
93+
} // namespace serialization
94+
} // namespace boost
95+
96+
// [[Rcpp::export]]
97+
RawVector serializeGPS(int d, int m, float s) {
98+
gps_position g(d, m, s);
99+
std::stringstream ss;
100+
{
101+
boost::archive::binary_oarchive oa(ss);
102+
oa << g;
103+
}
104+
ss.seekg(0, ss.end);
105+
RawVector retval(ss.tellg());
106+
ss.seekg(0, ss.beg);
107+
ss.read(reinterpret_cast<char*>(&retval[0]), retval.size());
108+
return retval;
109+
}
110+
```
111+
112+
```{r}
113+
serializeGPS(35L, 52L, 0.3)
114+
```

0 commit comments

Comments
 (0)