[GRASS-stats] Using Rcpp to interface GRASS from R and vice versa

Vaclav Petras wenzeslaus at gmail.com
Wed Jul 18 06:45:17 PDT 2012

Hello grass-stats,

I've tried the R packages Rcpp [1] and inline [2] to write a function
which returns values from a GRASS raster map using GRASS C functions.
In attached demo function returns only one value but more complex
objects can be returned too. There is also an option to pass C++
classes and R data frames.

In general Rcpp (+inline) can be used to write efficient C++ code and
use it in R. In case of GRASS and R it can be used to access large
raster datasets from R without the need to load everything to memory.

There is also another possibility. One can use Rcpp with RInside
package [3] and write C/C++ code which calls R internally. So GRASS
module which does some specific computations in R but handles memory
itself can be created. Note that RInside is R instance within C++
program, it can run any R code.

I'm sending a very simplified example how things are working. For
running it, you need latest R, Rcpp package and inline package [4].
Tested only on GNU/Linux (Ubuntu) but Rcpp should work even on MS

I'm interested (as an programmer) in this topic and I would like to
write something which uses these possibilities. I'm not experienced
with R so I'm not sure which way to go. So there are several

* Would it be useful to have something like R raster package [5] but
with GRASS raster maps as data format (direct access to GRASS maps
from R within GRASS session)?

* Should some GRASS modules or even wxGUI provide some R functionality?

* Not sure, but I think that something like R-based raster calculator
can be also created. Would be this helpful?

* Why are Rcpp and inline not used in spgrass6 [6]? Is there some
other reason than Rcpp is relatively new so it may be considered as

* Would it be more useful to bring some functionality from GRASS to R
or in the opposite way, trying to use Rcpp within spgrass6 (or
spgrass7) or just to have some general tools or just to create a guide
how to combine things?

Comments and suggestions are welcome.

Vaclav Petras

[1] http://cran.r-project.org/web/packages/Rcpp/
[2] http://cran.r-project.org/web/packages/inline/
[3] http://cran.r-project.org/web/packages/RInside/
[4] http://cran.r-project.org/bin/
[5] http://cran.r-project.org/web/packages/raster/
[6] http://cran.r-project.org/web/packages/spgrass6/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_rgrass.R
Type: application/octet-stream
Size: 1353 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/grass-stats/attachments/20120718/7159034d/attachment.obj>

More information about the grass-stats mailing list