2

How can I use a DBF layer in an R script in QGIS?

For example, I can input a vector by using ##Layer=vector, and a CSV table with ##Table=table, but it will fail if the table is based on a DBF file.

I can export from DBF to CSV, but then I will loose the joins I have defined.

Sara Barnes
  • 2,072
  • 22
  • 38
paul0207
  • 23
  • 1
  • 4
  • Not an R user but from a FAQ section, it mentions to download/install the foreign package which would allow you to use the read.dbf function. There's also some more information here which includes an example on how to read dbf files. Hope this is of some help! – Joseph Sep 03 '15 at 09:58
  • Thanks for the links. They are useful for importing a DBF in R and I may use the articles for a workaround but I'm more interested in QGIS providing the data directly to the R environment through a ## command for example. – paul0207 Sep 03 '15 at 20:16

2 Answers2

1

QGIS/Processing/R does not accept .dbf files. However there is a workaround that involves using the "foreign" package.

Create a script with a folder parameter and set its path to the folder where you stored your .dbf file. Then use this code to import the .dbf file into the R environment.

##Folder=folder
library(foreign)
x <- read.dbf("your_file.dbf")

enter image description here

Filipe Dias
  • 931
  • 6
  • 20
0

(Posted this as an answer as it's too long for a comment. But atleast I can add an image!)

I think you may need to install the rpy extension for Python as mentioned in this post: Resources on using R in QGIS for R users?.

You should be able to install this extension using the OSGeo4W setup and selecting Advanced Install setting. Go through the options until you get to Select Packages. You can leave everything to Default if you don't care about other packages but in the search box, type rpy and you should get a Libs package with the rpy binding. Click the Skip message to change it:

rpy extension

Hope this helps!

Joseph
  • 75,746
  • 7
  • 171
  • 282