north-road / qgis-processing-r

QGIS Processing R Provider Plugin
https://north-road.github.io/qgis-processing-r/
GNU General Public License v3.0
63 stars 14 forks source link

Path to project folder #37

Open mproctor340 opened 5 years ago

mproctor340 commented 5 years ago

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)

nyalldawson commented 5 years ago

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?