Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


R example moving window script (focal convolution matrix) using the raster package #463

merged 0 commits into from

5 participants


The creation of Help files would be important, but it is somehow not working properly yet.
Maybe I can try to put my hands on this issue soon.


the script is great, but have you tested that the stop() call works and doesn't break QGIS? interrupting the QGIS/SEXTANTE flow with an external dialog might cause strange things, I am afraid. If so, maybe I could implement some way of communicating from R to SEXTANTE, so R can log warnings in SEXTANTE, and that can replace your solution.


And Ok I'll check the behavior on failure tomorrow, but I think SEXTANTE has to deal with such situations soner or later... As you are strating R in a batch job on stop() R will probably simply exit as it would do when it finishes, an idea could be to always insert an on.exit() function to communicate a failure... more tomorrow!


Ok I tried, SEXTANTE breaks with its typical error
Screenshot from 2013-03-08 09:05:35
But it would be important to have some communication between R messages and SEXTANTE, maybe the console output as written in an former mail could be a simple and efficient method, with the disadvantage that R-script writer need to take care of it in the code, and that the rest of the not explicitly coded messages (other interruptions and warnings) are not considered.

Hier the Rout:

R version 2.15.3 (2013-03-01) -- "Security Blanket"
Copyright (C) 2013 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

deflibloc <- .libPaths()[1]
if (!require(rgdal)){

  • install.packages("rgdal", dependencies=TRUE, lib="/home/matteo/.qgis//sextante/rlibs")
  • } Loading required package: rgdal Loading required package: sp rgdal: version: 0.8-5, (SVN revision 449) Geospatial Data Abstraction Library extensions to R successfully loaded Loaded GDAL runtime: GDAL 1.9.2, released 2012/10/08 Path to GDAL shared files: /usr/share/gdal/1.9 Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480] Path to PROJ.4 shared files: (autodetected) if (!require(raster)){
  • install.packages("raster", dependencies=TRUE, lib="/home/matteo/.qgis//sextante/rlibs")
  • } Loading required package: raster raster 2.1-4 (8-February-2013) RasterFile = "/home/matteo/ARC_GEO/LANDSAT_ARC/LS_timeSeries/Lehrforst/RESULTS/kNN/HAwise/LS90/kNN_k5_meanS_LS90_LT5_2011237_ref.tif" RasterLayer="all" w="3,1,2" Function="mean"



  • {
  • RasterFile <- brick(RasterFile)
  • outputRasterFile <- stack(RasterFile)
  • } else
  • {
  • RasterFile <- raster(RasterFile,band=as.numeric(RasterLayer))
  • outputRasterFile <- raster(RasterFile)
  • }

w <- as.numeric(strsplit(w,",")[[1]])


  • {
  • if (sqrt(length(w))%%2!=1)
  • {
  • stop("It is not possible to create a convolution matrix from your 'w', check the length of your 'w', it must be possible to create a 3x3, 5x5, 7x7... matrix\n")
  • }
  • w <- matrix(w, nrow=sqrt(length(w)))
  • Function <- NULL
  • } Error: It is not possible to create a convolution matrix from your 'w', check the length of your 'w', it must be possible to create a 3x3, 5x5, 7x7... matrix Execution halted

What do you think to catch every rout line that doesn't start with ">"? In rout the normal code is always anticipated with ">" warnings and errors have nothing, they are just printed.
but it would need to examin the rout for this before the script gathers the output (raster, vector...).
if non empty lines without ">" (and maybe "+") are found in rout it is always a message for the user. Thus will open a window like the consoleOutput you already have?

@volaya volaya was assigned

Hey sorry for the delay. Hmmm, that sounds like a good idea. It would be great if the author of the script could create those outputs. Do you know anyway to print things from an R script, so they do not appear with the ">" symbol? We could separates outputs with a given symbol, so only explicit errors created by the script would be catched. It woul be more difficult for script authors, but safer. what do you think?


MatMatt on easter holiday - Will be back next tuesday .. :) FYI



I had the idea to harvest rout for keywords (ie: "Error", "Warning"...), but the problem is that it is language dependent.
An other idea could be to enlower the alert level in R using: >options(warn=-1) so that only errors are printed. But I think it is important to communicate also warnings and somehow the errors that are more important need also a handling.
It exists also a R-server, for many aspects a very interesting option because R and SEXTANTE could be connected much more directly... but it may requires additional installations and it not supported or easy to install on all OS's.

I think the simplest and quite complete way ( a little bit dirty) is to collect all non empty lines in rout that do not starting with an ">" (or and "+". I'm not sure if this "+" is only a print interpretation or really existing in rout) in a similar manner how your Python scripts are already doing it now).
Any R print from: warning, error, print(), cat() do not have the ">" in rout. Probably warning and error are using cat() but I don't know.
It is important to collect all these prints as also errors and warnings from lower level functions (not explicitly written in the script, like the stop(), warnings(), cat() in the movingwindow.rxe example) contain important information.
So I'm not sure if it is good to collect only those prints defined by the script creator.
You could use the warn=-1 option ('>options(warn=-1)') in the SEXTANTE R options so if a user prefers less info it can be reduced

1: Sextante generates the R execution stript and executes it.
2: Sextante checks for relevant prints and prints them
3: Sextante prints an error (could not load Layer:) if it happens

2 and 3 could be merged to 1 pop-up message


assigned to @volaya


@volaya : is this still a valid request? or is it already implemented? otherwise it could be closed (it's 7 month old)


@volaya bring that up again ..


@MatMatt @volaya still valid? it is the oldes pull request now :)


@volaya can you have a look at this please?

@mhugent mhugent merged commit 1ed94d0 into qgis:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 0 additions and 0 deletions.
Something went wrong with that request. Please try again.