Main Page | SQL Examples | Scripting Examples | Events | Log in / create account Article Discussion Edit History Go to the site toolbox

Manifold/R Integration

From Manipedia

R can be tightly integrated into a workflow with Manifold in a variety of ways. The simplest involve methods of transferring data between the two environments.

The most useful application for this is when you have some existing R code that you know will run, and you want it to work from within Manifold. More interactive connections are trickier as they rely on both knowledge of R and advanced scripting in Manifold.

If you have no knowledge of R or why it is useful for statistics (amongst other things), here is a simple description of linear models via formula.

If the variable "d" in R is a data.frame with columns "x" and "y" (analogous to a table in Manifold) then the following formula expression provides a basic regression of x vs. y.

reg <- lm(y ~ x, data = d)

Formulas can specify any linear model you want, and on a similar foundation you'll find practically any statistical formulation in R somewhere.


See http://www.r-project.org/ for full details on R.

Contents

[edit] Transfer table data from Manifold into R

There are four main methods for reading data into R that originated in Manifold.

  1. External text file exported from Manifold:
    d <- read.csv("C:/temp/file.csv")
  2. Copy and paste via the clipboard:
    d <- read.delim("clipboard") will read data from a table/selection copied in Manifold.
  3. Directly from a .map file using ODBC
    Using the function defined here, and the RODBC package
    1. open a connection to the file
      library(RODBC)
      ch <- odbcConnectManifold("C:/temp/world.map")
    2. pass a Manifold query to the connection
      d <- sqlQuery(ch, "SELECT [Longitude (I)] AS lon, [Latitude (I)] AS lat, [Capital] AS name FROM [Countries] WHERE [Area (I)] > 150;")
  4. Directly from a COM connection (advanced)
    See this thread for more details: http://forum.manifold.net/forum/t59964.13

[edit] Transfer Shape File (exported from Manifold) into R

There are several packages which allow you to import Shape Files into R:

  1. Using the rgdal package, needs to be installed by going to the relevant CRAN Archive:

>library(rgdal)
>dataset <- readOGR(".","ShapeFileName")

  1. Using the maptools package. Need to specify geometry type at import:

>library(maptools)
>dataset <- readShapePoints("ShapFileName.shp")

[edit] Further mechanisms

The R package rgdal (very easily installed with install.packages("rgdal") ) can read directly from shapefiles, GeoTIFFs, and a large variety of spatial formats. See GDAL raster formats and GDAL vector formats for details]

TODO: The ODBC driver in GDAL can also read directly from .map files, but up until recently some bugs on both sides made this problematic.

It is not clear what steps to take first for more tight coupling of R and Manifold: there is a lot that can be done via customizations within Manifold (and R) for the particular uses needed. See this post and the thread it is part of for more discussion, and get your suggestions in!

R/Scilab (D)COM Server provides a COM-Interface to R as well as various COM objects and Active X controls for your applications. Additionally, an Add-In for Microsoft Excel is provided to easily use R in Excel and create statistical applications with Excel as the main GUI. The main features of this package are:

  • COM server for local and remote use of R and Scilab
  • transfer of data into/from R, including NA, NaN,...
  • Active X Controls for text and graphics output
  • Repository for R instances for shared and exclusive access
  • Excel Add-In (available for separate download from http://rcom.univie.ac.at/)

If you have installed the package, you can then simply write a script as follows:

//Initialise R(D)Com Oject
var R = new ActiveXObject("StatConnectorSrv.StatConnector");
 
//Start R Interface
R.Init("R");
 
//Evaluate Command Set
var a = R.Evaluate("2+2");
Application.Messagebox(a);

[edit] User forum threads of interest

#07-Dec-07 17:30

#24-Apr-07 02:02

[edit] Interesting external resources

Site Toolbox:

Personal tools
This page was last modified 18:51, 26 February 2009. - This page has been accessed 2,370 times. - Disclaimers - About Manipedia