1

Good evening.

GOAL: I am attempting to build a batch geocoding package based on the New York City Department of City Planning's Geosupport software using RCPP from within RStudio on a machine running Windows. Geosupport returns a lot of useful information besides coordinates including building identification number and census geographies. I think such a package has the potential to be very useful to researchers and community advocates working with NYC data.

BACKGROUND: Geosupport is available as a free download on the NYC DCP website. The download comes with a interface for batch geocoding (known as GBAT). In addition, header, data and library files are provided so users can geocode from an application built using C, C++, or VB. The library files have a DLL extension and were compiled in C (not C++, I checked with one of the developers).

STATUS: Thus far, I have been able to include the header files and set up work areas. I run into problems when I attempt to use the functions from the C libraries. I have been reading Writing R Extensions - Using Makevars but I am still uncertain about how to proceed. I built my package using RStudio's Rtools with RCPP and a makevars file was not generated. I purchased Dirk's book (which is referenced in postings similar to mine) but it has not arrived, yet.

Thank you!

Gretchen

UPDATED CODE... 05.03.2016 at 19:45 EST: Per Coatless's suggestion, I created a GitHub repository. I also created a Makevars.win and Makevars files and relocated my header files to inst/include. The headers work OK but I still do not know what to do with the libraries. Also, the underlying data files that drive the geocoder are too large for GitHub (1.85 GB). I will try to add them using Git LFS from my home computer.

gmculp
  • 135
  • 1
  • 10

2 Answers2

5

No book? No problem!

First, try to understand the package structure by creating a package skeleton via Rcpp.package.skeleton() or use RStudio's Create an Rcpp Package bit.

For everything else, there are lots of examples in the vignettes and online in Rcpp's gallery.

First off the bat, the main reason for the difficult is the use of:

#include "../Include/NYCgeo.h"

This is not a good style as it goes against the file structure typical of R packages.

When trying to use library headers, one should opt for a package structure of either:

R/
src/
 |- Makevars
 |- Makevars.win
 |- header.h
 |- action.cpp
man/
DESCRIPTION
NAMESPACE

Under this approach, your header files are restricted solely to the package. To enable a LinkingTo: approach within the DESCRIPTION file and generally better inclusions, the structure you should aim for is:

R/
inst/
 |- include/
    |- header.h
src/
 |- Makevars
 |- Makevars.win
 |- action.cpp
man/
DESCRIPTION
NAMESPACE

Thus, in the action.cpp file you can just use:

#include <header.h> 

vs.

#include "header.h"

Now, with that being said, contents for the Makevars and Makevars.win files in /src when including headers in the /inst/include should be:

PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
PKG_CPPFLAGS =  -I../inst/include/

The second line is the most important.

For a very simplistic example of file inclusions, see sitmo {disclaimer: I wrote the R package}.

For a more intense and interesting version, see dplyr.

If you code toss the complete code onto GitHub, more help could be provided.

coatless
  • 20,011
  • 13
  • 69
  • 84
  • Very nice, very patient. – Dirk Eddelbuettel Apr 29 '16 at 01:53
  • This is excellent, Coatless! Thank you for taking the time to craft such a thorough and accessible explanation. I will post the code to my GitHub account. – gmculp May 03 '16 at 14:19
  • I just updated my question to include a link to my [GitHub repository](https://github.com/gmculp/RGBAT) for this project. I was able to get the headers to work using Coatless's response. I am still confused about how I can adjust the Makevars/Makevars.win files to reflect the location of the dll files (see [Bin folder](https://github.com/gmculp/RGBAT/tree/master/Bin)). – gmculp May 03 '16 at 23:52
1

I was able to access the functions within the C library MyLibrary.dll using Coatless' response.

Here is my directory structure:

R/
inst/
  |- include/
  |- header.h
src/
  |- Makevars
  |- Makevars.win
  |- action.cpp
 bin/
  |- MyLibrary.dll
 man/
 DESCRIPTION
 NAMESPACE

Here is my Makevars/Makevars.win file:

PKG_LIBS = -L../bin -lMyLibrary
PKG_CPPFLAGS =  -I../inst/include/
gmculp
  • 135
  • 1
  • 10