I have a workflow where my R script produces csv's and shapefiles. Those csv's and shapefiles then serve as layers in a QGIS project. Every time the R script is run, it overwrites the previous data, and the QGIS project is "dynamically" updated. The QGIS project is all set up such that it never needs to be updated or modified, since it dynamically loads the layers output by the R script.

My current workflow:

  1. Run R script
  2. Open QGIS project file
  3. Open Print composer manager
  4. Select and Open existing print composer layout
  5. Export (print) as PDF
  6. Quit qGIS
  7. View PDF

Desired workflow:

  1. Run R script
  2. View PDF

I can get part of the way there in R by opening the QGIS file in QGIS, by calling

# if on Windows:    
# or if on MAC OS X:
system(paste("open", "my_qGIS_file.qgs"))  

but then I can't figure out how to programmatically (from R) produce the pdf and quit qGIS. I've read and reread the PyQgis cookbook section on the print composer, as well as the other examples on the internet (including this one), but those are both beyond my python comprehension, and seem to be way more complicated than I need. It seems like all the examples are programmatically redefining all the print composer every time the script is run. In this workflow, I already have a print composer layout stored in the project that I had created through the GUI, and have no desire or ability to recreate it with code.

If this can't be done directly in R, I think it's possible to invoke a python script from R via a system call, something like: source("my_python_script_that_exports_my_PDF.py"), where the python script is self-standing.

Also, I have some QGIS plugins installed that bypass having to open the print composer and just export existing composer layouts cut down the workflow by a few clicks. One of the plugins is "Maps Printer", another is "multiPrint" Perhaps a python or R script can invoke commands from these plugins?

Most critically I would like any proposed solution to run on windows, but bonus points if the suggested solution:

  1. works on both windows and mac

  2. does NOT actually load the full QGIS GUI (i.e., the person running the script does not see the QGIS loading splash screen or anything QGIS related; they just see the PDF magically show up in a specific directory, perhaps with a file name that is "file_name_" + timestamp.PDF).

I am running QGIS 2.8

  • Maybe the question should be then - if I am correct: Is it possible to execute a print composer file (.qpt) from the command line ? Something like qqis-print-composer -project project.qgs -composition composition.qpt -output mymap.jpg? Then you could call it in R
    – nebi
    Commented Oct 25, 2015 at 13:00
  • Yes, thank you, that phrasing of the question is clearer. I've updated the title of the post. Commented Oct 25, 2015 at 14:21
  • kalinowski! Did you ever find a solution to this? This is exactly what I want to do. I have an R script that preps all my .QGS files. I just want an automated way to PDF the layouts, from R. Thanks! Commented Jul 19, 2019 at 16:10
  • Sorry, I did not find a solution in the end :( Commented Jul 19, 2019 at 18:04

1 Answer 1


There are many solutions

1) You can run directly R scripts in the QGIS Processing Toolbox (Resources on using R in QGIS for R users?, Configuring external applications,...)
The processing Python module use the subprocess module to call the R commands.

2) You can also use R from Python with the rpy2 module and Python from R with the RPython package

3) You can use PyQGIS without opening the application (Standalone applications using QGIS and environment variables and many other examples)

And finally, you can mix Python, PyQGIS, R and many other things using a Jupyter/IPython rmagic notebook (Filling in Python’s gaps in statistics packages with Rmagic)

  • 1
    Thank you for the response. I'm afraid I wasn't clear enough with my question. I know how to run R from inside qGIS, i'm more interested in how to control qGIS from R. Control might even be too strong of a word, I just want to just print an existing print composer. Could you maybe give an example magic code in pyton or R thats like: QGIS_file = load.qgis.file( myfile.qgs ); printPDF (qGIS_file, "FileName.qgs") Commented Oct 25, 2015 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.