<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://grasswiki.osgeo.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=%E2%9A%A0%EF%B8%8FDalby</id>
	<title>GRASS-Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://grasswiki.osgeo.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=%E2%9A%A0%EF%B8%8FDalby"/>
	<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/wiki/Special:Contributions/%E2%9A%A0%EF%B8%8FDalby"/>
	<updated>2026-04-18T13:31:40Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=R_statistics&amp;diff=16425</id>
		<title>R statistics</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=R_statistics&amp;diff=16425"/>
		<updated>2012-09-07T13:41:03Z</updated>

		<summary type="html">&lt;p&gt;⚠️Dalby: /* Mac OSX */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Q:''' How do I enjoy high quality statistic analysis in GRASS?&lt;br /&gt;
&lt;br /&gt;
'''A:''' Well, GRASS has got an interface to the most powerful statistics analysis package around: '''''R''''' (http://www.r-project.org)&lt;br /&gt;
: The [http://cran.r-project.org/web/packages/spgrass6/ '''spgrass6'''] ''R'' addon package provides the R &amp;amp;larr;&amp;amp;rarr; GRASS interface.&lt;br /&gt;
&lt;br /&gt;
Using R in GRASS GIS directly has two meanings:&lt;br /&gt;
&lt;br /&gt;
* The '''first''' is that R is run &amp;quot;on top of&amp;quot; GRASS, transferring GRASS data to R to run statistical functions on the imported data as R objects in memory, and possibly transfer the results back to GRASS.&lt;br /&gt;
* The '''second''' is to leave the data mostly in GRASS, and to use R as a scripting language &amp;quot;on top of&amp;quot; GRASS with execGRASS() - in this case, little data is moved to R, so memory constraints are not important, but R functionality is available.&lt;br /&gt;
&lt;br /&gt;
=== Quick start ===&lt;br /&gt;
&lt;br /&gt;
For the impatient just start it:&lt;br /&gt;
&lt;br /&gt;
  &amp;gt; R&lt;br /&gt;
  &lt;br /&gt;
  #and install packages directly from the net&lt;br /&gt;
  pkgs &amp;lt;- c('akima', 'spgrass6', 'RODBC', 'VR', 'gstat')&lt;br /&gt;
  &lt;br /&gt;
  install.packages(pkgs, dependencies=TRUE, type='source') &lt;br /&gt;
&lt;br /&gt;
or to get all packages for spatial analysis in one:&lt;br /&gt;
&lt;br /&gt;
  &amp;gt; R&lt;br /&gt;
&lt;br /&gt;
  #To automatically install the spatial task view, the ctv package needs to be installed, e.g., via&lt;br /&gt;
  install.packages(&amp;quot;ctv&amp;quot;)&lt;br /&gt;
  library(&amp;quot;ctv&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
  install.views(&amp;quot;Spatial&amp;quot;)&lt;br /&gt;
  #or&lt;br /&gt;
  update.views(&amp;quot;Spatial&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have '''''R''''' and '''''spgrass6''''' on your system, have a look at this tutorial:&lt;br /&gt;
: http://grass.osgeo.org/statsgrass/grass_geostats.html&lt;br /&gt;
&lt;br /&gt;
=== Installation ===&lt;br /&gt;
&lt;br /&gt;
First of all you need to install '''''R''''' onto your system.&lt;br /&gt;
&lt;br /&gt;
R and many of its addon packages are pre-built and distributed through the CRAN network of mirrors. In addition many Linux distributions prepackage R and a number of the most popular addon toolboxes.&lt;br /&gt;
&lt;br /&gt;
All the necessary functions for the GRASS 6 interface are now in packages on CRAN, so that on Linux/Unix (or Mac OSX) installing '''rgdal''' from source with PROJ4 and GDAL installed, or Windows installing from binary, the required packages are: '''sp'''; '''maptools''' (now includes spmaptools); '''rgdal''' (now includes spGDAL, spproj); '''spgrass6''' - now all on CRAN.&lt;br /&gt;
&lt;br /&gt;
==== Source packages ====&lt;br /&gt;
&lt;br /&gt;
From the R console first pick a local mirror:&lt;br /&gt;
 chooseCRANmirror()&lt;br /&gt;
&lt;br /&gt;
you can then see what it picked with&lt;br /&gt;
 options(&amp;quot;repos&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
To permanently save the mirror site add it to &amp;lt;tt&amp;gt;~/.Rprofile&amp;lt;/tt&amp;gt;. For example:&lt;br /&gt;
 options(repos=c(CRAN=&amp;quot;&amp;lt;nowiki&amp;gt;http://cran.stat.auckland.ac.nz&amp;lt;/nowiki&amp;gt;&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
and then run &amp;lt;tt&amp;gt;install.packages()&amp;lt;/tt&amp;gt; as in the Quick Start section above.&lt;br /&gt;
&lt;br /&gt;
For more information see http://cran.r-project.org/doc/manuals/R-admin.html&lt;br /&gt;
&lt;br /&gt;
==== Linux ====&lt;br /&gt;
&lt;br /&gt;
===== Debian and Ubuntu =====&lt;br /&gt;
&lt;br /&gt;
''R'' and a number of pre-build cran packages are already present in the main repositories. Start with:&lt;br /&gt;
&lt;br /&gt;
 # apt-get install r-base r-cran-vr r-cran-rodbc r-cran-xml&lt;br /&gt;
&lt;br /&gt;
Once those are installed start &amp;quot;&amp;lt;tt&amp;gt;R&amp;lt;/tt&amp;gt;&amp;quot; at the command prompt and install the libraries not packaged by the OS:&lt;br /&gt;
: ''n.b. &amp;lt;tt&amp;gt;r-cran-sp&amp;lt;/tt&amp;gt; is now shipped as an official Debian/Ubuntu package and can be installed with &amp;lt;tt&amp;gt;apt&amp;lt;/tt&amp;gt; as above''&lt;br /&gt;
&lt;br /&gt;
 install.packages(&amp;quot;sp&amp;quot;)&lt;br /&gt;
 install.packages(&amp;quot;gstat&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Debian/Lenny ships with ''R'' 2.7.1 which is too old for the modern ''rgdal'' package. So we have to fetch an old one from the archive and build it from the Linux command line:&lt;br /&gt;
 $ wget http://cran.r-project.org/src/contrib/Archive/rgdal/rgdal_0.6-24.tar.gz&lt;br /&gt;
 $ R CMD INSTALL -l /usr/local/lib/R/site-library rgdal_0.6-24.tar.gz&lt;br /&gt;
&lt;br /&gt;
If you are using a newer version of R than that you can install the ''rgdal'' CRAN package directly:&lt;br /&gt;
  install.packages(&amp;quot;rgdal&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
And finally, back inside the ''R'' session:&lt;br /&gt;
 install.packages(&amp;quot;spgrass6&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* [http://cran.r-project.org/bin/linux/debian/README Debian] and [http://cran.r-project.org/bin/linux/ubuntu/README Ubuntu] specific help is also available from the R-project website.&lt;br /&gt;
&lt;br /&gt;
You can also use the CRAN Debian package repository: (pick one; adjust distribution as needed [here &amp;quot;Debian/testing&amp;quot;])&lt;br /&gt;
 deb http://debian.cran.r-project.org/cran2deb/debian-i386 testing/&lt;br /&gt;
 deb http://debian.cran.r-project.org/cran2deb/debian-amd64 testing/&lt;br /&gt;
&lt;br /&gt;
===== RPM based =====&lt;br /&gt;
&lt;br /&gt;
* '''RedHat, Suse, Mandrake''' and similar distros: take the latest '''''R''''' RPM and install it&lt;br /&gt;
&lt;br /&gt;
==== Mac OSX ====&lt;br /&gt;
&lt;br /&gt;
Start a R session, then&lt;br /&gt;
&lt;br /&gt;
* for install.packages() you might have to rely on building packages from source code. try:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
install.packages(&amp;quot;spgrass6&amp;quot;, type=&amp;quot;source&amp;quot;, dependencies = TRUE)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Startup of GRASS from within R:'''&lt;br /&gt;
&lt;br /&gt;
First you need to find the path to the GRASS binaries: Control-click on the GRASS.app and you'll get a popup menu; select &amp;quot;Show Package Contents&amp;quot; - this opens you to the directory structure. Go to Contents-&amp;gt;MacOS which would be &amp;quot;GISBASE&amp;quot;; So, in my case, the &amp;quot;gisBase&amp;quot; parameter is &amp;quot;/HD/Applications/Grass-6.4/Contents/MacOS&amp;quot;. If you Command-click at the top of the window on the folder icon beside &amp;quot;MacOS&amp;quot; (from the line above this one), you can see the full path.&lt;br /&gt;
&lt;br /&gt;
Now we can run GRASS from within a R session:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
initGRASS(gisBase ='/Applications/GRASS/GRASS-6.4.app/Contents/MacOS', &lt;br /&gt;
          location = 'geostat2012_ll', mapset = 'user1', &lt;br /&gt;
          gisDbase = '/Users/Lars/Documents/Biologi/grassdata', override = TRUE)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Troubleshooting =====&lt;br /&gt;
