qgis-processing-r icon indicating copy to clipboard operation
qgis-processing-r copied to clipboard

Path to project folder

Open mproctor340 opened this issue 5 years ago • 1 comments

As part of my script I need to download rasters from the net, and use another file which contains functions, so I would like to set the working directory in order to save the rasters to disk and source the functions file.

I've been trying to identify using R the current directory of the script, and the following code spits out:

Find current directory of script and set as working directory

thisFile <- function() { cmdArgs <- commandArgs(trailingOnly = FALSE) needle <- "--file=" match <- grep(needle, cmdArgs) if (length(match) > 0) { # Rscript return(normalizePath(sub(needle, "", cmdArgs[match]))) } else { # 'source'd via R console return(normalizePath(sys.frames()[[1]]$ofile)) } } print(thisFile())

[1] "C:\Users\Matt Proctor\AppData\Local\Temp\processing_0e8b98b4e2154a4a83c579f6d3791d92\aad1a7bbc681497ca28fc19d0e5804e9\processing_script.r"

But the actual script is edited and saved in my project folder in my Documents, so it obviously creates a temp instance of the script to run from.

I would like to know if there is any way of identifying through R?: a) the current directory of the QGIS project b) the directory of the R scripts folder pointed to in the Processing options (at least that won't be a temp folder)

mproctor340 avatar Aug 21 '19 08:08 mproctor340

Hmm - I think the only way we could do this would be to have some set of tokens which are used to identify a part of the script which contains a (QGIS) expression which needs to be evaluated prior to running the script.

E.g. in QGIS layouts, you can put My label: [% some qgis expression %] and the part between [% %] will be evaluated.

What would you suggest is a safe set of starting/ending characters we could use which wouldn't clash with anything on the R side?

nyalldawson avatar Aug 21 '19 22:08 nyalldawson