Skip to content
Pedro R. Andrade edited this page Aug 2, 2017 · 15 revisions

Integration with R

Pedro R. Andrade, Pedro O. Costa

This tutorial was tested with TerraME 2.0.0-RC5 and R 3.4.0.

This tutorial is under development. It uses code that will work properly only in the next release of TerraME.

Summary

Introduction

It is possible to execute R functionalities within TerraME. Basic functionalities can be accessed using TerraME functions, but it is possible to execute R commands directly. This tutorial describes how to use R within TerraME.

Installing R and Rserve

The steps described in this session need to be execute only to create the environment. If you have already executed it (or when you will connect to a server in another machine), it is not necessary to execute its steps. First download R and install it. Then, run R and install Rserve by executing the following command:

install.packages("Rserve")

If you do not have permission to install a package on R installation directory, it will ask if you want to use a personal library:

Answer Yes. It will then suggest a location for it:

Answer Yes as well. After that, R might ask for a CRAN mirror, as shown below. Choose your country (or a closer one) or 0-Cloud and then press OK.

After selecting the mirror, Rserve will be installed and the output will be similar to the figure below:

Running Rserve

To Run Rserve, first open R. Then execute in the following commands:

require(Rserve)
Rserve()

The output will be similar to the figure below:

Now R is working as a server. Let it open and switch to TerraME.

Basic statistics

To execute a statistical analysis, first it is necessary to install package rstats from the graphical interface. Then it is necessary to load the package.

import("rstats")

To start a connection with R, just create an instance of Rserve. This object is usually called R, but you can choose another name.

R = Rserve{}

Let us now read some data from a shapefile to execute a statistical analysis. The code below declares amazonia, a CellularSpace from amazonia.shp within base package.

Read amazonia shape and do some statistical analysis printing the output. Show the error in TerraME when Serve is not running.

amazonia = CellularSpace{
    file = filePath("amazonia.shp"),
}

Using amazonia, let us execute a linear regression using attribute prodes_10 as response and distroads, protected, and distports as terms.

result = R:lm{
    data = amazonia,
    response = "prodes_10",
    terms = {"distroads", "protected", "distports"}
}

print(result)

The following result will be shown:

{
    22.433773199025,      -- intercept
    -8.7823791788271e-05, -- distroads
    -11.950298484846,     -- protected
    -4.7008135934568e-06  -- distports
}

If it cannot connect to Rserve, an error such as the one below will be shown:

Error: attempt to read past end of buffer
Stack traceback:
    File 'lm.lua', line 15, in call to function 'lm'

Executing R commands

If needed, it is possible to execute R commands directly. To execute a statistical analysis over some data, it is necessary to first send the data to R. The code below declares a variable amaz in R that has the content of amazonia variable in TerraME.

R = Rserve{}

amazonia = CellularSpace{
    file = filePath("amazonia.shp"),
}

R.amaz = amazonia

After that, it is possible to call R functions using Rserve connection. The function can be called directly from R object. For example, the following code computes the sum of column protected.

print(R:sum("amaz[, 'protected']"))

The code above is just a syntax sugar for the following command:

print(R:evaluate("sum(amaz[, 'protected'])"))