knitr::opts_chunk$set( collapse = TRUE, comment = "# ", eval = F )
library(Rcpp) library(sfheaders) # library(sf)
There are two ways you can use the C++ code
Rcpp::cppFunction()
sfheaders
in a packageThe first option works, but is a bit pointless because if you're in R anyway you would probably just call the R functions.
The second option is the whole reason I build this library!
The reason all the C++ code is in header files in inst/include
is so you can call this code from another library. To do so
LinkingTo
For example, here's the Description file of the gpx
package I'm working on.
sfheaders
header files you need in a .cpp fileThese screenshots are taken from my gpx library. Feel free to have a look and see how I use them.
Here are some example cppFunction()
calls you can use to make various sf objects
cppFunction( includes = '#include "sfheaders/sfg/sfg.hpp"' , code = ' SEXP a_point( SEXP x ) { return sfheaders::sfg::sfg_point( x ); } ' , plugins = "cpp11" , depends = c("geometries", "sfheaders") ) ## vector a_point( c(1,3) ) ## single-row matrix a_point( matrix(c(1,2), ncol = 2) ) ## single-row data.frame a_point( data.frame(x = 1, y = 2) )
cppFunction( includes = ' #include "sfheaders/sfc/multipoint/sfc_multipoint.hpp" ', code = ' SEXP a_multipoint( SEXP x ) { return sfheaders::sfc::sfc_multipoint( x ); } ' , plugins = "cpp11" , depends = c("geometries", "sfheaders") ) x <- 1:2 a_multipoint( x ) x <- matrix(c(1,2,3,4,5,6), ncol = 2) a_multipoint( x ) x <- data.frame(x = 1:3, y = 4:6) a_multipoint( x )
cppFunction( includes = ' #include "sfheaders/sf/linestring/sf_linestring.hpp" ', code = ' SEXP a_linestring( SEXP x, SEXP geometry_columns, SEXP id_column ) { return sfheaders::sf::sf_linestring( x, geometry_columns, id_column ); } ', plugins = "cpp11", depends = c("geometries", "sfheaders") ) x <- 1:2 a_linestring( x, NULL, NULL ) x <- matrix(c(1,2,3,4,5,6), ncol = 2) a_linestring( x, NULL, NULL ) x <- data.frame(x = 1:6, y = 4:9, id = c(rep(1,3), rep(2,3))) a_linestring( x, NULL, "id" ) x <- data.frame(x = 1:6, y = 4:9, z = 8:10, id = c(rep(1,3), rep(2,3))) a_linestring( x, c("x","y"), "id" ) a_linestring( x, c("x","y", "z"), "id" )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.