If you get an error message when trying to call GRASS from R containing this line: dyld: Library not loaded: /usr/local/lib/libintl.8.dylib you need to establish a link from /Applications/Grass/GRASS-7.0.app/Contents/MacOS/lib/libintl.8.dylib to /usr/local/lib. This can be done through Terminal with the command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo ln -s /Applications/Grass/GRASS-7.0.app/Contents/MacOS/lib/libintl.8.dylib /usr/local/lib/&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: The path to the GRASS-x.x.app must reflect your own configuration.&lt;br /&gt;
&lt;br /&gt;
==== MS Windows ====&lt;br /&gt;
&lt;br /&gt;
===== Installation =====&lt;br /&gt;
Run:&lt;br /&gt;
 install.packages(&amp;quot;spgrass6&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
or install Task View 'Spatial' - Analysis of Spatial Data&lt;br /&gt;
 install.packages(&amp;quot;ctv&amp;quot;)&lt;br /&gt;
 library(&amp;quot;ctv&amp;quot;)&lt;br /&gt;
 install.views(&amp;quot;Spatial&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
===== GRASS 6: Calling R from GRASS =====&lt;br /&gt;
On Windows, the easiest way is calling  R in a running GRASS 6 session by specifying the full path to the R startup executable. Note that all &amp;quot;\&amp;quot; must be changed to &amp;quot;/&amp;quot;, furthermore quote white spaces in the path:&lt;br /&gt;
&lt;br /&gt;
   GRASS 6.4&amp;gt; C:/Users/&amp;quot;Catherine user&amp;quot;/Documents/R/R-2.15.1/bin/i386/R.exe&lt;br /&gt;
   R version 2.15.1 (2012-06-22) -- &amp;quot;Roasted Marshmallows&amp;quot;&lt;br /&gt;
   ...&lt;br /&gt;
   library(spgrass6)&lt;br /&gt;
&lt;br /&gt;
The best solution is then to add this path to the %PATH% entry in the grass64.bat-starting script.&lt;br /&gt;
&lt;br /&gt;
===== GRASS 6: calling GRASS from R =====&lt;br /&gt;
On Windows, the second easiest way is calling GRASS from R with ''initGRASS()'' from the R package spgrass6(). After GRASS has been initialized for R, you have access to GRASS commands from within R (you may need to adjust the path):&lt;br /&gt;
&lt;br /&gt;
   spgrass6()&lt;br /&gt;
   loc &amp;lt;- initGRASS(&amp;quot;C:\\Program Files\\GRASS 6.4&amp;quot;, home=tempdir())&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For an example, see http://geomorphometry.org/content/geomorphometry-r-saga-ilwis-grass&lt;br /&gt;
&lt;br /&gt;
===== GRASS 6 Usage III (taken from the grass-stats-ML) =====&lt;br /&gt;
&lt;br /&gt;
http://lists.osgeo.org/pipermail/grass-stats/2010-September/001274.html&lt;br /&gt;
&lt;br /&gt;
at the moment there is following for a Grass-R-connection implemented in WinGRASS 64&lt;br /&gt;
&lt;br /&gt;
(1) the WinGrass64-installer searches during installation for a installed R and writes - &lt;br /&gt;
if found - the R-installation-path to %PATH% in the grass64.bat-starting script&lt;br /&gt;
(see http://trac.osgeo.org/grass/browser/grass/branches/develbranch_6/mswindows/GRASS-Installer.nsi#L660)&lt;br /&gt;
&lt;br /&gt;
--&amp;gt; this means that you FIRST have to install R and then winGRASS!&lt;br /&gt;
&lt;br /&gt;
(2) you can start &amp;quot;GRASS-command-line&amp;quot; - it's a windows-command-line, not a msys-rxvt-terminal &lt;br /&gt;
(you can find this starting option under Programs -&amp;gt; GRASS64 -&amp;gt; Grass command line; but not as a desktop icon)&lt;br /&gt;
&lt;br /&gt;
with this starting option you start a Grass-session in the good old text mode. &lt;br /&gt;
if you type R in the command line, then you start R inside a GRASS-session like in Linux.&lt;br /&gt;
&lt;br /&gt;
===== GRASS 7 Usage =====&lt;br /&gt;
&lt;br /&gt;
In WinGRASS 7 ([http://grass.osgeo.org/download/software.php#g70x standalone installer]) the [http://code.google.com/p/batchfiles/ Windows batchfiles for use with R] are now integrated for a smooth WinGRASS-R-integration. The R-installation-path is dynamically loaded into PATH.&lt;br /&gt;
&lt;br /&gt;
Start WinGRASS 7, bring the WinGRASS-windows console in front and type ''R'' for opening a R-session (command line mode) inside a GRASS-session.&lt;br /&gt;
&lt;br /&gt;
[[Image:wingrass7_R_integration.png|600px|center]]&lt;br /&gt;
&lt;br /&gt;
Start WinGRASS 7, bring the WinGRASS-windows console in front and type ''RGui'' for opening a R-session (GUI mode) inside a GRASS-session.&lt;br /&gt;
&lt;br /&gt;
[[Image:wingrass7_RGui_integration.png|600px|center]]&lt;br /&gt;
&lt;br /&gt;
===== Open tickets =====&lt;br /&gt;
&lt;br /&gt;
* Ticket {{trac|1103}} (new enhancement) WinGrass64 - windows-commandline not released: a Grass-session with wxGui, command-line and R inside a Grass-session would be possible (as already does in WinGrass7)&lt;br /&gt;
* Ticket {{trac|1149}} (new enhancement) WinGrass - load R-installation-path dynamically into PATH:  Wingrass would recognize also an upgraded R-installation (which does not at the moment)&lt;br /&gt;
&lt;br /&gt;
=== Command help ===&lt;br /&gt;
&lt;br /&gt;
Start the ''R'' help browser:&lt;br /&gt;
 help.start()&lt;br /&gt;
&lt;br /&gt;
* Select '''Packages''' and then '''spgrass6'''.&lt;br /&gt;
&lt;br /&gt;
=== Running ===&lt;br /&gt;
: ''by Roger Bivand''&lt;br /&gt;
&lt;br /&gt;
The ''R'' interface for GRASS 5.4 was provided by a CRAN package called ''grass''. Changes going forward to the current GRASS 6 release meant that the interface had to be rewritten, and this provided the opportunity to adapt it to the ''sp'' CRAN package classes. Because GRASS provides the same kinds of data as ''sp'' classes handle, and relies on much of the same open source infrastructure (PROJ.4, GDAL, OGR), this step seemed sensible. Wherever possible ''spgrass6'' tries to respect the [[current region]] in GRASS to avoid handling raster data with different resolutions or extents. ''R'' is assumed to be running within GRASS:&lt;br /&gt;
&lt;br /&gt;
==== Startup ====&lt;br /&gt;
* Start GRASS. At the GRASS command line start ''R''.&lt;br /&gt;
: ''In this example we will use the sample Spearfish dataset.''&lt;br /&gt;
&lt;br /&gt;
Reset the region settings to the defaults&lt;br /&gt;
 GRASS&amp;gt; g.region -d&lt;br /&gt;
&lt;br /&gt;
Launch R from the GRASS prompt&lt;br /&gt;
 GRASS&amp;gt; R&lt;br /&gt;
&lt;br /&gt;
Load the ''spgrass6'' library:&lt;br /&gt;
 &amp;gt; library(spgrass6)&lt;br /&gt;
&lt;br /&gt;
Get the GRASS environment (mapset, region, map projection, etc.); you can display the metadata for your location by printing G:&lt;br /&gt;
 &amp;gt; G &amp;lt;- gmeta6()&lt;br /&gt;
&lt;br /&gt;
==== Reading in data ====&lt;br /&gt;
Read in two raster maps:&lt;br /&gt;
 &amp;gt; spear &amp;lt;- readRAST6(c(&amp;quot;geology&amp;quot;, &amp;quot;elevation.dem&amp;quot;),&lt;br /&gt;
 +          cat=c(TRUE, FALSE), ignore.stderr=TRUE,&lt;br /&gt;
 +          plugin=NULL)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The metadata are accessed and available, but are not (yet) used to structure the ''sp'' class objects, here a SpatialGridDataFrame object filled with data from two Spearfish layers. Here is a plot of the elevation data:&lt;br /&gt;
 &amp;gt; image(spear, attr = 2, col = terrain.colors(20))&lt;br /&gt;
&lt;br /&gt;
Add a title to the plot:&lt;br /&gt;
 &amp;gt; title(&amp;quot;Spearfish elevation&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
[[Image:R_stats_elev.png|center]]&lt;br /&gt;
&lt;br /&gt;
In addition, we can show what is going on inside the objects read into R:&lt;br /&gt;
 &amp;gt; '''str(G)'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
List of 26&lt;br /&gt;
 $ GISDBASE     : chr &amp;quot;/home/rsb/topics/grassdata&amp;quot;&lt;br /&gt;
 $ LOCATION_NAME: chr &amp;quot;spearfish57&amp;quot;&lt;br /&gt;
 $ MAPSET       : chr &amp;quot;rsb&amp;quot;&lt;br /&gt;
 $ DEBUG        : chr &amp;quot;0&amp;quot;&lt;br /&gt;
 $ GRASS_GUI    : chr &amp;quot;text&amp;quot;&lt;br /&gt;
 $ projection   : chr &amp;quot;1 (UTM)&amp;quot;&lt;br /&gt;
 $ zone         : chr &amp;quot;13&amp;quot;&lt;br /&gt;
 $ datum        : chr &amp;quot;nad27&amp;quot;&lt;br /&gt;
 $ ellipsoid    : chr &amp;quot;clark66&amp;quot;&lt;br /&gt;
 $ north        : num 4928010&lt;br /&gt;
 $ south        : num 4913700&lt;br /&gt;
 $ west         : num 589980&lt;br /&gt;
 $ east         : num 609000&lt;br /&gt;
 $ top          : num 1&lt;br /&gt;
 $ bottom       : num 0&lt;br /&gt;
 $ nsres        : num 30&lt;br /&gt;
 $ nsres3       : num 30&lt;br /&gt;
 $ ewres        : num 30&lt;br /&gt;
 $ ewres3       : num 30&lt;br /&gt;
 $ tbres        : num 1&lt;br /&gt;
 $ rows         : int 477&lt;br /&gt;
 $ rows3        : int 477&lt;br /&gt;
 $ cols         : int 634&lt;br /&gt;
 $ cols3        : int 634&lt;br /&gt;
 $ depths       : int 1&lt;br /&gt;
 $ proj4        : chr &amp;quot;+proj=utm +zone=13 +a=6378206.4 +rf=294.9786982 +no_defs +nadgrids=/home/rsb/topics/grass61/grass-6.1.cvs/etc/nad/conus&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;gt; '''summary(spear)'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object of class SpatialGridDataFrame&lt;br /&gt;
Coordinates:&lt;br /&gt;
              min     max&lt;br /&gt;
coords.x1  589980  609000&lt;br /&gt;
coords.x2 4913700 4928010&lt;br /&gt;
Is projected: TRUE &lt;br /&gt;
proj4string : [+proj=utm +zone=13 +a=6378206.4 +rf=294.9786982 +no_defs +nadgrids=/home/rsb/topics/grass61/grass-6.1.cvs/etc/nad/conus]&lt;br /&gt;
Number of points: 2&lt;br /&gt;
Grid attributes:&lt;br /&gt;
  cellcentre.offset cellsize cells.dim&lt;br /&gt;
1            589995       30       634&lt;br /&gt;
2           4913715       30       477&lt;br /&gt;
Data attributes:&lt;br /&gt;
      geology      elevation.dem  &lt;br /&gt;
 sandstone:74959   Min.   : 1066  &lt;br /&gt;
 limestone:61355   1st Qu.: 1200  &lt;br /&gt;
 shale    :46423   Median : 1316  &lt;br /&gt;
 sand     :36561   Mean   : 1354  &lt;br /&gt;
 igneous  :36534   3rd Qu.: 1488  &lt;br /&gt;
 (Other)  :37636   Max.   : 1840  &lt;br /&gt;
 NA's     : 8950   NA's   :10101  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Summarizing data ====&lt;br /&gt;
We can create a table of cell counts:&lt;br /&gt;
 &amp;gt; '''table(spear$geology)'''&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!metamorphic&lt;br /&gt;
!transition&lt;br /&gt;
!igneous&lt;br /&gt;
!sandstone&lt;br /&gt;
!limestone&lt;br /&gt;
!shale&lt;br /&gt;
!sandy shale&lt;br /&gt;
!claysand&lt;br /&gt;
!sand&lt;br /&gt;
|-&lt;br /&gt;
|11693&lt;br /&gt;
|142&lt;br /&gt;
|36534&lt;br /&gt;
|74959&lt;br /&gt;
|61355&lt;br /&gt;
|46423&lt;br /&gt;
|11266&lt;br /&gt;
|14535&lt;br /&gt;
|36561&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
And compare with the equivalent GRASS module:&lt;br /&gt;
 &amp;gt; '''execGRASS(&amp;quot;r.stats&amp;quot;, flags=c(&amp;quot;c&amp;quot;, &amp;quot;l&amp;quot;), parameters=list(input=&amp;quot;geology&amp;quot;), ignore.stderr=TRUE)'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 metamorphic 11693&lt;br /&gt;
2 transition 142&lt;br /&gt;
3 igneous 36534&lt;br /&gt;
4 sandstone 74959&lt;br /&gt;
5 limestone 61355&lt;br /&gt;
6 shale 46423&lt;br /&gt;
7 sandy shale 11266&lt;br /&gt;
8 claysand 14535&lt;br /&gt;
9 sand 36561&lt;br /&gt;
* no data 8950&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a box plot of geologic types at different elevations:&lt;br /&gt;
 &amp;gt; '''boxplot(spear$elevation.dem ~ spear$geology, medlwd = 1)'''&lt;br /&gt;
&lt;br /&gt;
[[Image:R_stats_boxplot_geo.png|center]]&lt;br /&gt;
&lt;br /&gt;
==== Exporting data back to GRASS ====&lt;br /&gt;
Finally, a SpatialGridDataFrame object is written back to a GRASS raster map:&lt;br /&gt;
&lt;br /&gt;
First prepare some data:  (square root of elevation)&lt;br /&gt;
 &amp;gt; spear$sqdem &amp;lt;- sqrt(spear$elevation.dem)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Export data from ''R'' back into a GRASS raster map:&lt;br /&gt;
 &amp;gt; writeRAST6(spear, &amp;quot;sqdemSP&amp;quot;, zcol=&amp;quot;sqdem&amp;quot;, ignore.stderr=TRUE)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Check that it imported into GRASS ok:&lt;br /&gt;
 &amp;gt; '''execGRASS(&amp;quot;r.info&amp;quot;, parameters=list(map=&amp;quot;sqdemSP&amp;quot;))'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 +----------------------------------------------------------------------------+&lt;br /&gt;
 | Layer:    sqdemSP                        Date: Sun May 14 21:59:26 2006    |&lt;br /&gt;
 | Mapset:   rsb                            Login of Creator: rsb             |&lt;br /&gt;
 | Location: spearfish57                                                      |&lt;br /&gt;
 | DataBase: /home/rsb/topics/grassdata                                       |&lt;br /&gt;
 | Title:     ( sqdemSP )                                                     |&lt;br /&gt;
 |----------------------------------------------------------------------------|&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |   Type of Map:  raster              Number of Categories: 255              |&lt;br /&gt;
 |   Data Type:    FCELL                                                      |&lt;br /&gt;
 |   Rows:         477                                                        |&lt;br /&gt;
 |   Columns:      634                                                        |&lt;br /&gt;
 |   Total Cells:  302418                                                     |&lt;br /&gt;
 |        Projection: UTM (zone 13)                                           |&lt;br /&gt;
 |            N:    4928010    S:    4913700   Res:    30                     |&lt;br /&gt;
 |            E:     609000    W:     589980   Res:    30                     |&lt;br /&gt;
 |   Range of data:    min =  32.649654 max = 42.895222                       |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |   Data Source:                                                             |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |   Data Description:                                                        |&lt;br /&gt;
 |    generated by r.in.gdal                                                  |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 +----------------------------------------------------------------------------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Calling GRASS functionality in R batch job ===&lt;br /&gt;
&lt;br /&gt;
To call GRASS functionality within a R batch job, use the initGRASS() function to define the GRASS settings:&lt;br /&gt;
&lt;br /&gt;
    library(spgrass6)&lt;br /&gt;
    &lt;br /&gt;
    # initialisation and the use of spearfish60 data&lt;br /&gt;
    initGRASS(gisBase = &amp;quot;/usr/local/grass-6.4.1&amp;quot;, home = tempdir(), &lt;br /&gt;
              gisDbase = &amp;quot;/home/neteler/grassdata/&amp;quot;,&lt;br /&gt;
              location = &amp;quot;spearfish60&amp;quot;, mapset = &amp;quot;user1&amp;quot;, SG=&amp;quot;elevation.dem&amp;quot;,&lt;br /&gt;
              override = TRUE)&lt;br /&gt;
    &lt;br /&gt;
    system(&amp;quot;g.region -d&amp;quot;)&lt;br /&gt;
    # verify&lt;br /&gt;
    gmeta6()&lt;br /&gt;
    &lt;br /&gt;
    spear &amp;lt;- readRAST6(c(&amp;quot;geology&amp;quot;, &amp;quot;elevation.dem&amp;quot;),&lt;br /&gt;
              cat=c(TRUE, FALSE), ignore.stderr=TRUE,&lt;br /&gt;
              plugin=NULL)&lt;br /&gt;
    &lt;br /&gt;
    summary(spear$geology)&lt;br /&gt;
&lt;br /&gt;
Run this script with&lt;br /&gt;
&lt;br /&gt;
    R CMD BATCH batch.R&lt;br /&gt;
&lt;br /&gt;
The result is (shorted here):&lt;br /&gt;
&lt;br /&gt;
    cat batch.Rout&lt;br /&gt;
    &lt;br /&gt;
    R version 2.10.0 (2009-10-26)&lt;br /&gt;
    Copyright (C) 2009 The R Foundation for Statistical Computing&lt;br /&gt;
    ISBN 3-900051-07-0&lt;br /&gt;
    ...&lt;br /&gt;
    &amp;gt; library(spgrass6)&lt;br /&gt;
    Loading required package: sp&lt;br /&gt;
    Loading required package: rgdal&lt;br /&gt;
    Geospatial Data Abstraction Library extensions to R successfully loaded&lt;br /&gt;
    Loaded GDAL runtime: GDAL 1.7.2, released 2010/04/23&lt;br /&gt;
    Path to GDAL shared files: /usr/local/share/gdal&lt;br /&gt;
    Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009&lt;br /&gt;
    Path to PROJ.4 shared files: (autodetected)&lt;br /&gt;
    Loading required package: XML&lt;br /&gt;
    GRASS GIS interface loaded with GRASS version: (GRASS not running)&lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; # initialisation and the use of spearfish60 data&lt;br /&gt;
    &amp;gt; initGRASS(gisBase = &amp;quot;/usr/local/grass-6.4.1&amp;quot;, home = tempdir(), gisDbase = &amp;quot;/home/neteler/grassdata/&amp;quot;,&lt;br /&gt;
    +           location = &amp;quot;spearfish60&amp;quot;, mapset = &amp;quot;user1&amp;quot;, SG=&amp;quot;elevation.dem&amp;quot;, override = TRUE)&lt;br /&gt;
    gisdbase    /home/neteler/grassdata/ &lt;br /&gt;
    location    spearfish60 &lt;br /&gt;
    mapset      user1 &lt;br /&gt;
    rows        477 &lt;br /&gt;
    columns     634 &lt;br /&gt;
    north       4928010 &lt;br /&gt;
    south       4913700 &lt;br /&gt;
    west        589980 &lt;br /&gt;
    east        609000 &lt;br /&gt;
    nsres       30 &lt;br /&gt;
    ewres       30 &lt;br /&gt;
    projection  +proj=utm +zone=13 +a=6378206.4 +rf=294.9786982 +no_defs&lt;br /&gt;
    +nadgrids=/usr/local/grass-6.4.1/etc/nad/conus +to_meter=1.0 &lt;br /&gt;
    Warning messages:&lt;br /&gt;
    1: In dir.create(gisDbase) : '/home/neteler/grassdata' already exists&lt;br /&gt;
    2: In dir.create(loc_path) :&lt;br /&gt;
      '/home/neteler/grassdata//spearfish60' already exists&lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; system(&amp;quot;g.region -d&amp;quot;)&lt;br /&gt;
    &amp;gt; # verify&lt;br /&gt;
    &amp;gt; gmeta6()&lt;br /&gt;
    gisdbase    /home/neteler/grassdata/ &lt;br /&gt;
    location    spearfish60 &lt;br /&gt;
    mapset      user1 &lt;br /&gt;
    rows        477 &lt;br /&gt;
    columns     634 &lt;br /&gt;
    north       4928010 &lt;br /&gt;
    ...&lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; spear &amp;lt;- readRAST6(c(&amp;quot;geology&amp;quot;, &amp;quot;elevation.dem&amp;quot;),&lt;br /&gt;
    +           cat=c(TRUE, FALSE), ignore.stderr=TRUE,&lt;br /&gt;
    +           plugin=NULL)&lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; summary(spear$geology)&lt;br /&gt;
    metamorphic  transition     igneous   sandstone   limestone       shale &lt;br /&gt;
          11693         142       36534       74959       61355       46423 &lt;br /&gt;
    sandy shale    claysand        sand        NA's &lt;br /&gt;
          11266       14535       36561        8950 &lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; proc.time()&lt;br /&gt;
       user  system elapsed &lt;br /&gt;
      2.891   0.492   3.412 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== GRASS Modules ===&lt;br /&gt;
&lt;br /&gt;
==== v.krige ====&lt;br /&gt;
&lt;br /&gt;
{{cmd|v.krige|version=70}} is a GRASS python script which performs kriging operations in the GRASS environment, using R functions for the back-end interpolation. It is present in GRASS 6.5svn, and further developed in GRASS 7svn. It requires a number of dependencies: '''python-rpy2''' (''needs to be &amp;quot;Rpy2&amp;quot;, &amp;quot;Rpy&amp;quot; will not do''), then the following R-CRAN packages:&lt;br /&gt;
* gstat, spgrass6 (as above)&lt;br /&gt;
* maptools&lt;br /&gt;
 install.packages(&amp;quot;maptools&amp;quot;)&lt;br /&gt;
* automap (optional), with gpclib (or rgeos)&lt;br /&gt;
 install.packages(&amp;quot;automap&amp;quot;)&lt;br /&gt;
 install.packages(&amp;quot;rgeos&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
(''merge further info from man page here'')&lt;br /&gt;
&lt;br /&gt;
=== Getting Support ===&lt;br /&gt;
&lt;br /&gt;
* Primary support for ''R'' + GRASS and the ''spgrass6'' package is through the [http://lists.osgeo.org/mailman/listinfo/grass-stats grass-stats] mailing list.&lt;br /&gt;
&lt;br /&gt;
=== See also ===&lt;br /&gt;
&lt;br /&gt;
* R. Bivand, 2007: [http://spatial.nhh.no/R/etc/FBK07 Interfacing R and OSGeo projects: status and perspectives] (Presentation with slides and scripts)&lt;br /&gt;
&lt;br /&gt;
* http://grass.ibiblio.org/statsgrass/index.php#grassR&lt;br /&gt;
&lt;br /&gt;
* Using GRASS and R: http://grass.osgeo.org/statsgrass/grass6_r_interface.html&lt;br /&gt;
&lt;br /&gt;
* Connecting R to RDBMS: http://grass.osgeo.org/statsgrass/r_and_dbms.html&lt;br /&gt;
&lt;br /&gt;
* [http://www.r-project.org R-Statistics homepage]&lt;br /&gt;
&lt;br /&gt;
* [http://r-spatial.sourceforge.net/ R-spatial main web page]&lt;br /&gt;
&lt;br /&gt;
* [http://geodacenter.asu.edu/r-spatial-projects R Spatial Projects at ASU]&lt;br /&gt;
&lt;br /&gt;
* Neural Networks with GRASS and R (posted by Markus Neteler on the grass-user mailing list) http://www.uam.es/proyectosinv/Mclim/pdf/MBenito_EcoMod.pdf&lt;br /&gt;
&lt;br /&gt;
* A detailed example on the use of GRASS and R, with spearfish data: http://casoilresource.lawr.ucdavis.edu/drupal/node/438&lt;br /&gt;
&lt;br /&gt;
* Using R and GRASS with cygwin: It is possible to use Rterm inside the GRASS shell in cygwin, just as in Unix/Linux or OSX. You should not, however, start Rterm from a cygwin xterm, because Rterm is not expecting to be run in an xterm under Windows, and loses its input. If you use the regular cygwin bash shell, but need to start display windows, start X from within GRASS with startx &amp;amp;, and then start Rterm in the same cygwin shell, not in the xterm.&lt;br /&gt;
&lt;br /&gt;
* [http://r-spatial.sourceforge.net/ Spatial data in R] (&amp;lt;code&amp;gt;sp&amp;lt;/code&amp;gt;) is a '''''R''''' library that provides classes and methods for spatial data (points, lines, polygons, grids), and to new or existing spatial statistics '''''R''''' packages that use sp, depend on sp, or will become dependent on &amp;lt;code&amp;gt;sp&amp;lt;/code&amp;gt;, such as &amp;lt;code&amp;gt;maptools&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;rgdal&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;splancs&amp;lt;/code&amp;gt;, '''&amp;lt;code&amp;gt;spgrass6&amp;lt;/code&amp;gt;''', &amp;lt;code&amp;gt;gstat&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;spgwr&amp;lt;/code&amp;gt; and many others.&lt;br /&gt;
&lt;br /&gt;
* [http://rpy.sourceforge.net/ RPy] - Python interface to the R Programming Language&lt;br /&gt;
&lt;br /&gt;
=== Workshop material ===&lt;br /&gt;
&lt;br /&gt;
* M. Neteler and M. Metz, 2011: ''Introduction to GRASS GIS''. GEOSTAT 2011 Landau. [http://geostat-course.org/Topic_NetelerMetz_2011 Download workshop material] '''(includes a R session)'''&lt;br /&gt;
&lt;br /&gt;
=== Articles ===&lt;br /&gt;
&lt;br /&gt;
* [http://grass.osgeo.org/newsletter/grassnews3.html GRASS News vol.3], June 2005 (R. Bivand. Interfacing GRASS 6 and R. ''GRASS Newsletter'', 3:11-16, June 2005. ISSN 1614-8746).&lt;br /&gt;
* [http://www.osgeo.org/journal OSGeo Journal] vol. 1 May 2007 (R. Bivand. Using the R— GRASS interface. ''OSGeo Journal'', 1:31-33, May 2007. ISSN 1614-8746).&lt;br /&gt;
* [http://www.grassbook.org GRASS Book, last chapter]&lt;br /&gt;
&lt;br /&gt;
[[Category:Installation]]&lt;br /&gt;
[[Category:FAQ]]&lt;br /&gt;
[[Category:Linking to other languages]]&lt;br /&gt;
[[Category:R]]&lt;/div&gt;</summary>
		<author><name>⚠️Dalby</name></author>
	</entry>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=R_statistics&amp;diff=16424</id>
		<title>R statistics</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=R_statistics&amp;diff=16424"/>
		<updated>2012-09-07T13:13:00Z</updated>

		<summary type="html">&lt;p&gt;⚠️Dalby: /* Mac OSX */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Q:''' How do I enjoy high quality statistic analysis in GRASS?&lt;br /&gt;
&lt;br /&gt;
'''A:''' Well, GRASS has got an interface to the most powerful statistics analysis package around: '''''R''''' (http://www.r-project.org)&lt;br /&gt;
: The [http://cran.r-project.org/web/packages/spgrass6/ '''spgrass6'''] ''R'' addon package provides the R &amp;amp;larr;&amp;amp;rarr; GRASS interface.&lt;br /&gt;
&lt;br /&gt;
Using R in GRASS GIS directly has two meanings:&lt;br /&gt;
&lt;br /&gt;
* The '''first''' is that R is run &amp;quot;on top of&amp;quot; GRASS, transferring GRASS data to R to run statistical functions on the imported data as R objects in memory, and possibly transfer the results back to GRASS.&lt;br /&gt;
* The '''second''' is to leave the data mostly in GRASS, and to use R as a scripting language &amp;quot;on top of&amp;quot; GRASS with execGRASS() - in this case, little data is moved to R, so memory constraints are not important, but R functionality is available.&lt;br /&gt;
&lt;br /&gt;
=== Quick start ===&lt;br /&gt;
&lt;br /&gt;
For the impatient just start it:&lt;br /&gt;
&lt;br /&gt;
  &amp;gt; R&lt;br /&gt;
  &lt;br /&gt;
  #and install packages directly from the net&lt;br /&gt;
  pkgs &amp;lt;- c('akima', 'spgrass6', 'RODBC', 'VR', 'gstat')&lt;br /&gt;
  &lt;br /&gt;
  install.packages(pkgs, dependencies=TRUE, type='source') &lt;br /&gt;
&lt;br /&gt;
or to get all packages for spatial analysis in one:&lt;br /&gt;
&lt;br /&gt;
  &amp;gt; R&lt;br /&gt;
&lt;br /&gt;
  #To automatically install the spatial task view, the ctv package needs to be installed, e.g., via&lt;br /&gt;
  install.packages(&amp;quot;ctv&amp;quot;)&lt;br /&gt;
  library(&amp;quot;ctv&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
  install.views(&amp;quot;Spatial&amp;quot;)&lt;br /&gt;
  #or&lt;br /&gt;
  update.views(&amp;quot;Spatial&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have '''''R''''' and '''''spgrass6''''' on your system, have a look at this tutorial:&lt;br /&gt;
: http://grass.osgeo.org/statsgrass/grass_geostats.html&lt;br /&gt;
&lt;br /&gt;
=== Installation ===&lt;br /&gt;
&lt;br /&gt;
First of all you need to install '''''R''''' onto your system.&lt;br /&gt;
&lt;br /&gt;
R and many of its addon packages are pre-built and distributed through the CRAN network of mirrors. In addition many Linux distributions prepackage R and a number of the most popular addon toolboxes.&lt;br /&gt;
&lt;br /&gt;
All the necessary functions for the GRASS 6 interface are now in packages on CRAN, so that on Linux/Unix (or Mac OSX) installing '''rgdal''' from source with PROJ4 and GDAL installed, or Windows installing from binary, the required packages are: '''sp'''; '''maptools''' (now includes spmaptools); '''rgdal''' (now includes spGDAL, spproj); '''spgrass6''' - now all on CRAN.&lt;br /&gt;
&lt;br /&gt;
==== Source packages ====&lt;br /&gt;
&lt;br /&gt;
From the R console first pick a local mirror:&lt;br /&gt;
 chooseCRANmirror()&lt;br /&gt;
&lt;br /&gt;
you can then see what it picked with&lt;br /&gt;
 options(&amp;quot;repos&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
To permanently save the mirror site add it to &amp;lt;tt&amp;gt;~/.Rprofile&amp;lt;/tt&amp;gt;. For example:&lt;br /&gt;
 options(repos=c(CRAN=&amp;quot;&amp;lt;nowiki&amp;gt;http://cran.stat.auckland.ac.nz&amp;lt;/nowiki&amp;gt;&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
and then run &amp;lt;tt&amp;gt;install.packages()&amp;lt;/tt&amp;gt; as in the Quick Start section above.&lt;br /&gt;
&lt;br /&gt;
For more information see http://cran.r-project.org/doc/manuals/R-admin.html&lt;br /&gt;
&lt;br /&gt;
==== Linux ====&lt;br /&gt;
&lt;br /&gt;
===== Debian and Ubuntu =====&lt;br /&gt;
&lt;br /&gt;
''R'' and a number of pre-build cran packages are already present in the main repositories. Start with:&lt;br /&gt;
&lt;br /&gt;
 # apt-get install r-base r-cran-vr r-cran-rodbc r-cran-xml&lt;br /&gt;
&lt;br /&gt;
Once those are installed start &amp;quot;&amp;lt;tt&amp;gt;R&amp;lt;/tt&amp;gt;&amp;quot; at the command prompt and install the libraries not packaged by the OS:&lt;br /&gt;
: ''n.b. &amp;lt;tt&amp;gt;r-cran-sp&amp;lt;/tt&amp;gt; is now shipped as an official Debian/Ubuntu package and can be installed with &amp;lt;tt&amp;gt;apt&amp;lt;/tt&amp;gt; as above''&lt;br /&gt;
&lt;br /&gt;
 install.packages(&amp;quot;sp&amp;quot;)&lt;br /&gt;
 install.packages(&amp;quot;gstat&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Debian/Lenny ships with ''R'' 2.7.1 which is too old for the modern ''rgdal'' package. So we have to fetch an old one from the archive and build it from the Linux command line:&lt;br /&gt;
 $ wget http://cran.r-project.org/src/contrib/Archive/rgdal/rgdal_0.6-24.tar.gz&lt;br /&gt;
 $ R CMD INSTALL -l /usr/local/lib/R/site-library rgdal_0.6-24.tar.gz&lt;br /&gt;
&lt;br /&gt;
If you are using a newer version of R than that you can install the ''rgdal'' CRAN package directly:&lt;br /&gt;
  install.packages(&amp;quot;rgdal&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
And finally, back inside the ''R'' session:&lt;br /&gt;
 install.packages(&amp;quot;spgrass6&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* [http://cran.r-project.org/bin/linux/debian/README Debian] and [http://cran.r-project.org/bin/linux/ubuntu/README Ubuntu] specific help is also available from the R-project website.&lt;br /&gt;
&lt;br /&gt;
You can also use the CRAN Debian package repository: (pick one; adjust distribution as needed [here &amp;quot;Debian/testing&amp;quot;])&lt;br /&gt;
 deb http://debian.cran.r-project.org/cran2deb/debian-i386 testing/&lt;br /&gt;
 deb http://debian.cran.r-project.org/cran2deb/debian-amd64 testing/&lt;br /&gt;
&lt;br /&gt;
===== RPM based =====&lt;br /&gt;
&lt;br /&gt;
* '''RedHat, Suse, Mandrake''' and similar distros: take the latest '''''R''''' RPM and install it&lt;br /&gt;
&lt;br /&gt;
==== Mac OSX ====&lt;br /&gt;
&lt;br /&gt;
Start a R session, then&lt;br /&gt;
&lt;br /&gt;
* for install.packages() you might have to rely on building packages from source code. try:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
install.packages(&amp;quot;spgrass6&amp;quot;, type=&amp;quot;source&amp;quot;, dependencies = TRUE)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Startup of GRASS from within R:'''&lt;br /&gt;
&lt;br /&gt;
First you need to find the path to the GRASS binaries: Control-click on the GRASS.app and you'll get a popup menu; select &amp;quot;Show Package Contents&amp;quot; - this opens you to the directory structure. Go to Contents-&amp;gt;MacOS which would be &amp;quot;GISBASE&amp;quot;; So, in my case, the &amp;quot;gisBase&amp;quot; parameter is &amp;quot;/HD/Applications/Grass-6.4/Contents/MacOS&amp;quot;. If you Command-click at the top of the window on the folder icon beside &amp;quot;MacOS&amp;quot; (from the line above this one), you can see the full path.&lt;br /&gt;
&lt;br /&gt;
Now we can run GRASS from within a R session:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
initGRASS(gisBase ='/Applications/GRASS/GRASS-6.4.app/Contents/MacOS', &lt;br /&gt;
          location = 'geostat2012_ll', mapset = 'user1', &lt;br /&gt;
          gisDbase = '/Users/Lars/Documents/Biologi/grassdata', override = TRUE)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Troubleshooting =====&lt;br /&gt;
If you get an error message when trying to call GRASS from R containing this line: dyld: Library not loaded: /usr/local/lib/libintl.8.dylib you need to establish a link from /Applications/Grass/GRASS-7.0.app/Contents/MacOS/lib/libintl.8.dylib to /usr/local/lib. This can be done through Terminal with the command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo ln -s /Applications/Grass/GRASS-7.0.app/Contents/MacOS/lib/libintl.8.dylib /usr/local/lib/&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: The path to the GRASS-x.x must reflect your own configuration.&lt;br /&gt;
&lt;br /&gt;
==== MS Windows ====&lt;br /&gt;
&lt;br /&gt;
===== Installation =====&lt;br /&gt;
Run:&lt;br /&gt;
 install.packages(&amp;quot;spgrass6&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
or install Task View 'Spatial' - Analysis of Spatial Data&lt;br /&gt;
 install.packages(&amp;quot;ctv&amp;quot;)&lt;br /&gt;
 library(&amp;quot;ctv&amp;quot;)&lt;br /&gt;
 install.views(&amp;quot;Spatial&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
===== GRASS 6: Calling R from GRASS =====&lt;br /&gt;
On Windows, the easiest way is calling  R in a running GRASS 6 session by specifying the full path to the R startup executable. Note that all &amp;quot;\&amp;quot; must be changed to &amp;quot;/&amp;quot;, furthermore quote white spaces in the path:&lt;br /&gt;
&lt;br /&gt;
   GRASS 6.4&amp;gt; C:/Users/&amp;quot;Catherine user&amp;quot;/Documents/R/R-2.15.1/bin/i386/R.exe&lt;br /&gt;
   R version 2.15.1 (2012-06-22) -- &amp;quot;Roasted Marshmallows&amp;quot;&lt;br /&gt;
   ...&lt;br /&gt;
   library(spgrass6)&lt;br /&gt;
&lt;br /&gt;
The best solution is then to add this path to the %PATH% entry in the grass64.bat-starting script.&lt;br /&gt;
&lt;br /&gt;
===== GRASS 6: calling GRASS from R =====&lt;br /&gt;
On Windows, the second easiest way is calling GRASS from R with ''initGRASS()'' from the R package spgrass6(). After GRASS has been initialized for R, you have access to GRASS commands from within R (you may need to adjust the path):&lt;br /&gt;
&lt;br /&gt;
   spgrass6()&lt;br /&gt;
   loc &amp;lt;- initGRASS(&amp;quot;C:\\Program Files\\GRASS 6.4&amp;quot;, home=tempdir())&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For an example, see http://geomorphometry.org/content/geomorphometry-r-saga-ilwis-grass&lt;br /&gt;
&lt;br /&gt;
===== GRASS 6 Usage III (taken from the grass-stats-ML) =====&lt;br /&gt;
&lt;br /&gt;
http://lists.osgeo.org/pipermail/grass-stats/2010-September/001274.html&lt;br /&gt;
&lt;br /&gt;
at the moment there is following for a Grass-R-connection implemented in WinGRASS 64&lt;br /&gt;
&lt;br /&gt;
(1) the WinGrass64-installer searches during installation for a installed R and writes - &lt;br /&gt;
if found - the R-installation-path to %PATH% in the grass64.bat-starting script&lt;br /&gt;
(see http://trac.osgeo.org/grass/browser/grass/branches/develbranch_6/mswindows/GRASS-Installer.nsi#L660)&lt;br /&gt;
&lt;br /&gt;
--&amp;gt; this means that you FIRST have to install R and then winGRASS!&lt;br /&gt;
&lt;br /&gt;
(2) you can start &amp;quot;GRASS-command-line&amp;quot; - it's a windows-command-line, not a msys-rxvt-terminal &lt;br /&gt;
(you can find this starting option under Programs -&amp;gt; GRASS64 -&amp;gt; Grass command line; but not as a desktop icon)&lt;br /&gt;
&lt;br /&gt;
with this starting option you start a Grass-session in the good old text mode. &lt;br /&gt;
if you type R in the command line, then you start R inside a GRASS-session like in Linux.&lt;br /&gt;
&lt;br /&gt;
===== GRASS 7 Usage =====&lt;br /&gt;
&lt;br /&gt;
In WinGRASS 7 ([http://grass.osgeo.org/download/software.php#g70x standalone installer]) the [http://code.google.com/p/batchfiles/ Windows batchfiles for use with R] are now integrated for a smooth WinGRASS-R-integration. The R-installation-path is dynamically loaded into PATH.&lt;br /&gt;
&lt;br /&gt;
Start WinGRASS 7, bring the WinGRASS-windows console in front and type ''R'' for opening a R-session (command line mode) inside a GRASS-session.&lt;br /&gt;
&lt;br /&gt;
[[Image:wingrass7_R_integration.png|600px|center]]&lt;br /&gt;
&lt;br /&gt;
Start WinGRASS 7, bring the WinGRASS-windows console in front and type ''RGui'' for opening a R-session (GUI mode) inside a GRASS-session.&lt;br /&gt;
&lt;br /&gt;
[[Image:wingrass7_RGui_integration.png|600px|center]]&lt;br /&gt;
&lt;br /&gt;
===== Open tickets =====&lt;br /&gt;
&lt;br /&gt;
* Ticket {{trac|1103}} (new enhancement) WinGrass64 - windows-commandline not released: a Grass-session with wxGui, command-line and R inside a Grass-session would be possible (as already does in WinGrass7)&lt;br /&gt;
* Ticket {{trac|1149}} (new enhancement) WinGrass - load R-installation-path dynamically into PATH:  Wingrass would recognize also an upgraded R-installation (which does not at the moment)&lt;br /&gt;
&lt;br /&gt;
=== Command help ===&lt;br /&gt;
&lt;br /&gt;
Start the ''R'' help browser:&lt;br /&gt;
 help.start()&lt;br /&gt;
&lt;br /&gt;
* Select '''Packages''' and then '''spgrass6'''.&lt;br /&gt;
&lt;br /&gt;
=== Running ===&lt;br /&gt;
: ''by Roger Bivand''&lt;br /&gt;
&lt;br /&gt;
The ''R'' interface for GRASS 5.4 was provided by a CRAN package called ''grass''. Changes going forward to the current GRASS 6 release meant that the interface had to be rewritten, and this provided the opportunity to adapt it to the ''sp'' CRAN package classes. Because GRASS provides the same kinds of data as ''sp'' classes handle, and relies on much of the same open source infrastructure (PROJ.4, GDAL, OGR), this step seemed sensible. Wherever possible ''spgrass6'' tries to respect the [[current region]] in GRASS to avoid handling raster data with different resolutions or extents. ''R'' is assumed to be running within GRASS:&lt;br /&gt;
&lt;br /&gt;
==== Startup ====&lt;br /&gt;
* Start GRASS. At the GRASS command line start ''R''.&lt;br /&gt;
: ''In this example we will use the sample Spearfish dataset.''&lt;br /&gt;
&lt;br /&gt;
Reset the region settings to the defaults&lt;br /&gt;
 GRASS&amp;gt; g.region -d&lt;br /&gt;
&lt;br /&gt;
Launch R from the GRASS prompt&lt;br /&gt;
 GRASS&amp;gt; R&lt;br /&gt;
&lt;br /&gt;
Load the ''spgrass6'' library:&lt;br /&gt;
 &amp;gt; library(spgrass6)&lt;br /&gt;
&lt;br /&gt;
Get the GRASS environment (mapset, region, map projection, etc.); you can display the metadata for your location by printing G:&lt;br /&gt;
 &amp;gt; G &amp;lt;- gmeta6()&lt;br /&gt;
&lt;br /&gt;
==== Reading in data ====&lt;br /&gt;
Read in two raster maps:&lt;br /&gt;
 &amp;gt; spear &amp;lt;- readRAST6(c(&amp;quot;geology&amp;quot;, &amp;quot;elevation.dem&amp;quot;),&lt;br /&gt;
 +          cat=c(TRUE, FALSE), ignore.stderr=TRUE,&lt;br /&gt;
 +          plugin=NULL)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The metadata are accessed and available, but are not (yet) used to structure the ''sp'' class objects, here a SpatialGridDataFrame object filled with data from two Spearfish layers. Here is a plot of the elevation data:&lt;br /&gt;
 &amp;gt; image(spear, attr = 2, col = terrain.colors(20))&lt;br /&gt;
&lt;br /&gt;
Add a title to the plot:&lt;br /&gt;
 &amp;gt; title(&amp;quot;Spearfish elevation&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
[[Image:R_stats_elev.png|center]]&lt;br /&gt;
&lt;br /&gt;
In addition, we can show what is going on inside the objects read into R:&lt;br /&gt;
 &amp;gt; '''str(G)'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
List of 26&lt;br /&gt;
 $ GISDBASE     : chr &amp;quot;/home/rsb/topics/grassdata&amp;quot;&lt;br /&gt;
 $ LOCATION_NAME: chr &amp;quot;spearfish57&amp;quot;&lt;br /&gt;
 $ MAPSET       : chr &amp;quot;rsb&amp;quot;&lt;br /&gt;
 $ DEBUG        : chr &amp;quot;0&amp;quot;&lt;br /&gt;
 $ GRASS_GUI    : chr &amp;quot;text&amp;quot;&lt;br /&gt;
 $ projection   : chr &amp;quot;1 (UTM)&amp;quot;&lt;br /&gt;
 $ zone         : chr &amp;quot;13&amp;quot;&lt;br /&gt;
 $ datum        : chr &amp;quot;nad27&amp;quot;&lt;br /&gt;
 $ ellipsoid    : chr &amp;quot;clark66&amp;quot;&lt;br /&gt;
 $ north        : num 4928010&lt;br /&gt;
 $ south        : num 4913700&lt;br /&gt;
 $ west         : num 589980&lt;br /&gt;
 $ east         : num 609000&lt;br /&gt;
 $ top          : num 1&lt;br /&gt;
 $ bottom       : num 0&lt;br /&gt;
 $ nsres        : num 30&lt;br /&gt;
 $ nsres3       : num 30&lt;br /&gt;
 $ ewres        : num 30&lt;br /&gt;
 $ ewres3       : num 30&lt;br /&gt;
 $ tbres        : num 1&lt;br /&gt;
 $ rows         : int 477&lt;br /&gt;
 $ rows3        : int 477&lt;br /&gt;
 $ cols         : int 634&lt;br /&gt;
 $ cols3        : int 634&lt;br /&gt;
 $ depths       : int 1&lt;br /&gt;
 $ proj4        : chr &amp;quot;+proj=utm +zone=13 +a=6378206.4 +rf=294.9786982 +no_defs +nadgrids=/home/rsb/topics/grass61/grass-6.1.cvs/etc/nad/conus&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;gt; '''summary(spear)'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object of class SpatialGridDataFrame&lt;br /&gt;
Coordinates:&lt;br /&gt;
              min     max&lt;br /&gt;
coords.x1  589980  609000&lt;br /&gt;
coords.x2 4913700 4928010&lt;br /&gt;
Is projected: TRUE &lt;br /&gt;
proj4string : [+proj=utm +zone=13 +a=6378206.4 +rf=294.9786982 +no_defs +nadgrids=/home/rsb/topics/grass61/grass-6.1.cvs/etc/nad/conus]&lt;br /&gt;
Number of points: 2&lt;br /&gt;
Grid attributes:&lt;br /&gt;
  cellcentre.offset cellsize cells.dim&lt;br /&gt;
1            589995       30       634&lt;br /&gt;
2           4913715       30       477&lt;br /&gt;
Data attributes:&lt;br /&gt;
      geology      elevation.dem  &lt;br /&gt;
 sandstone:74959   Min.   : 1066  &lt;br /&gt;
 limestone:61355   1st Qu.: 1200  &lt;br /&gt;
 shale    :46423   Median : 1316  &lt;br /&gt;
 sand     :36561   Mean   : 1354  &lt;br /&gt;
 igneous  :36534   3rd Qu.: 1488  &lt;br /&gt;
 (Other)  :37636   Max.   : 1840  &lt;br /&gt;
 NA's     : 8950   NA's   :10101  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Summarizing data ====&lt;br /&gt;
We can create a table of cell counts:&lt;br /&gt;
 &amp;gt; '''table(spear$geology)'''&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!metamorphic&lt;br /&gt;
!transition&lt;br /&gt;
!igneous&lt;br /&gt;
!sandstone&lt;br /&gt;
!limestone&lt;br /&gt;
!shale&lt;br /&gt;
!sandy shale&lt;br /&gt;
!claysand&lt;br /&gt;
!sand&lt;br /&gt;
|-&lt;br /&gt;
|11693&lt;br /&gt;
|142&lt;br /&gt;
|36534&lt;br /&gt;
|74959&lt;br /&gt;
|61355&lt;br /&gt;
|46423&lt;br /&gt;
|11266&lt;br /&gt;
|14535&lt;br /&gt;
|36561&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
And compare with the equivalent GRASS module:&lt;br /&gt;
 &amp;gt; '''execGRASS(&amp;quot;r.stats&amp;quot;, flags=c(&amp;quot;c&amp;quot;, &amp;quot;l&amp;quot;), parameters=list(input=&amp;quot;geology&amp;quot;), ignore.stderr=TRUE)'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 metamorphic 11693&lt;br /&gt;
2 transition 142&lt;br /&gt;
3 igneous 36534&lt;br /&gt;
4 sandstone 74959&lt;br /&gt;
5 limestone 61355&lt;br /&gt;
6 shale 46423&lt;br /&gt;
7 sandy shale 11266&lt;br /&gt;
8 claysand 14535&lt;br /&gt;
9 sand 36561&lt;br /&gt;
* no data 8950&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a box plot of geologic types at different elevations:&lt;br /&gt;
 &amp;gt; '''boxplot(spear$elevation.dem ~ spear$geology, medlwd = 1)'''&lt;br /&gt;
&lt;br /&gt;
[[Image:R_stats_boxplot_geo.png|center]]&lt;br /&gt;
&lt;br /&gt;
==== Exporting data back to GRASS ====&lt;br /&gt;
Finally, a SpatialGridDataFrame object is written back to a GRASS raster map:&lt;br /&gt;
&lt;br /&gt;
First prepare some data:  (square root of elevation)&lt;br /&gt;
 &amp;gt; spear$sqdem &amp;lt;- sqrt(spear$elevation.dem)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Export data from ''R'' back into a GRASS raster map:&lt;br /&gt;
 &amp;gt; writeRAST6(spear, &amp;quot;sqdemSP&amp;quot;, zcol=&amp;quot;sqdem&amp;quot;, ignore.stderr=TRUE)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Check that it imported into GRASS ok:&lt;br /&gt;
 &amp;gt; '''execGRASS(&amp;quot;r.info&amp;quot;, parameters=list(map=&amp;quot;sqdemSP&amp;quot;))'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 +----------------------------------------------------------------------------+&lt;br /&gt;
 | Layer:    sqdemSP                        Date: Sun May 14 21:59:26 2006    |&lt;br /&gt;
 | Mapset:   rsb                            Login of Creator: rsb             |&lt;br /&gt;
 | Location: spearfish57                                                      |&lt;br /&gt;
 | DataBase: /home/rsb/topics/grassdata                                       |&lt;br /&gt;
 | Title:     ( sqdemSP )                                                     |&lt;br /&gt;
 |----------------------------------------------------------------------------|&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |   Type of Map:  raster              Number of Categories: 255              |&lt;br /&gt;
 |   Data Type:    FCELL                                                      |&lt;br /&gt;
 |   Rows:         477                                                        |&lt;br /&gt;
 |   Columns:      634                                                        |&lt;br /&gt;
 |   Total Cells:  302418                                                     |&lt;br /&gt;
 |        Projection: UTM (zone 13)                                           |&lt;br /&gt;
 |            N:    4928010    S:    4913700   Res:    30                     |&lt;br /&gt;
 |            E:     609000    W:     589980   Res:    30                     |&lt;br /&gt;
 |   Range of data:    min =  32.649654 max = 42.895222                       |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |   Data Source:                                                             |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |   Data Description:                                                        |&lt;br /&gt;
 |    generated by r.in.gdal                                                  |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 +----------------------------------------------------------------------------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Calling GRASS functionality in R batch job ===&lt;br /&gt;
&lt;br /&gt;
To call GRASS functionality within a R batch job, use the initGRASS() function to define the GRASS settings:&lt;br /&gt;
&lt;br /&gt;
    library(spgrass6)&lt;br /&gt;
    &lt;br /&gt;
    # initialisation and the use of spearfish60 data&lt;br /&gt;
    initGRASS(gisBase = &amp;quot;/usr/local/grass-6.4.1&amp;quot;, home = tempdir(), &lt;br /&gt;
              gisDbase = &amp;quot;/home/neteler/grassdata/&amp;quot;,&lt;br /&gt;
              location = &amp;quot;spearfish60&amp;quot;, mapset = &amp;quot;user1&amp;quot;, SG=&amp;quot;elevation.dem&amp;quot;,&lt;br /&gt;
              override = TRUE)&lt;br /&gt;
    &lt;br /&gt;
    system(&amp;quot;g.region -d&amp;quot;)&lt;br /&gt;
    # verify&lt;br /&gt;
    gmeta6()&lt;br /&gt;
    &lt;br /&gt;
    spear &amp;lt;- readRAST6(c(&amp;quot;geology&amp;quot;, &amp;quot;elevation.dem&amp;quot;),&lt;br /&gt;
              cat=c(TRUE, FALSE), ignore.stderr=TRUE,&lt;br /&gt;
              plugin=NULL)&lt;br /&gt;
    &lt;br /&gt;
    summary(spear$geology)&lt;br /&gt;
&lt;br /&gt;
Run this script with&lt;br /&gt;
&lt;br /&gt;
    R CMD BATCH batch.R&lt;br /&gt;
&lt;br /&gt;
The result is (shorted here):&lt;br /&gt;
&lt;br /&gt;
    cat batch.Rout&lt;br /&gt;
    &lt;br /&gt;
    R version 2.10.0 (2009-10-26)&lt;br /&gt;
    Copyright (C) 2009 The R Foundation for Statistical Computing&lt;br /&gt;
    ISBN 3-900051-07-0&lt;br /&gt;
    ...&lt;br /&gt;
    &amp;gt; library(spgrass6)&lt;br /&gt;
    Loading required package: sp&lt;br /&gt;
    Loading required package: rgdal&lt;br /&gt;
    Geospatial Data Abstraction Library extensions to R successfully loaded&lt;br /&gt;
    Loaded GDAL runtime: GDAL 1.7.2, released 2010/04/23&lt;br /&gt;
    Path to GDAL shared files: /usr/local/share/gdal&lt;br /&gt;
    Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009&lt;br /&gt;
    Path to PROJ.4 shared files: (autodetected)&lt;br /&gt;
    Loading required package: XML&lt;br /&gt;
    GRASS GIS interface loaded with GRASS version: (GRASS not running)&lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; # initialisation and the use of spearfish60 data&lt;br /&gt;
    &amp;gt; initGRASS(gisBase = &amp;quot;/usr/local/grass-6.4.1&amp;quot;, home = tempdir(), gisDbase = &amp;quot;/home/neteler/grassdata/&amp;quot;,&lt;br /&gt;
    +           location = &amp;quot;spearfish60&amp;quot;, mapset = &amp;quot;user1&amp;quot;, SG=&amp;quot;elevation.dem&amp;quot;, override = TRUE)&lt;br /&gt;
    gisdbase    /home/neteler/grassdata/ &lt;br /&gt;
    location    spearfish60 &lt;br /&gt;
    mapset      user1 &lt;br /&gt;
    rows        477 &lt;br /&gt;
    columns     634 &lt;br /&gt;
    north       4928010 &lt;br /&gt;
    south       4913700 &lt;br /&gt;
    west        589980 &lt;br /&gt;
    east        609000 &lt;br /&gt;
    nsres       30 &lt;br /&gt;
    ewres       30 &lt;br /&gt;
    projection  +proj=utm +zone=13 +a=6378206.4 +rf=294.9786982 +no_defs&lt;br /&gt;
    +nadgrids=/usr/local/grass-6.4.1/etc/nad/conus +to_meter=1.0 &lt;br /&gt;
    Warning messages:&lt;br /&gt;
    1: In dir.create(gisDbase) : '/home/neteler/grassdata' already exists&lt;br /&gt;
    2: In dir.create(loc_path) :&lt;br /&gt;
      '/home/neteler/grassdata//spearfish60' already exists&lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; system(&amp;quot;g.region -d&amp;quot;)&lt;br /&gt;
    &amp;gt; # verify&lt;br /&gt;
    &amp;gt; gmeta6()&lt;br /&gt;
    gisdbase    /home/neteler/grassdata/ &lt;br /&gt;
    location    spearfish60 &lt;br /&gt;
    mapset      user1 &lt;br /&gt;
    rows        477 &lt;br /&gt;
    columns     634 &lt;br /&gt;
    north       4928010 &lt;br /&gt;
    ...&lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; spear &amp;lt;- readRAST6(c(&amp;quot;geology&amp;quot;, &amp;quot;elevation.dem&amp;quot;),&lt;br /&gt;
    +           cat=c(TRUE, FALSE), ignore.stderr=TRUE,&lt;br /&gt;
    +           plugin=NULL)&lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; summary(spear$geology)&lt;br /&gt;
    metamorphic  transition     igneous   sandstone   limestone       shale &lt;br /&gt;
          11693         142       36534       74959       61355       46423 &lt;br /&gt;
    sandy shale    claysand        sand        NA's &lt;br /&gt;
          11266       14535       36561        8950 &lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; proc.time()&lt;br /&gt;
       user  system elapsed &lt;br /&gt;
      2.891   0.492   3.412 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== GRASS Modules ===&lt;br /&gt;
&lt;br /&gt;
==== v.krige ====&lt;br /&gt;
&lt;br /&gt;
{{cmd|v.krige|version=70}} is a GRASS python script which performs kriging operations in the GRASS environment, using R functions for the back-end interpolation. It is present in GRASS 6.5svn, and further developed in GRASS 7svn. It requires a number of dependencies: '''python-rpy2''' (''needs to be &amp;quot;Rpy2&amp;quot;, &amp;quot;Rpy&amp;quot; will not do''), then the following R-CRAN packages:&lt;br /&gt;
* gstat, spgrass6 (as above)&lt;br /&gt;
* maptools&lt;br /&gt;
 install.packages(&amp;quot;maptools&amp;quot;)&lt;br /&gt;
* automap (optional), with gpclib (or rgeos)&lt;br /&gt;
 install.packages(&amp;quot;automap&amp;quot;)&lt;br /&gt;
 install.packages(&amp;quot;rgeos&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
(''merge further info from man page here'')&lt;br /&gt;
&lt;br /&gt;
=== Getting Support ===&lt;br /&gt;
&lt;br /&gt;
* Primary support for ''R'' + GRASS and the ''spgrass6'' package is through the [http://lists.osgeo.org/mailman/listinfo/grass-stats grass-stats] mailing list.&lt;br /&gt;
&lt;br /&gt;
=== See also ===&lt;br /&gt;
&lt;br /&gt;
* R. Bivand, 2007: [http://spatial.nhh.no/R/etc/FBK07 Interfacing R and OSGeo projects: status and perspectives] (Presentation with slides and scripts)&lt;br /&gt;
&lt;br /&gt;
* http://grass.ibiblio.org/statsgrass/index.php#grassR&lt;br /&gt;
&lt;br /&gt;
* Using GRASS and R: http://grass.osgeo.org/statsgrass/grass6_r_interface.html&lt;br /&gt;
&lt;br /&gt;
* Connecting R to RDBMS: http://grass.osgeo.org/statsgrass/r_and_dbms.html&lt;br /&gt;
&lt;br /&gt;
* [http://www.r-project.org R-Statistics homepage]&lt;br /&gt;
&lt;br /&gt;
* [http://r-spatial.sourceforge.net/ R-spatial main web page]&lt;br /&gt;
&lt;br /&gt;
* [http://geodacenter.asu.edu/r-spatial-projects R Spatial Projects at ASU]&lt;br /&gt;
&lt;br /&gt;
* Neural Networks with GRASS and R (posted by Markus Neteler on the grass-user mailing list) http://www.uam.es/proyectosinv/Mclim/pdf/MBenito_EcoMod.pdf&lt;br /&gt;
&lt;br /&gt;
* A detailed example on the use of GRASS and R, with spearfish data: http://casoilresource.lawr.ucdavis.edu/drupal/node/438&lt;br /&gt;
&lt;br /&gt;
* Using R and GRASS with cygwin: It is possible to use Rterm inside the GRASS shell in cygwin, just as in Unix/Linux or OSX. You should not, however, start Rterm from a cygwin xterm, because Rterm is not expecting to be run in an xterm under Windows, and loses its input. If you use the regular cygwin bash shell, but need to start display windows, start X from within GRASS with startx &amp;amp;, and then start Rterm in the same cygwin shell, not in the xterm.&lt;br /&gt;
&lt;br /&gt;
* [http://r-spatial.sourceforge.net/ Spatial data in R] (&amp;lt;code&amp;gt;sp&amp;lt;/code&amp;gt;) is a '''''R''''' library that provides classes and methods for spatial data (points, lines, polygons, grids), and to new or existing spatial statistics '''''R''''' packages that use sp, depend on sp, or will become dependent on &amp;lt;code&amp;gt;sp&amp;lt;/code&amp;gt;, such as &amp;lt;code&amp;gt;maptools&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;rgdal&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;splancs&amp;lt;/code&amp;gt;, '''&amp;lt;code&amp;gt;spgrass6&amp;lt;/code&amp;gt;''', &amp;lt;code&amp;gt;gstat&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;spgwr&amp;lt;/code&amp;gt; and many others.&lt;br /&gt;
&lt;br /&gt;
* [http://rpy.sourceforge.net/ RPy] - Python interface to the R Programming Language&lt;br /&gt;
&lt;br /&gt;
=== Workshop material ===&lt;br /&gt;
&lt;br /&gt;
* M. Neteler and M. Metz, 2011: ''Introduction to GRASS GIS''. GEOSTAT 2011 Landau. [http://geostat-course.org/Topic_NetelerMetz_2011 Download workshop material] '''(includes a R session)'''&lt;br /&gt;
&lt;br /&gt;
=== Articles ===&lt;br /&gt;
&lt;br /&gt;
* [http://grass.osgeo.org/newsletter/grassnews3.html GRASS News vol.3], June 2005 (R. Bivand. Interfacing GRASS 6 and R. ''GRASS Newsletter'', 3:11-16, June 2005. ISSN 1614-8746).&lt;br /&gt;
* [http://www.osgeo.org/journal OSGeo Journal] vol. 1 May 2007 (R. Bivand. Using the R— GRASS interface. ''OSGeo Journal'', 1:31-33, May 2007. ISSN 1614-8746).&lt;br /&gt;
* [http://www.grassbook.org GRASS Book, last chapter]&lt;br /&gt;
&lt;br /&gt;
[[Category:Installation]]&lt;br /&gt;
[[Category:FAQ]]&lt;br /&gt;
[[Category:Linking to other languages]]&lt;br /&gt;
[[Category:R]]&lt;/div&gt;</summary>
		<author><name>⚠️Dalby</name></author>
	</entry>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=R_statistics&amp;diff=16420</id>
		<title>R statistics</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=R_statistics&amp;diff=16420"/>
		<updated>2012-09-07T11:59:52Z</updated>

		<summary type="html">&lt;p&gt;⚠️Dalby: /* Mac OSX */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Q:''' How do I enjoy high quality statistic analysis in GRASS?&lt;br /&gt;
&lt;br /&gt;
'''A:''' Well, GRASS has got an interface to the most powerful statistics analysis package around: '''''R''''' (http://www.r-project.org)&lt;br /&gt;
: The [http://cran.r-project.org/web/packages/spgrass6/ '''spgrass6'''] ''R'' addon package provides the R &amp;amp;larr;&amp;amp;rarr; GRASS interface.&lt;br /&gt;
&lt;br /&gt;
Using R in GRASS GIS directly has two meanings:&lt;br /&gt;
&lt;br /&gt;
* The '''first''' is that R is run &amp;quot;on top of&amp;quot; GRASS, transferring GRASS data to R to run statistical functions on the imported data as R objects in memory, and possibly transfer the results back to GRASS.&lt;br /&gt;
* The '''second''' is to leave the data mostly in GRASS, and to use R as a scripting language &amp;quot;on top of&amp;quot; GRASS with execGRASS() - in this case, little data is moved to R, so memory constraints are not important, but R functionality is available.&lt;br /&gt;
&lt;br /&gt;
=== Quick start ===&lt;br /&gt;
&lt;br /&gt;
For the impatient just start it:&lt;br /&gt;
&lt;br /&gt;
  &amp;gt; R&lt;br /&gt;
  &lt;br /&gt;
  #and install packages directly from the net&lt;br /&gt;
  pkgs &amp;lt;- c('akima', 'spgrass6', 'RODBC', 'VR', 'gstat')&lt;br /&gt;
  &lt;br /&gt;
  install.packages(pkgs, dependencies=TRUE, type='source') &lt;br /&gt;
&lt;br /&gt;
or to get all packages for spatial analysis in one:&lt;br /&gt;
&lt;br /&gt;
  &amp;gt; R&lt;br /&gt;
&lt;br /&gt;
  #To automatically install the spatial task view, the ctv package needs to be installed, e.g., via&lt;br /&gt;
  install.packages(&amp;quot;ctv&amp;quot;)&lt;br /&gt;
  library(&amp;quot;ctv&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
  install.views(&amp;quot;Spatial&amp;quot;)&lt;br /&gt;
  #or&lt;br /&gt;
  update.views(&amp;quot;Spatial&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Once you have '''''R''''' and '''''spgrass6''''' on your system, have a look at this tutorial:&lt;br /&gt;
: http://grass.osgeo.org/statsgrass/grass_geostats.html&lt;br /&gt;
&lt;br /&gt;
=== Installation ===&lt;br /&gt;
&lt;br /&gt;
First of all you need to install '''''R''''' onto your system.&lt;br /&gt;
&lt;br /&gt;
R and many of its addon packages are pre-built and distributed through the CRAN network of mirrors. In addition many Linux distributions prepackage R and a number of the most popular addon toolboxes.&lt;br /&gt;
&lt;br /&gt;
All the necessary functions for the GRASS 6 interface are now in packages on CRAN, so that on Linux/Unix (or Mac OSX) installing '''rgdal''' from source with PROJ4 and GDAL installed, or Windows installing from binary, the required packages are: '''sp'''; '''maptools''' (now includes spmaptools); '''rgdal''' (now includes spGDAL, spproj); '''spgrass6''' - now all on CRAN.&lt;br /&gt;
&lt;br /&gt;
==== Source packages ====&lt;br /&gt;
&lt;br /&gt;
From the R console first pick a local mirror:&lt;br /&gt;
 chooseCRANmirror()&lt;br /&gt;
&lt;br /&gt;
you can then see what it picked with&lt;br /&gt;
 options(&amp;quot;repos&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
To permanently save the mirror site add it to &amp;lt;tt&amp;gt;~/.Rprofile&amp;lt;/tt&amp;gt;. For example:&lt;br /&gt;
 options(repos=c(CRAN=&amp;quot;&amp;lt;nowiki&amp;gt;http://cran.stat.auckland.ac.nz&amp;lt;/nowiki&amp;gt;&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
and then run &amp;lt;tt&amp;gt;install.packages()&amp;lt;/tt&amp;gt; as in the Quick Start section above.&lt;br /&gt;
&lt;br /&gt;
For more information see http://cran.r-project.org/doc/manuals/R-admin.html&lt;br /&gt;
&lt;br /&gt;
==== Linux ====&lt;br /&gt;
&lt;br /&gt;
===== Debian and Ubuntu =====&lt;br /&gt;
&lt;br /&gt;
''R'' and a number of pre-build cran packages are already present in the main repositories. Start with:&lt;br /&gt;
&lt;br /&gt;
 # apt-get install r-base r-cran-vr r-cran-rodbc r-cran-xml&lt;br /&gt;
&lt;br /&gt;
Once those are installed start &amp;quot;&amp;lt;tt&amp;gt;R&amp;lt;/tt&amp;gt;&amp;quot; at the command prompt and install the libraries not packaged by the OS:&lt;br /&gt;
: ''n.b. &amp;lt;tt&amp;gt;r-cran-sp&amp;lt;/tt&amp;gt; is now shipped as an official Debian/Ubuntu package and can be installed with &amp;lt;tt&amp;gt;apt&amp;lt;/tt&amp;gt; as above''&lt;br /&gt;
&lt;br /&gt;
 install.packages(&amp;quot;sp&amp;quot;)&lt;br /&gt;
 install.packages(&amp;quot;gstat&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Debian/Lenny ships with ''R'' 2.7.1 which is too old for the modern ''rgdal'' package. So we have to fetch an old one from the archive and build it from the Linux command line:&lt;br /&gt;
 $ wget http://cran.r-project.org/src/contrib/Archive/rgdal/rgdal_0.6-24.tar.gz&lt;br /&gt;
 $ R CMD INSTALL -l /usr/local/lib/R/site-library rgdal_0.6-24.tar.gz&lt;br /&gt;
&lt;br /&gt;
If you are using a newer version of R than that you can install the ''rgdal'' CRAN package directly:&lt;br /&gt;
  install.packages(&amp;quot;rgdal&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
And finally, back inside the ''R'' session:&lt;br /&gt;
 install.packages(&amp;quot;spgrass6&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* [http://cran.r-project.org/bin/linux/debian/README Debian] and [http://cran.r-project.org/bin/linux/ubuntu/README Ubuntu] specific help is also available from the R-project website.&lt;br /&gt;
&lt;br /&gt;
You can also use the CRAN Debian package repository: (pick one; adjust distribution as needed [here &amp;quot;Debian/testing&amp;quot;])&lt;br /&gt;
 deb http://debian.cran.r-project.org/cran2deb/debian-i386 testing/&lt;br /&gt;
 deb http://debian.cran.r-project.org/cran2deb/debian-amd64 testing/&lt;br /&gt;
&lt;br /&gt;
===== RPM based =====&lt;br /&gt;
&lt;br /&gt;
* '''RedHat, Suse, Mandrake''' and similar distros: take the latest '''''R''''' RPM and install it&lt;br /&gt;
&lt;br /&gt;
==== Mac OSX ====&lt;br /&gt;
&lt;br /&gt;
Start a R session, then&lt;br /&gt;
&lt;br /&gt;
* for install.packages() you might have to rely on building packages from source code. try:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
install.packages(&amp;quot;spgrass6&amp;quot;, type=&amp;quot;source&amp;quot;, dependencies = TRUE)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Startup of GRASS from within R:'''&lt;br /&gt;
&lt;br /&gt;
First you need to find the path to the GRASS binaries: Control-click on the GRASS.app and you'll get a popup menu; select &amp;quot;Show Package Contents&amp;quot; - this opens you to the directory structure. Go to Contents-&amp;gt;MacOS which would be &amp;quot;GISBASE&amp;quot;; So, in my case, the &amp;quot;gisBase&amp;quot; parameter is &amp;quot;/HD/Applications/Grass-6.4/Contents/MacOS&amp;quot;. If you Command-click at the top of the window on the folder icon beside &amp;quot;MacOS&amp;quot; (from the line above this one), you can see the full path.&lt;br /&gt;
&lt;br /&gt;
Now we can run GRASS from within a R session:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
initGRASS(gisBase ='/Applications/GRASS/GRASS-6.4.app/Contents/MacOS', &lt;br /&gt;
          location = 'geostat2012_ll', mapset = 'user1', &lt;br /&gt;
          gisDbase = '/Users/Lars/Documents/Biologi/grassdata', override = TRUE)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== MS Windows ====&lt;br /&gt;
&lt;br /&gt;
===== Installation =====&lt;br /&gt;
Run:&lt;br /&gt;
 install.packages(&amp;quot;spgrass6&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
or install Task View 'Spatial' - Analysis of Spatial Data&lt;br /&gt;
 install.packages(&amp;quot;ctv&amp;quot;)&lt;br /&gt;
 library(&amp;quot;ctv&amp;quot;)&lt;br /&gt;
 install.views(&amp;quot;Spatial&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
===== GRASS 6: Calling R from GRASS =====&lt;br /&gt;
On Windows, the easiest way is calling  R in a running GRASS 6 session by specifying the full path to the R startup executable. Note that all &amp;quot;\&amp;quot; must be changed to &amp;quot;/&amp;quot;, furthermore quote white spaces in the path:&lt;br /&gt;
&lt;br /&gt;
   GRASS 6.4&amp;gt; C:/Users/&amp;quot;Catherine user&amp;quot;/Documents/R/R-2.15.1/bin/i386/R.exe&lt;br /&gt;
   R version 2.15.1 (2012-06-22) -- &amp;quot;Roasted Marshmallows&amp;quot;&lt;br /&gt;
   ...&lt;br /&gt;
   library(spgrass6)&lt;br /&gt;
&lt;br /&gt;
The best solution is then to add this path to the %PATH% entry in the grass64.bat-starting script.&lt;br /&gt;
&lt;br /&gt;
===== GRASS 6: calling GRASS from R =====&lt;br /&gt;
On Windows, the second easiest way is calling GRASS from R with ''initGRASS()'' from the R package spgrass6(). After GRASS has been initialized for R, you have access to GRASS commands from within R (you may need to adjust the path):&lt;br /&gt;
&lt;br /&gt;
   spgrass6()&lt;br /&gt;
   loc &amp;lt;- initGRASS(&amp;quot;C:\\Program Files\\GRASS 6.4&amp;quot;, home=tempdir())&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For an example, see http://geomorphometry.org/content/geomorphometry-r-saga-ilwis-grass&lt;br /&gt;
&lt;br /&gt;
===== GRASS 6 Usage III (taken from the grass-stats-ML) =====&lt;br /&gt;
&lt;br /&gt;
http://lists.osgeo.org/pipermail/grass-stats/2010-September/001274.html&lt;br /&gt;
&lt;br /&gt;
at the moment there is following for a Grass-R-connection implemented in WinGRASS 64&lt;br /&gt;
&lt;br /&gt;
(1) the WinGrass64-installer searches during installation for a installed R and writes - &lt;br /&gt;
if found - the R-installation-path to %PATH% in the grass64.bat-starting script&lt;br /&gt;
(see http://trac.osgeo.org/grass/browser/grass/branches/develbranch_6/mswindows/GRASS-Installer.nsi#L660)&lt;br /&gt;
&lt;br /&gt;
--&amp;gt; this means that you FIRST have to install R and then winGRASS!&lt;br /&gt;
&lt;br /&gt;
(2) you can start &amp;quot;GRASS-command-line&amp;quot; - it's a windows-command-line, not a msys-rxvt-terminal &lt;br /&gt;
(you can find this starting option under Programs -&amp;gt; GRASS64 -&amp;gt; Grass command line; but not as a desktop icon)&lt;br /&gt;
&lt;br /&gt;
with this starting option you start a Grass-session in the good old text mode. &lt;br /&gt;
if you type R in the command line, then you start R inside a GRASS-session like in Linux.&lt;br /&gt;
&lt;br /&gt;
===== GRASS 7 Usage =====&lt;br /&gt;
&lt;br /&gt;
In WinGRASS 7 ([http://grass.osgeo.org/download/software.php#g70x standalone installer]) the [http://code.google.com/p/batchfiles/ Windows batchfiles for use with R] are now integrated for a smooth WinGRASS-R-integration. The R-installation-path is dynamically loaded into PATH.&lt;br /&gt;
&lt;br /&gt;
Start WinGRASS 7, bring the WinGRASS-windows console in front and type ''R'' for opening a R-session (command line mode) inside a GRASS-session.&lt;br /&gt;
&lt;br /&gt;
[[Image:wingrass7_R_integration.png|600px|center]]&lt;br /&gt;
&lt;br /&gt;
Start WinGRASS 7, bring the WinGRASS-windows console in front and type ''RGui'' for opening a R-session (GUI mode) inside a GRASS-session.&lt;br /&gt;
&lt;br /&gt;
[[Image:wingrass7_RGui_integration.png|600px|center]]&lt;br /&gt;
&lt;br /&gt;
===== Open tickets =====&lt;br /&gt;
&lt;br /&gt;
* Ticket {{trac|1103}} (new enhancement) WinGrass64 - windows-commandline not released: a Grass-session with wxGui, command-line and R inside a Grass-session would be possible (as already does in WinGrass7)&lt;br /&gt;
* Ticket {{trac|1149}} (new enhancement) WinGrass - load R-installation-path dynamically into PATH:  Wingrass would recognize also an upgraded R-installation (which does not at the moment)&lt;br /&gt;
&lt;br /&gt;
=== Command help ===&lt;br /&gt;
&lt;br /&gt;
Start the ''R'' help browser:&lt;br /&gt;
 help.start()&lt;br /&gt;
&lt;br /&gt;
* Select '''Packages''' and then '''spgrass6'''.&lt;br /&gt;
&lt;br /&gt;
=== Running ===&lt;br /&gt;
: ''by Roger Bivand''&lt;br /&gt;
&lt;br /&gt;
The ''R'' interface for GRASS 5.4 was provided by a CRAN package called ''grass''. Changes going forward to the current GRASS 6 release meant that the interface had to be rewritten, and this provided the opportunity to adapt it to the ''sp'' CRAN package classes. Because GRASS provides the same kinds of data as ''sp'' classes handle, and relies on much of the same open source infrastructure (PROJ.4, GDAL, OGR), this step seemed sensible. Wherever possible ''spgrass6'' tries to respect the [[current region]] in GRASS to avoid handling raster data with different resolutions or extents. ''R'' is assumed to be running within GRASS:&lt;br /&gt;
&lt;br /&gt;
==== Startup ====&lt;br /&gt;
* Start GRASS. At the GRASS command line start ''R''.&lt;br /&gt;
: ''In this example we will use the sample Spearfish dataset.''&lt;br /&gt;
&lt;br /&gt;
Reset the region settings to the defaults&lt;br /&gt;
 GRASS&amp;gt; g.region -d&lt;br /&gt;
&lt;br /&gt;
Launch R from the GRASS prompt&lt;br /&gt;
 GRASS&amp;gt; R&lt;br /&gt;
&lt;br /&gt;
Load the ''spgrass6'' library:&lt;br /&gt;
 &amp;gt; library(spgrass6)&lt;br /&gt;
&lt;br /&gt;
Get the GRASS environment (mapset, region, map projection, etc.); you can display the metadata for your location by printing G:&lt;br /&gt;
 &amp;gt; G &amp;lt;- gmeta6()&lt;br /&gt;
&lt;br /&gt;
==== Reading in data ====&lt;br /&gt;
Read in two raster maps:&lt;br /&gt;
 &amp;gt; spear &amp;lt;- readRAST6(c(&amp;quot;geology&amp;quot;, &amp;quot;elevation.dem&amp;quot;),&lt;br /&gt;
 +          cat=c(TRUE, FALSE), ignore.stderr=TRUE,&lt;br /&gt;
 +          plugin=NULL)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The metadata are accessed and available, but are not (yet) used to structure the ''sp'' class objects, here a SpatialGridDataFrame object filled with data from two Spearfish layers. Here is a plot of the elevation data:&lt;br /&gt;
 &amp;gt; image(spear, attr = 2, col = terrain.colors(20))&lt;br /&gt;
&lt;br /&gt;
Add a title to the plot:&lt;br /&gt;
 &amp;gt; title(&amp;quot;Spearfish elevation&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
[[Image:R_stats_elev.png|center]]&lt;br /&gt;
&lt;br /&gt;
In addition, we can show what is going on inside the objects read into R:&lt;br /&gt;
 &amp;gt; '''str(G)'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
List of 26&lt;br /&gt;
 $ GISDBASE     : chr &amp;quot;/home/rsb/topics/grassdata&amp;quot;&lt;br /&gt;
 $ LOCATION_NAME: chr &amp;quot;spearfish57&amp;quot;&lt;br /&gt;
 $ MAPSET       : chr &amp;quot;rsb&amp;quot;&lt;br /&gt;
 $ DEBUG        : chr &amp;quot;0&amp;quot;&lt;br /&gt;
 $ GRASS_GUI    : chr &amp;quot;text&amp;quot;&lt;br /&gt;
 $ projection   : chr &amp;quot;1 (UTM)&amp;quot;&lt;br /&gt;
 $ zone         : chr &amp;quot;13&amp;quot;&lt;br /&gt;
 $ datum        : chr &amp;quot;nad27&amp;quot;&lt;br /&gt;
 $ ellipsoid    : chr &amp;quot;clark66&amp;quot;&lt;br /&gt;
 $ north        : num 4928010&lt;br /&gt;
 $ south        : num 4913700&lt;br /&gt;
 $ west         : num 589980&lt;br /&gt;
 $ east         : num 609000&lt;br /&gt;
 $ top          : num 1&lt;br /&gt;
 $ bottom       : num 0&lt;br /&gt;
 $ nsres        : num 30&lt;br /&gt;
 $ nsres3       : num 30&lt;br /&gt;
 $ ewres        : num 30&lt;br /&gt;
 $ ewres3       : num 30&lt;br /&gt;
 $ tbres        : num 1&lt;br /&gt;
 $ rows         : int 477&lt;br /&gt;
 $ rows3        : int 477&lt;br /&gt;
 $ cols         : int 634&lt;br /&gt;
 $ cols3        : int 634&lt;br /&gt;
 $ depths       : int 1&lt;br /&gt;
 $ proj4        : chr &amp;quot;+proj=utm +zone=13 +a=6378206.4 +rf=294.9786982 +no_defs +nadgrids=/home/rsb/topics/grass61/grass-6.1.cvs/etc/nad/conus&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;gt; '''summary(spear)'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object of class SpatialGridDataFrame&lt;br /&gt;
Coordinates:&lt;br /&gt;
              min     max&lt;br /&gt;
coords.x1  589980  609000&lt;br /&gt;
coords.x2 4913700 4928010&lt;br /&gt;
Is projected: TRUE &lt;br /&gt;
proj4string : [+proj=utm +zone=13 +a=6378206.4 +rf=294.9786982 +no_defs +nadgrids=/home/rsb/topics/grass61/grass-6.1.cvs/etc/nad/conus]&lt;br /&gt;
Number of points: 2&lt;br /&gt;
Grid attributes:&lt;br /&gt;
  cellcentre.offset cellsize cells.dim&lt;br /&gt;
1            589995       30       634&lt;br /&gt;
2           4913715       30       477&lt;br /&gt;
Data attributes:&lt;br /&gt;
      geology      elevation.dem  &lt;br /&gt;
 sandstone:74959   Min.   : 1066  &lt;br /&gt;
 limestone:61355   1st Qu.: 1200  &lt;br /&gt;
 shale    :46423   Median : 1316  &lt;br /&gt;
 sand     :36561   Mean   : 1354  &lt;br /&gt;
 igneous  :36534   3rd Qu.: 1488  &lt;br /&gt;
 (Other)  :37636   Max.   : 1840  &lt;br /&gt;
 NA's     : 8950   NA's   :10101  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Summarizing data ====&lt;br /&gt;
We can create a table of cell counts:&lt;br /&gt;
 &amp;gt; '''table(spear$geology)'''&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!metamorphic&lt;br /&gt;
!transition&lt;br /&gt;
!igneous&lt;br /&gt;
!sandstone&lt;br /&gt;
!limestone&lt;br /&gt;
!shale&lt;br /&gt;
!sandy shale&lt;br /&gt;
!claysand&lt;br /&gt;
!sand&lt;br /&gt;
|-&lt;br /&gt;
|11693&lt;br /&gt;
|142&lt;br /&gt;
|36534&lt;br /&gt;
|74959&lt;br /&gt;
|61355&lt;br /&gt;
|46423&lt;br /&gt;
|11266&lt;br /&gt;
|14535&lt;br /&gt;
|36561&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
And compare with the equivalent GRASS module:&lt;br /&gt;
 &amp;gt; '''execGRASS(&amp;quot;r.stats&amp;quot;, flags=c(&amp;quot;c&amp;quot;, &amp;quot;l&amp;quot;), parameters=list(input=&amp;quot;geology&amp;quot;), ignore.stderr=TRUE)'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1 metamorphic 11693&lt;br /&gt;
2 transition 142&lt;br /&gt;
3 igneous 36534&lt;br /&gt;
4 sandstone 74959&lt;br /&gt;
5 limestone 61355&lt;br /&gt;
6 shale 46423&lt;br /&gt;
7 sandy shale 11266&lt;br /&gt;
8 claysand 14535&lt;br /&gt;
9 sand 36561&lt;br /&gt;
* no data 8950&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a box plot of geologic types at different elevations:&lt;br /&gt;
 &amp;gt; '''boxplot(spear$elevation.dem ~ spear$geology, medlwd = 1)'''&lt;br /&gt;
&lt;br /&gt;
[[Image:R_stats_boxplot_geo.png|center]]&lt;br /&gt;
&lt;br /&gt;
==== Exporting data back to GRASS ====&lt;br /&gt;
Finally, a SpatialGridDataFrame object is written back to a GRASS raster map:&lt;br /&gt;
&lt;br /&gt;
First prepare some data:  (square root of elevation)&lt;br /&gt;
 &amp;gt; spear$sqdem &amp;lt;- sqrt(spear$elevation.dem)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Export data from ''R'' back into a GRASS raster map:&lt;br /&gt;
 &amp;gt; writeRAST6(spear, &amp;quot;sqdemSP&amp;quot;, zcol=&amp;quot;sqdem&amp;quot;, ignore.stderr=TRUE)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Check that it imported into GRASS ok:&lt;br /&gt;
 &amp;gt; '''execGRASS(&amp;quot;r.info&amp;quot;, parameters=list(map=&amp;quot;sqdemSP&amp;quot;))'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 +----------------------------------------------------------------------------+&lt;br /&gt;
 | Layer:    sqdemSP                        Date: Sun May 14 21:59:26 2006    |&lt;br /&gt;
 | Mapset:   rsb                            Login of Creator: rsb             |&lt;br /&gt;
 | Location: spearfish57                                                      |&lt;br /&gt;
 | DataBase: /home/rsb/topics/grassdata                                       |&lt;br /&gt;
 | Title:     ( sqdemSP )                                                     |&lt;br /&gt;
 |----------------------------------------------------------------------------|&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |   Type of Map:  raster              Number of Categories: 255              |&lt;br /&gt;
 |   Data Type:    FCELL                                                      |&lt;br /&gt;
 |   Rows:         477                                                        |&lt;br /&gt;
 |   Columns:      634                                                        |&lt;br /&gt;
 |   Total Cells:  302418                                                     |&lt;br /&gt;
 |        Projection: UTM (zone 13)                                           |&lt;br /&gt;
 |            N:    4928010    S:    4913700   Res:    30                     |&lt;br /&gt;
 |            E:     609000    W:     589980   Res:    30                     |&lt;br /&gt;
 |   Range of data:    min =  32.649654 max = 42.895222                       |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |   Data Source:                                                             |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |   Data Description:                                                        |&lt;br /&gt;
 |    generated by r.in.gdal                                                  |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 +----------------------------------------------------------------------------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Calling GRASS functionality in R batch job ===&lt;br /&gt;
&lt;br /&gt;
To call GRASS functionality within a R batch job, use the initGRASS() function to define the GRASS settings:&lt;br /&gt;
&lt;br /&gt;
    library(spgrass6)&lt;br /&gt;
    &lt;br /&gt;
    # initialisation and the use of spearfish60 data&lt;br /&gt;
    initGRASS(gisBase = &amp;quot;/usr/local/grass-6.4.1&amp;quot;, home = tempdir(), &lt;br /&gt;
              gisDbase = &amp;quot;/home/neteler/grassdata/&amp;quot;,&lt;br /&gt;
              location = &amp;quot;spearfish60&amp;quot;, mapset = &amp;quot;user1&amp;quot;, SG=&amp;quot;elevation.dem&amp;quot;,&lt;br /&gt;
              override = TRUE)&lt;br /&gt;
    &lt;br /&gt;
    system(&amp;quot;g.region -d&amp;quot;)&lt;br /&gt;
    # verify&lt;br /&gt;
    gmeta6()&lt;br /&gt;
    &lt;br /&gt;
    spear &amp;lt;- readRAST6(c(&amp;quot;geology&amp;quot;, &amp;quot;elevation.dem&amp;quot;),&lt;br /&gt;
              cat=c(TRUE, FALSE), ignore.stderr=TRUE,&lt;br /&gt;
              plugin=NULL)&lt;br /&gt;
    &lt;br /&gt;
    summary(spear$geology)&lt;br /&gt;
&lt;br /&gt;
Run this script with&lt;br /&gt;
&lt;br /&gt;
    R CMD BATCH batch.R&lt;br /&gt;
&lt;br /&gt;
The result is (shorted here):&lt;br /&gt;
&lt;br /&gt;
    cat batch.Rout&lt;br /&gt;
    &lt;br /&gt;
    R version 2.10.0 (2009-10-26)&lt;br /&gt;
    Copyright (C) 2009 The R Foundation for Statistical Computing&lt;br /&gt;
    ISBN 3-900051-07-0&lt;br /&gt;
    ...&lt;br /&gt;
    &amp;gt; library(spgrass6)&lt;br /&gt;
    Loading required package: sp&lt;br /&gt;
    Loading required package: rgdal&lt;br /&gt;
    Geospatial Data Abstraction Library extensions to R successfully loaded&lt;br /&gt;
    Loaded GDAL runtime: GDAL 1.7.2, released 2010/04/23&lt;br /&gt;
    Path to GDAL shared files: /usr/local/share/gdal&lt;br /&gt;
    Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009&lt;br /&gt;
    Path to PROJ.4 shared files: (autodetected)&lt;br /&gt;
    Loading required package: XML&lt;br /&gt;
    GRASS GIS interface loaded with GRASS version: (GRASS not running)&lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; # initialisation and the use of spearfish60 data&lt;br /&gt;
    &amp;gt; initGRASS(gisBase = &amp;quot;/usr/local/grass-6.4.1&amp;quot;, home = tempdir(), gisDbase = &amp;quot;/home/neteler/grassdata/&amp;quot;,&lt;br /&gt;
    +           location = &amp;quot;spearfish60&amp;quot;, mapset = &amp;quot;user1&amp;quot;, SG=&amp;quot;elevation.dem&amp;quot;, override = TRUE)&lt;br /&gt;
    gisdbase    /home/neteler/grassdata/ &lt;br /&gt;
    location    spearfish60 &lt;br /&gt;
    mapset      user1 &lt;br /&gt;
    rows        477 &lt;br /&gt;
    columns     634 &lt;br /&gt;
    north       4928010 &lt;br /&gt;
    south       4913700 &lt;br /&gt;
    west        589980 &lt;br /&gt;
    east        609000 &lt;br /&gt;
    nsres       30 &lt;br /&gt;
    ewres       30 &lt;br /&gt;
    projection  +proj=utm +zone=13 +a=6378206.4 +rf=294.9786982 +no_defs&lt;br /&gt;
    +nadgrids=/usr/local/grass-6.4.1/etc/nad/conus +to_meter=1.0 &lt;br /&gt;
    Warning messages:&lt;br /&gt;
    1: In dir.create(gisDbase) : '/home/neteler/grassdata' already exists&lt;br /&gt;
    2: In dir.create(loc_path) :&lt;br /&gt;
      '/home/neteler/grassdata//spearfish60' already exists&lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; system(&amp;quot;g.region -d&amp;quot;)&lt;br /&gt;
    &amp;gt; # verify&lt;br /&gt;
    &amp;gt; gmeta6()&lt;br /&gt;
    gisdbase    /home/neteler/grassdata/ &lt;br /&gt;
    location    spearfish60 &lt;br /&gt;
    mapset      user1 &lt;br /&gt;
    rows        477 &lt;br /&gt;
    columns     634 &lt;br /&gt;
    north       4928010 &lt;br /&gt;
    ...&lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; spear &amp;lt;- readRAST6(c(&amp;quot;geology&amp;quot;, &amp;quot;elevation.dem&amp;quot;),&lt;br /&gt;
    +           cat=c(TRUE, FALSE), ignore.stderr=TRUE,&lt;br /&gt;
    +           plugin=NULL)&lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; summary(spear$geology)&lt;br /&gt;
    metamorphic  transition     igneous   sandstone   limestone       shale &lt;br /&gt;
          11693         142       36534       74959       61355       46423 &lt;br /&gt;
    sandy shale    claysand        sand        NA's &lt;br /&gt;
          11266       14535       36561        8950 &lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; &lt;br /&gt;
    &amp;gt; proc.time()&lt;br /&gt;
       user  system elapsed &lt;br /&gt;
      2.891   0.492   3.412 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== GRASS Modules ===&lt;br /&gt;
&lt;br /&gt;
==== v.krige ====&lt;br /&gt;
&lt;br /&gt;
{{cmd|v.krige|version=70}} is a GRASS python script which performs kriging operations in the GRASS environment, using R functions for the back-end interpolation. It is present in GRASS 6.5svn, and further developed in GRASS 7svn. It requires a number of dependencies: '''python-rpy2''' (''needs to be &amp;quot;Rpy2&amp;quot;, &amp;quot;Rpy&amp;quot; will not do''), then the following R-CRAN packages:&lt;br /&gt;
* gstat, spgrass6 (as above)&lt;br /&gt;
* maptools&lt;br /&gt;
 install.packages(&amp;quot;maptools&amp;quot;)&lt;br /&gt;
* automap (optional), with gpclib (or rgeos)&lt;br /&gt;
 install.packages(&amp;quot;automap&amp;quot;)&lt;br /&gt;
 install.packages(&amp;quot;rgeos&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
(''merge further info from man page here'')&lt;br /&gt;
&lt;br /&gt;
=== Getting Support ===&lt;br /&gt;
&lt;br /&gt;
* Primary support for ''R'' + GRASS and the ''spgrass6'' package is through the [http://lists.osgeo.org/mailman/listinfo/grass-stats grass-stats] mailing list.&lt;br /&gt;
&lt;br /&gt;
=== See also ===&lt;br /&gt;
&lt;br /&gt;
* R. Bivand, 2007: [http://spatial.nhh.no/R/etc/FBK07 Interfacing R and OSGeo projects: status and perspectives] (Presentation with slides and scripts)&lt;br /&gt;
&lt;br /&gt;
* http://grass.ibiblio.org/statsgrass/index.php#grassR&lt;br /&gt;
&lt;br /&gt;
* Using GRASS and R: http://grass.osgeo.org/statsgrass/grass6_r_interface.html&lt;br /&gt;
&lt;br /&gt;
* Connecting R to RDBMS: http://grass.osgeo.org/statsgrass/r_and_dbms.html&lt;br /&gt;
&lt;br /&gt;
* [http://www.r-project.org R-Statistics homepage]&lt;br /&gt;
&lt;br /&gt;
* [http://r-spatial.sourceforge.net/ R-spatial main web page]&lt;br /&gt;
&lt;br /&gt;
* [http://geodacenter.asu.edu/r-spatial-projects R Spatial Projects at ASU]&lt;br /&gt;
&lt;br /&gt;
* Neural Networks with GRASS and R (posted by Markus Neteler on the grass-user mailing list) http://www.uam.es/proyectosinv/Mclim/pdf/MBenito_EcoMod.pdf&lt;br /&gt;
&lt;br /&gt;
* A detailed example on the use of GRASS and R, with spearfish data: http://casoilresource.lawr.ucdavis.edu/drupal/node/438&lt;br /&gt;
&lt;br /&gt;
* Using R and GRASS with cygwin: It is possible to use Rterm inside the GRASS shell in cygwin, just as in Unix/Linux or OSX. You should not, however, start Rterm from a cygwin xterm, because Rterm is not expecting to be run in an xterm under Windows, and loses its input. If you use the regular cygwin bash shell, but need to start display windows, start X from within GRASS with startx &amp;amp;, and then start Rterm in the same cygwin shell, not in the xterm.&lt;br /&gt;
&lt;br /&gt;
* [http://r-spatial.sourceforge.net/ Spatial data in R] (&amp;lt;code&amp;gt;sp&amp;lt;/code&amp;gt;) is a '''''R''''' library that provides classes and methods for spatial data (points, lines, polygons, grids), and to new or existing spatial statistics '''''R''''' packages that use sp, depend on sp, or will become dependent on &amp;lt;code&amp;gt;sp&amp;lt;/code&amp;gt;, such as &amp;lt;code&amp;gt;maptools&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;rgdal&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;splancs&amp;lt;/code&amp;gt;, '''&amp;lt;code&amp;gt;spgrass6&amp;lt;/code&amp;gt;''', &amp;lt;code&amp;gt;gstat&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;spgwr&amp;lt;/code&amp;gt; and many others.&lt;br /&gt;
&lt;br /&gt;
* [http://rpy.sourceforge.net/ RPy] - Python interface to the R Programming Language&lt;br /&gt;
&lt;br /&gt;
=== Workshop material ===&lt;br /&gt;
&lt;br /&gt;
* M. Neteler and M. Metz, 2011: ''Introduction to GRASS GIS''. GEOSTAT 2011 Landau. [http://geostat-course.org/Topic_NetelerMetz_2011 Download workshop material] '''(includes a R session)'''&lt;br /&gt;
&lt;br /&gt;
=== Articles ===&lt;br /&gt;
&lt;br /&gt;
* [http://grass.osgeo.org/newsletter/grassnews3.html GRASS News vol.3], June 2005 (R. Bivand. Interfacing GRASS 6 and R. ''GRASS Newsletter'', 3:11-16, June 2005. ISSN 1614-8746).&lt;br /&gt;
* [http://www.osgeo.org/journal OSGeo Journal] vol. 1 May 2007 (R. Bivand. Using the R— GRASS interface. ''OSGeo Journal'', 1:31-33, May 2007. ISSN 1614-8746).&lt;br /&gt;
* [http://www.grassbook.org GRASS Book, last chapter]&lt;br /&gt;
&lt;br /&gt;
[[Category:Installation]]&lt;br /&gt;
[[Category:FAQ]]&lt;br /&gt;
[[Category:Linking to other languages]]&lt;br /&gt;
[[Category:R]]&lt;/div&gt;</summary>
		<author><name>⚠️Dalby</name></author>
	</entry>
</feed>