Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path to project folder #37

Open
mproctor340 opened this issue Aug 21, 2019 · 1 comment
Open

Path to project folder #37

mproctor340 opened this issue Aug 21, 2019 · 1 comment
Labels
question Further information is requested

Comments

@mproctor340
Copy link

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
Copy link
Contributor

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 nyalldawson added the question Further information is requested label Aug 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants