When running R scripts in QGIS, upon loading my vector layer with ##Layer=vector it automatically runs Layer <- st_read("path/to/shape.shp", quiet = True, stringsAsFactors = FALSE) on my input layer

This prevents me from being able to use the raw filepath later on in my scripts. Is it still possible to get the raw filepath for an input layer from an R script in QGIS?

The output I want when I type Layer is:


The output I get is:

Simple feature collection with 1 feature and 1 field
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: 581560.5 ymin: 5649045 xmax: 583123.1 ymax: 5649831
Projected CRS: NAD83(NSRS2007) / UTM zone 10N
id geometry
1 1 POLYGON ((583085.2 5649232,...

I'm not very familiar with st_read and I'm not sure if it stores the filepath or if I can retrieve it after st_read has been run

1 Answer 1


Not sure I understand your problem. If you want the path to the shapefile for use later in the script, then just declare it as a variable, and it will be available: i.e.

library(sf)  # This library contains all the st_* functions
shape_dir <- "path/to/shapes"
shape_path <- file.path(shape_dir, "shape.shp")
Layer <- st_read(shape_path, stringsAsFactors = FALSE)
# Reuse the file path:
print(paste("Just loaded shapefile:", shape_path))
  • Hey @Micha, thanks for the response. QGIS, with the 'Processing R Provider' plugin, loads shapefiles in differently. Using the code ##Layer=vector it creates a dropdown that allows you to select a vector from your current map or on your hdd. However, it automatically runs st_read() on the file. Thus, file.path() doesn't work in this instance. I was hoping for similar code that works for finding a filepath after st_read() has been run or a QGIS specific fix. The QGIS docs are kind of weak on this.
    – jdavid05
    Apr 19, 2021 at 20:09

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.