[Moving this to grass-dev as this is becoming a more involved development discussion.]
On 31/05/18 11:47, Helmut Kudrnovsky wrote:
Here's a complete working example of the entire OBIA chain in the NC
dataset (warning: this is a demo example, not necessarily one of best
practice ;-)) :thanks, testing it here now.
https://trac.osgeo.org/grass/browser/grass-addons/grass7/vector/v.class.mlR/v.class.mlR.py#L278
278 install_package = "if(!is.element('%s',
installed.packages()[,1])){\n"
279 install_package += "cat('\\n\\nInstalling %s package from
CRAN\n')\n"
280 install_package += "if(!file.exists(Sys.getenv('R_LIBS_USER'))){\n"
281 install_package += "dir.create(Sys.getenv('R_LIBS_USER'),
recursive=TRUE)\n"
282 install_package += ".libPaths(Sys.getenv('R_LIBS_USER'))}\n"
283 install_package += "chooseCRANmirror(ind=1)\n"
284 install_package += "install.packages('%s', dependencies=TRUE)}"for installling packages in windows R:
for installing system wide in windows administrator rights are needed. maybe
some possible source of troubles.
The .libPaths(Sys.getenv('R_LIBS_USER')) call should set the installation directory to the user's. But maybe this is not guaranteed to be defined which wouldn't make it a good option.
so maybe a recommendation in the manual?: let the user install the needed
packages first in R itself, then start the grass addon.
This module was originally developed for a classroom environment with the attempt to not force students (who don't know R for most parts at that stage) to have to do R packages installations, especially since which R packages you have to install depends on which classifiers you use.
But maybe you are right that we should just declare the R packages as dependencies up to the user to install.
Or rewrite the whole thing in scikit-learn just as r.learn.ml. At the time, it seemed more robust to assume a general R installation and install the needed packages automatically than to assume the presence of the correct version of scikit-learn...
addon test here shows:
-----------------
v.class.mlR --verbose segments_map=lsat7_segs_stats training_map=training
train_class_colum=class
Running R now. Following output is R output.
Loading required package: caret
Loading required package: lattice
Loading required package: ggplot2
There were 50 or more warnings (use warnings() to see the first 50)
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
Calls: write.csv -> eval.parent -> eval -> eval -> write.table -> file
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file '.gscript_tmp_model_output_1300.csv': Permission denied
Execution halted
ERROR: There was an error in the execution of the R script.
Please check the R output.
-----------------it seems, R is started, packages loaded correctly.
where is the file '.gscript_tmp_model_output_1300.csv' written to?
485 if allmap and not flags['f']:
486 model_output = '.gscript_tmp_model_output_%d.csv' % os.getpid()
487 write_string = "write.csv(resultsdf, '%s'," % model_output
488 write_string += " row.names=FALSE, quote=FALSE)"
489 r_file.write(write_string)
490 r_file.write("\n")
Cf the other mail.
would it be possible to catch the R error/output?
Could you elaborate ? The module already goes through a try/except running R:
gscript.message("Running R now. Following output is R output.")
try:
subprocess.check_call(['Rscript', r_commands], stderr=subprocess.STDOUT, )
except subprocess.CalledProcessError:
gscript.fatal("There was an error in the execution of the R script.\nPlease check the R output.")
And in your above about everything from "Running R now. Following output is R output." is the R output.
What else do you mean by catch the R output ?
Moritz