[Qgis-user] R in QGIS on OSX using Kyngchaos build

gene martin.laloux at gmail.com
Sun Mar 6 00:55:51 PST 2016


Processing has nothing to do with rpy2. it uses  subprocess
<https://docs.python.org/2/library/subprocess.html>   to execute the
original R script

The explication is in the
/Applications/QGIS.app/Contents/Resources/python/plugins/processing/algs/r/RUtils.py
script

1) RUtils first create a temporary file named
/Users/.../.qgis2/processing/processing_script.r with the desired R commands
(def createRScriptFromRCommands(commands):, line 73)

Example of the content of a processing_script.r file 

    options("repos"="http://cran.at.r-project.org/")
    tryCatch(find.package("rgdal"), error=function(e)
install.packages("rgdal", dependencies=TRUE))
    tryCatch(find.package("raster"), error=function(e)
install.packages("raster", dependencies=TRUE))
    library("raster")
    library("rgdal")
    Layer =
readOGR("/Users/Shared/telechargement/04_01_16/valuerelation_testproject",layer="testo2")
    Field="num"
    Summary_statistics<-data.frame(rbind(sum(Layer[[Field]]),
    length(Layer[[Field]]),
    length(unique(Layer[[Field]])),
    min(Layer[[Field]]),
    max(Layer[[Field]]),
    max(Layer[[Field]])-min(Layer[[Field]]),
    mean(Layer[[Field]]),
   
median(Layer[[Field]]),sd(Layer[[Field]])),row.names=c("Sum:","Count:","Unique
values:","Minimum value:","Maximum value:","Range:","Mean value:","Median
value:","Standard deviation:"))
    colnames(Summary_statistics)<-c(Field)
    Summary_statistics

2) it execute the script with subprocess (def executeRAlgorithm:, line 88). 

      command = ["R", "CMD","BATCH", "--vanilla",  
RUtils.getRScriptFilename(),RUtils.getConsoleOutputFilename()]
      proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,
stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True)

3) If you want, you can also use R directly to execute the script 

     source("processing_script.r")

Conclusions

a) you need access to R in command line (Terminal.app) 

   export R_HOME=/Library/Frameworks/R.framework/Versions/Current/Resources/
in the .bash_profile file

b) you need to install all the R packages independently of QGIS
      install.packages('rgdal')
      install.package('raster') 
      .....




--
View this message in context: http://osgeo-org.1560.x6.nabble.com/R-in-QGIS-on-OSX-using-Kyngchaos-build-tp5254567p5254644.html
Sent from the Quantum GIS - User mailing list archive at Nabble.com.



More information about the Qgis-user mailing list