<?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%8FMichaelTiemann</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%8FMichaelTiemann"/>
	<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/wiki/Special:Contributions/%E2%9A%A0%EF%B8%8FMichaelTiemann"/>
	<updated>2026-05-31T07:20:52Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=R_statistics/rgrass&amp;diff=21984</id>
		<title>R statistics/rgrass</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=R_statistics/rgrass&amp;diff=21984"/>
		<updated>2015-09-12T15:15:44Z</updated>

		<summary type="html">&lt;p&gt;⚠️MichaelTiemann: /* R within GRASS */ Added note about loading rgdal library&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''This page refers to the usage of R within a GRASS GIS 7 session and the use of GRASS GIS 7 within an R session.''' (see also [[R_statistics/spgrass6]])&lt;br /&gt;
&lt;br /&gt;
== Terminology ==&lt;br /&gt;
&lt;br /&gt;
Using R in conjunction with GRASS GIS can have two meanings:&lt;br /&gt;
&lt;br /&gt;
* Using '''R within GRASS GIS session''', i.e. you start R (or RStudio) from the GRASS GIS command line. You may like this variant if you are primarily a GIS user.&lt;br /&gt;
* Using '''GRASS GIS within a R session''', i.e. you connect to a GRASS GIS location/mapset from within R (or RStudio). You may like this variant if you are primarily a R user.&lt;br /&gt;
&lt;br /&gt;
Keep in mind:&lt;br /&gt;
* if you already have a GRASS location/mapset, use R inside GRASS and do not use initGRASS();&lt;br /&gt;
* if the GRASS location/mapset is only a throwaway one, use initGRASS() and run GRASS inside R.&lt;br /&gt;
&lt;br /&gt;
'''References''': see &amp;quot;Overview&amp;quot; in [[R_statistics]]&lt;br /&gt;
&lt;br /&gt;
== Installation ==&lt;br /&gt;
&lt;br /&gt;
See [[R_statistics/Installation]]&lt;br /&gt;
&lt;br /&gt;
== R within GRASS ==&lt;br /&gt;
&lt;br /&gt;
Using '''R within GRASS GIS session''', i.e. you start R (or RStudio) from the GRASS GIS command line.&lt;br /&gt;
&lt;br /&gt;
=== Startup ===&lt;br /&gt;
&lt;br /&gt;
* First start a GRASS GIS session. Then, at the GRASS command line start ''R'' (for a 'rstudio' session, see below)&lt;br /&gt;
: ''In this example we will use the [http://grass.osgeo.org/download/sample-data/ North Carolina sample dataset].''&lt;br /&gt;
&lt;br /&gt;
Reset the region settings to the defaults&lt;br /&gt;
 GRASS 7.0.1svn (nc_spm_08_grass7):~ &amp;gt; g.region -d&lt;br /&gt;
&lt;br /&gt;
Launch R from the GRASS prompt&lt;br /&gt;
 GRASS 7.0.1svn (nc_spm_08_grass7):~ &amp;gt; R&lt;br /&gt;
&lt;br /&gt;
Load the ''rgrass7'' library:&lt;br /&gt;
 &amp;gt; library(rgrass7)&lt;br /&gt;
&lt;br /&gt;
If you plan to follow the example using the North Carolina sample dataset, load the ''rgdal'' library:&lt;br /&gt;
 &amp;gt; library(rgdal)&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;- gmeta()&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gisdbase    /home/neteler/grassdata &lt;br /&gt;
location    nc_spm_08_grass7 &lt;br /&gt;
mapset      user1 &lt;br /&gt;
rows        620 &lt;br /&gt;
columns     1630 &lt;br /&gt;
north       320000 &lt;br /&gt;
south       10000 &lt;br /&gt;
west        120000 &lt;br /&gt;
east        935000 &lt;br /&gt;
nsres       500 &lt;br /&gt;
ewres       500 &lt;br /&gt;
projection  +proj=lcc +lat_1=36.16666666666666 +lat_2=34.33333333333334&lt;br /&gt;
+lat_0=33.75 +lon_0=-79 +x_0=609601.22 +y_0=0 +no_defs +a=6378137&lt;br /&gt;
+rf=298.257222101 +towgs84=0.000,0.000,0.000 +to_meter=1 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Listing of existing maps ===&lt;br /&gt;
&lt;br /&gt;
List available vector maps:&lt;br /&gt;
 &amp;gt; execGRASS(&amp;quot;g.list&amp;quot;, parameters = list(type = &amp;quot;vector&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
List selected vector maps (wildcard):&lt;br /&gt;
 &amp;gt; execGRASS(&amp;quot;g.list&amp;quot;, parameters = list(type = &amp;quot;vector&amp;quot;, pattern = &amp;quot;precip*&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
Save selected vector maps into R vector:&lt;br /&gt;
 &amp;gt; my_vmaps &amp;lt;- execGRASS(&amp;quot;g.list&amp;quot;, parameters = list(type = &amp;quot;vector&amp;quot;, pattern = &amp;quot;precip*&amp;quot;))&lt;br /&gt;
 &amp;gt; attributes(my_vmaps)&lt;br /&gt;
 &amp;gt; attributes(my_vmaps)$resOut&lt;br /&gt;
&lt;br /&gt;
List available raster maps:&lt;br /&gt;
 &amp;gt; execGRASS(&amp;quot;g.list&amp;quot;, parameters = list(type = &amp;quot;raster&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
List selected raster maps (wildcard):&lt;br /&gt;
 &amp;gt; execGRASS(&amp;quot;g.list&amp;quot;, parameters = list(type = &amp;quot;raster&amp;quot;, pattern = &amp;quot;lsat7_2002*&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
=== Reading in data from GRASS ===&lt;br /&gt;
&lt;br /&gt;
==== Example 1 ====&lt;br /&gt;
&lt;br /&gt;
Read in two GRASS raster maps (the maps &amp;quot;geology_30m&amp;quot; and &amp;quot;elevation&amp;quot; from the North Carolina sample data location) into the R current session as a new object &amp;quot;ncdata&amp;quot; (containing then two SpatialGridDataFrames as well as metadata):&lt;br /&gt;
 # the cat parameter indicates which raster values to be returned as factors&lt;br /&gt;
 # - geology_30m is a categorical map (i.e., it comes with classes)&lt;br /&gt;
 # - elevation is a continuous surface&lt;br /&gt;
 &amp;gt; ncdata &amp;lt;- readRAST(c(&amp;quot;geology_30m&amp;quot;, &amp;quot;elevation&amp;quot;), cat=c(TRUE, FALSE))&lt;br /&gt;
&lt;br /&gt;
(A warning may appear since in the &amp;quot;geology_30m&amp;quot; map two labels are not unique - as found in the original data.)&lt;br /&gt;
&lt;br /&gt;
We can verify the new R object:&lt;br /&gt;
 &amp;gt; str(ncdata)&lt;br /&gt;
 Formal class 'SpatialGridDataFrame' [package &amp;quot;sp&amp;quot;] with 4 slots&lt;br /&gt;
  ..@ data       :'data.frame':	16616 obs. of  2 variables:&lt;br /&gt;
&lt;br /&gt;
and also check the data structure in more detail:&lt;br /&gt;
 &amp;gt; str(ncdata@data)&lt;br /&gt;
 'data.frame':	16616 obs. of  2 variables:&lt;br /&gt;
  $ geology_30m: Factor w/ 12 levels &amp;quot;CZfg_217&amp;quot;,&amp;quot;CZlg_262&amp;quot;,..: NA NA NA NA NA NA NA NA NA NA ...&lt;br /&gt;
  $ elevation  : num  NA NA NA NA NA NA NA NA NA NA ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The metadata are now accessed and available, but are not (yet) used to structure the ''sp'' class objects, in this case a SpatialGridDataFrame object filled with data from two North Carolina layers. Here is a plot of the elevation data:&lt;br /&gt;
 &amp;gt; image(ncdata, 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;North Carolina elevation&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
[[Image:ncdata.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;
 $ DEBUG        : chr &amp;quot;0&amp;quot;&lt;br /&gt;
 $ LOCATION_NAME: chr &amp;quot;nc_spm_08_grass7&amp;quot;&lt;br /&gt;
 $ GISDBASE     : chr &amp;quot;/home/veroandreo/grassdata&amp;quot;&lt;br /&gt;
 $ MAPSET       : chr &amp;quot;PERMANENT&amp;quot;&lt;br /&gt;
 $ GUI          : chr &amp;quot;wxpython&amp;quot;&lt;br /&gt;
 $ projection   : chr &amp;quot;99&amp;quot;&lt;br /&gt;
 $ zone         : chr &amp;quot;0&amp;quot;&lt;br /&gt;
 $ n            : num 228500&lt;br /&gt;
 $ s            : num 215000&lt;br /&gt;
 $ w            : num 630000&lt;br /&gt;
 $ e            : num 645000&lt;br /&gt;
 $ t            : num 1&lt;br /&gt;
 $ b            : num 0&lt;br /&gt;
 $ nsres        : num 27.5&lt;br /&gt;
 $ nsres3       : num 10&lt;br /&gt;
 $ ewres        : num 37.5&lt;br /&gt;
 $ ewres3       : num 10&lt;br /&gt;
 $ tbres        : num 1&lt;br /&gt;
 $ rows         : int 491&lt;br /&gt;
 $ rows3        : int 1350&lt;br /&gt;
 $ cols         : int 400&lt;br /&gt;
 $ cols3        : int 1500&lt;br /&gt;
 $ depths       : int 1&lt;br /&gt;
 $ cells        : chr &amp;quot;196400&amp;quot;&lt;br /&gt;
 $ cells3       : chr &amp;quot;2025000&amp;quot;&lt;br /&gt;
 $ proj4        : chr &amp;quot;+proj=lcc +lat_1=36.16666666666666 +lat_2=34.33333333333334 +lat_0=33.75 +lon_0=-79 +x_0=609601.22 +y_0=0 +no_defs +a=6378137 +&amp;quot;| __truncated__&lt;br /&gt;
 - attr(*, &amp;quot;class&amp;quot;)= chr &amp;quot;gmeta&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;gt; summary(ncdata)&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;
[1,] 630000 645000&lt;br /&gt;
[2,] 215000 228500&lt;br /&gt;
Is projected: TRUE &lt;br /&gt;
proj4string :&lt;br /&gt;
[+proj=lcc +lat_1=36.16666666666666 +lat_2=34.33333333333334&lt;br /&gt;
+lat_0=33.75 +lon_0=-79 +x_0=609601.22 +y_0=0 +no_defs +a=6378137&lt;br /&gt;
+rf=298.257222101 +towgs84=0.000,0.000,0.000 +to_meter=1]&lt;br /&gt;
Grid attributes:&lt;br /&gt;
  cellcentre.offset cellsize cells.dim&lt;br /&gt;
1          630018.8 37.50000       400&lt;br /&gt;
2          215013.7 27.49491       491&lt;br /&gt;
Data attributes:&lt;br /&gt;
   geology_30m      elevation     &lt;br /&gt;
 CZfg_217:70381   Min.   : 55.92  &lt;br /&gt;
 CZig_270:66861   1st Qu.: 94.78  &lt;br /&gt;
 CZbg_405:24561   Median :108.88  &lt;br /&gt;
 CZlg_262:19287   Mean   :110.38  &lt;br /&gt;
 CZam_862: 6017   3rd Qu.:126.78  &lt;br /&gt;
 CZbg_910: 4351   Max.   :156.25  &lt;br /&gt;
 (Other) : 4942                   &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Example 2 ====&lt;br /&gt;
&lt;br /&gt;
Here an example for a single map transfer from GRASS GIS to R:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
library(rgrass7)&lt;br /&gt;
execGRASS(&amp;quot;g.region&amp;quot;, raster = &amp;quot;elevation&amp;quot;, flags = &amp;quot;p&amp;quot;)&lt;br /&gt;
ncdata &amp;lt;- readRAST(&amp;quot;elevation&amp;quot;, cat=FALSE)&lt;br /&gt;
summary(ncdata)&lt;br /&gt;
spplot(ncdata, col = terrain.colors(20))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Summarizing data ===&lt;br /&gt;
&lt;br /&gt;
We can create a table of cell counts:&lt;br /&gt;
 &amp;gt; table(ncdata$geology_30m)&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
!CZfg_217 &lt;br /&gt;
!CZlg_262 &lt;br /&gt;
!CZig_270 &lt;br /&gt;
!CZbg_405 &lt;br /&gt;
!CZve_583 &lt;br /&gt;
!CZam_720  &lt;br /&gt;
!CZg_766 &lt;br /&gt;
!CZam_862 &lt;br /&gt;
!CZbg_910   &lt;br /&gt;
!Km_921 &lt;br /&gt;
!CZbg_945 &lt;br /&gt;
!CZam_946 &lt;br /&gt;
!CZam_948&lt;br /&gt;
|-&lt;br /&gt;
|70381&lt;br /&gt;
|19287&lt;br /&gt;
|66861    &lt;br /&gt;
|24561     &lt;br /&gt;
|2089      &lt;br /&gt;
|467      &lt;br /&gt;
|691     &lt;br /&gt;
|6017 &lt;br /&gt;
|4351&lt;br /&gt;
|1211        &lt;br /&gt;
|1      &lt;br /&gt;
|398       &lt;br /&gt;
|85&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_30m&amp;quot;), ignore.stderr=TRUE)&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
217 CZfg 70381&lt;br /&gt;
262 CZlg 19287&lt;br /&gt;
270 CZig 66861&lt;br /&gt;
405 CZbg 24561&lt;br /&gt;
583 CZve 2089&lt;br /&gt;
720 CZam 467&lt;br /&gt;
766 CZg 691&lt;br /&gt;
862 CZam 6017&lt;br /&gt;
910 CZbg 4351&lt;br /&gt;
921 Km 1211&lt;br /&gt;
945 CZbg 1&lt;br /&gt;
946 CZam 398&lt;br /&gt;
948 CZam 85&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a box plot of geologic types at different elevations:&lt;br /&gt;
 &amp;gt; boxplot(ncdata$elevation ~ ncdata$geology_30m, medlwd = 1)&lt;br /&gt;
&lt;br /&gt;
[[Image:boxplot_geo.png|center]]&lt;br /&gt;
&lt;br /&gt;
=== Querying maps ===&lt;br /&gt;
&lt;br /&gt;
Sometimes you may want to query GRASS GIS maps from your R session.&lt;br /&gt;
&lt;br /&gt;
As an example, here the transfer of raster pixel values at the position of vector points:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# set the computational region first to the raster map:&lt;br /&gt;
&amp;gt; execGRASS(&amp;quot;g.region&amp;quot;, raster = &amp;quot;elev_state_500m&amp;quot;, flags = &amp;quot;p&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# query raster map at vector points, transfer result into R&lt;br /&gt;
&amp;gt; goutput &amp;lt;- execGRASS(&amp;quot;r.what&amp;quot;, map=&amp;quot;elev_state_500m&amp;quot;, points=&amp;quot;geodetic_pts&amp;quot;, separator=&amp;quot;,&amp;quot;, intern=TRUE)&lt;br /&gt;
&amp;gt; str(goutput)&lt;br /&gt;
 chr [1:29939] &amp;quot;571530.81289275,265739.968425953,,187.8082200648&amp;quot; ...&lt;br /&gt;
&lt;br /&gt;
# parse it&lt;br /&gt;
&amp;gt; con &amp;lt;- textConnection(goutput)&lt;br /&gt;
&amp;gt; go1 &amp;lt;- read.csv(con, header=FALSE)&lt;br /&gt;
&amp;gt; str(go1)&lt;br /&gt;
'data.frame':	29939 obs. of  4 variables:&lt;br /&gt;
 $ V1: num  571531 571359 571976 572391 573011 ...&lt;br /&gt;
 $ V2: num  265740 265987 267049 267513 269615 ...&lt;br /&gt;
 $ V3: logi  NA NA NA NA NA NA ...&lt;br /&gt;
 $ V4: Factor w/ 22738 levels &amp;quot;-0.0048115728&amp;quot;,..: 6859 6642 6749 6411 6356 6904 7506 7224 6908 7167 ...&lt;br /&gt;
&amp;gt; summary(go1)&lt;br /&gt;
       V1               V2            V3                V4       &lt;br /&gt;
 Min.   :121862   Min.   :  7991   Mode:logical   0      :  723  &lt;br /&gt;
 1st Qu.:462706   1st Qu.:162508   NA's:29939     *      :  293  &lt;br /&gt;
 Median :610328   Median :204502                  0.3048 :   47  &lt;br /&gt;
 Mean   :588514   Mean   :200038                  0.6096 :   44  &lt;br /&gt;
 3rd Qu.:717610   3rd Qu.:247633                  1.524  :   42  &lt;br /&gt;
 Max.   :946743   Max.   :327186                  0.9144 :   23  &lt;br /&gt;
                                                  (Other):28767  &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Exporting data back to GRASS ===&lt;br /&gt;
&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; ncdata$sqdem &amp;lt;- sqrt(ncdata$elevation)&lt;br /&gt;
&lt;br /&gt;
Export data from ''R'' back into a GRASS raster map:&lt;br /&gt;
 &amp;gt; writeRAST(ncdata, &amp;quot;sqdemNC&amp;quot;, zcol=&amp;quot;sqdem&amp;quot;, ignore.stderr=TRUE)&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;sqdemNC&amp;quot;))&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 +----------------------------------------------------------------------------+&lt;br /&gt;
 | Map:      sqdemNC                        Date: Sun Jul 19 13:06:34 2015    |&lt;br /&gt;
 | Mapset:   PERMANENT                      Login of Creator: veroandreo      |&lt;br /&gt;
 | Location: nc_spm_08_grass7                                                 |&lt;br /&gt;
 | DataBase: /home/veroandreo/grassdata                                       |&lt;br /&gt;
 | Title:     ( sqdemNC )                                                     |&lt;br /&gt;
 | Timestamp: none                                                            |&lt;br /&gt;
 |----------------------------------------------------------------------------|&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |   Type of Map:  raster               Number of Categories: 0               |&lt;br /&gt;
 |   Data Type:    DCELL                                                      |&lt;br /&gt;
 |   Rows:         491                                                        |&lt;br /&gt;
 |   Columns:      400                                                        |&lt;br /&gt;
 |   Total Cells:  196400                                                     |&lt;br /&gt;
 |        Projection: Lambert Conformal Conic                                 |&lt;br /&gt;
 |            N:     228500    S: 215000.0002   Res: 27.49490794              |&lt;br /&gt;
 |            E:     645000    W:     630000   Res:  37.5                     |&lt;br /&gt;
 |   Range of data:    min = 7.47818253045085  max = 12.5000787351036         |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |   Data Description:                                                        |&lt;br /&gt;
 |    generated by r.in.bin                                                   |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 |   Comments:                                                                |&lt;br /&gt;
 |                                                                            |&lt;br /&gt;
 +----------------------------------------------------------------------------+&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Using RStudio in a GRASS GIS session ===&lt;br /&gt;
&lt;br /&gt;
If you are most used to run R through RStudio, but still want to have all GRASS data available for performing&lt;br /&gt;
any analyses without loosing the possibility of still using GRASS command line, you can run:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GRASS&amp;gt; rstudio &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or, if you already are working on a certain RStudio project:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
GRASS&amp;gt; rstudio /path/to/project/folder/ &amp;amp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, you load rgrass7 library in your RStudio project&lt;br /&gt;
&lt;br /&gt;
 &amp;gt; library(rgrass7) &lt;br /&gt;
&lt;br /&gt;
and you are ready to go.&lt;br /&gt;
&lt;br /&gt;
[[File:Grass7_rstudio.png|thumb|500px|center|RStudio used in GRASS GIS 7 session]]&lt;br /&gt;
&lt;br /&gt;
== GRASS within R ==&lt;br /&gt;
&lt;br /&gt;
Using '''GRASS GIS within a R session''', i.e. you connect to a GRASS GIS location/mapset from within R (or RStudio).&lt;br /&gt;
&lt;br /&gt;
=== Startup ===&lt;br /&gt;
&lt;br /&gt;
In the first place, find out the path to the GRASS GIS library. This can be easily done with the following command (still outside of R; or through a system() call inside of R):&lt;br /&gt;
&lt;br /&gt;
  # OSGeo4W users: nothing to do&lt;br /&gt;
 &lt;br /&gt;
  # Linux, Mac OSX users:&lt;br /&gt;
  grass70 --config path&lt;br /&gt;
&lt;br /&gt;
It may report something like:&lt;br /&gt;
  /usr/local/grass-7.0.1&lt;br /&gt;
&lt;br /&gt;
To call GRASS GIS 7 functionality from R, start R and use the initGRASS() function to define the GRASS settings:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
## MS-Windows users:&lt;br /&gt;
library(rgrass7)&lt;br /&gt;
# initialisation and the use of North Carolina sample dataset&lt;br /&gt;
initGRASS(gisBase = &amp;quot;C:/OSGeo4W/apps/grass/grass-7.1.svn&amp;quot;,&lt;br /&gt;
         gisDbase = &amp;quot;C:/Users/marissa/GRASSdata/&amp;quot;,&lt;br /&gt;
         location = &amp;quot;nc_spm_08_grass7&amp;quot;, mapset = &amp;quot;user1&amp;quot;, SG=&amp;quot;elevation&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
## Linux, Mac OSX users:&lt;br /&gt;
library(rgrass7)&lt;br /&gt;
# initialisation and the use of North Carolina sample dataset&lt;br /&gt;
initGRASS(gisBase = &amp;quot;/usr/local/grass-7.0.1&amp;quot;, home = tempdir(), &lt;br /&gt;
          gisDbase = &amp;quot;/home/veroandreo/grassdata/&amp;quot;,&lt;br /&gt;
          location = &amp;quot;nc_spm_08_grass7&amp;quot;, mapset = &amp;quot;user1&amp;quot;, SG=&amp;quot;elevation&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: the optional SG parameter is a 'SpatialGrid' object to define the ‘DEFAULT_WIND’ of the temporary location.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# set computational region to default (optional)&lt;br /&gt;
system(&amp;quot;g.region -dp&amp;quot;)&lt;br /&gt;
# verify metadata&lt;br /&gt;
gmeta()&lt;br /&gt;
&lt;br /&gt;
# get two raster maps into R space&lt;br /&gt;
ncdata &amp;lt;- readRAST(c(&amp;quot;geology_30m&amp;quot;, &amp;quot;elevation&amp;quot;), cat=c(TRUE, FALSE))&lt;br /&gt;
&lt;br /&gt;
# calculate object summaries&lt;br /&gt;
summary(ncdata$geology_30m)&lt;br /&gt;
 CZfg_217 CZlg_262 CZig_270 CZbg_405 CZve_583 CZam_720  CZg_766 CZam_862 &lt;br /&gt;
     292       78      277      102        8        1        2       25 &lt;br /&gt;
 CZbg_910   Km_921 CZam_946     NA's &lt;br /&gt;
      18        5        2  1009790 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== R in GRASS in batch mode ==&lt;br /&gt;
&lt;br /&gt;
Run the following script with&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
R CMD BATCH batch.R&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
library(rgrass7)&lt;br /&gt;
# initialisation and the use of north carolina dataset&lt;br /&gt;
initGRASS(gisBase = &amp;quot;/home/veroandreo/software/grass-7.0.svn/dist.x86_64-unknown-linux-gnu&amp;quot;, &lt;br /&gt;
          home = tempdir(), &lt;br /&gt;
          gisDbase = &amp;quot;/home/veroandreo/grassdata/&amp;quot;,&lt;br /&gt;
          location = &amp;quot;nc_spm_08_grass7&amp;quot;, mapset = &amp;quot;user1&amp;quot;, SG=&amp;quot;elevation&amp;quot;,&lt;br /&gt;
          override = TRUE)&lt;br /&gt;
# set region to default&lt;br /&gt;
system(&amp;quot;g.region -dp&amp;quot;)&lt;br /&gt;
# verify&lt;br /&gt;
gmeta()&lt;br /&gt;
# read data into R&lt;br /&gt;
ncdata &amp;lt;- readRAST(c(&amp;quot;geology_30m&amp;quot;, &amp;quot;elevation&amp;quot;), cat=c(TRUE, FALSE))&lt;br /&gt;
# summary of geology map&lt;br /&gt;
summary(ncdata$geology_30m)&lt;br /&gt;
proc.time()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&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 3.2.1 (2015-06-18) -- &amp;quot;World-Famous Astronaut&amp;quot;&lt;br /&gt;
    Copyright (C) 2015 The R Foundation for Statistical Computing&lt;br /&gt;
    Platform: x86_64-redhat-linux-gnu (64-bit)&lt;br /&gt;
    ...&lt;br /&gt;
    &amp;gt; library(rgrass7)&lt;br /&gt;
    Loading required package: sp&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; # initialisation and the use of north carolina dataset&lt;br /&gt;
    &amp;gt; initGRASS(gisBase = &amp;quot;/home/veroandreo/software/grass-7.0.svn/dist.x86_64-unknown-linux-gnu&amp;quot;, home = tempdir(), &lt;br /&gt;
    +           gisDbase = &amp;quot;/home/veroandreo/grassdata/&amp;quot;,&lt;br /&gt;
    +           location = &amp;quot;nc_spm_08_grass7&amp;quot;, mapset = &amp;quot;user1&amp;quot;, SG=&amp;quot;elevation&amp;quot;,&lt;br /&gt;
    +           override = TRUE)&lt;br /&gt;
    gisdbase    /home/veroandreo/grassdata/ &lt;br /&gt;
    location    nc_spm_08_grass7 &lt;br /&gt;
    mapset      user1 &lt;br /&gt;
    rows        620 &lt;br /&gt;
    columns     1630 &lt;br /&gt;
    north       320000 &lt;br /&gt;
    south       10000 &lt;br /&gt;
    west        120000 &lt;br /&gt;
    east        935000 &lt;br /&gt;
    nsres       500 &lt;br /&gt;
    ewres       500 &lt;br /&gt;
    projection  +proj=lcc +lat_1=36.16666666666666 +lat_2=34.33333333333334&lt;br /&gt;
    +lat_0=33.75 +lon_0=-79 +x_0=609601.22 +y_0=0 +no_defs +a=6378137&lt;br /&gt;
    +rf=298.257222101 +towgs84=0.000,0.000,0.000 +to_meter=1 &lt;br /&gt;
    &lt;br /&gt;
    &amp;gt; system(&amp;quot;g.region -dp&amp;quot;)&lt;br /&gt;
    projection: 99 (Lambert Conformal Conic)&lt;br /&gt;
    zone:       0&lt;br /&gt;
    datum:      nad83&lt;br /&gt;
    ellipsoid:  a=6378137 es=0.006694380022900787&lt;br /&gt;
    north:      320000&lt;br /&gt;
    south:      10000&lt;br /&gt;
    west:       120000&lt;br /&gt;
    east:       935000&lt;br /&gt;
    nsres:      500&lt;br /&gt;
    ewres:      500&lt;br /&gt;
    rows:       620&lt;br /&gt;
    cols:       1630&lt;br /&gt;
    cells:      1010600&lt;br /&gt;
    &amp;gt; gmeta()&lt;br /&gt;
    gisdbase    /home/veroandreo/grassdata/ &lt;br /&gt;
    location    nc_spm_08_grass7 &lt;br /&gt;
    mapset      user1 &lt;br /&gt;
    rows        620 &lt;br /&gt;
    columns     1630 &lt;br /&gt;
    north       320000 &lt;br /&gt;
    south       10000 &lt;br /&gt;
    ...&lt;br /&gt;
    &amp;gt; ncdata &amp;lt;- readRAST(c(&amp;quot;geology_30m&amp;quot;, &amp;quot;elevation&amp;quot;), cat=c(TRUE, FALSE))&lt;br /&gt;
    ...&lt;br /&gt;
    &amp;gt; summary(ncdata$geology_30m)&lt;br /&gt;
    CZfg_217 CZlg_262 CZig_270 CZbg_405 CZve_583 CZam_720  CZg_766 CZam_862 &lt;br /&gt;
         292       78      277      102        8        1        2       25 &lt;br /&gt;
    CZbg_910   Km_921 CZam_946     NA's &lt;br /&gt;
          18        5        2  1009790 &lt;br /&gt;
    &amp;gt; proc.time()&lt;br /&gt;
       user  system elapsed &lt;br /&gt;
      8.061   2.016  10.048 &lt;br /&gt;
&lt;br /&gt;
== Troubleshooting ==&lt;br /&gt;
&lt;br /&gt;
=== Running out of disk space ===&lt;br /&gt;
&lt;br /&gt;
Linux: A common issue is that /tmp/ is used for temporary files albeit being often a small partition. To change that to a larger directory, you may add to your &amp;lt;tt&amp;gt;$HOME/.bashrc&amp;lt;/tt&amp;gt; the entry:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
# change TMP directory of R (note: of course also another directory than suggested here is fine)&lt;br /&gt;
mkdir -p $HOME/tmp&lt;br /&gt;
export TMP=$HOME/tmp&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The drawback is that on modern Linux systems the /tmp/ is a fast RAM disk (based on tempfs) while HOME directories are often on slower spinning disks (unless you have a SSD drive). At least you no longer run out of disk space easily.&lt;br /&gt;
&lt;br /&gt;
[[Category:FAQ]]&lt;br /&gt;
[[Category:Linking to other languages]]&lt;br /&gt;
[[Category:R]]&lt;br /&gt;
[[Category:Statistics]]&lt;/div&gt;</summary>
		<author><name>⚠️MichaelTiemann</name></author>
	</entry>
</feed>