R system() not working with MODIS Reprojection Tool - r

I have a command that works when typed into the Terminal on a Mac (OSX El Cap), but the same command fails when called from R using system().
I am trying to batch-process MODIS satellite files using the MODIS Reprojection Tool. My software is all up to date.
This is a simple example in which I mosaic two files. The names of the two files are in a text input file called input.list. The command just tells the mrtmosaic routine where to find the input list and where to put the output.
This command works correctly in the Terminal:
/Applications/Modis_Reprojection_Tool/bin/mrtmosaic -i ~/temp/input.list -o ~/temp/output.hdf
However, if I put exactly the same string into a variable and run it from R (using RStudio), it fails:
comstring<-"/Applications/Modis_Reprojection_Tool/bin/mrtmosaic -i ~/temp/input.list -o ~/temp/output.hdf"
system(comstring)
Warning: gctp_call : Environmental Variable Not Found:
MRT_DATA_DIR nor MRTDATADIR not defined
Error: GetInputGeoCornerMosaic : General Processing Error converting lat/long coordinates to input projection coordinates.
Fatal Error, Terminating...
The strange thing is that the system knows what the environment variables are. In the terminal, the command
echo $MRT_DATA_DIR
shows the correct directory: /Applications/Modis_Reprojection_Tool/data
I don't see why it would have trouble finding the variables from an R system() call when it has no trouble in the Terminal. I'm very stumped!

I posted this question to the R help list, and two people there helped me solve the problem. They suggested (if I understood right) that the problem was that OSX El Capitan was not successfully passing environment variables to R. In any case:
These do not work:
Setting the environment variable in my .bash_profile (for example by adding and exporting MTR_DATA_DIR="/Applications/MRT/data"); or
Setting the environment variable by adding the same line to the .Rprofile file in my home directory.
These do work:
Setting the environment variable at the R command line by typing Sys.setenv(MRT_DATA_DIR="/Applications/MRT/data"); or
Setting the environment variable in the .Renviron file (which is in my home directory) by adding MRT_DATA_DIR="/Applications/MRT/data" to it; or
Typing MRT_DATA_DIR="/Applications/MRT/data" open -a Rstudio in the Terminal. This last method is an effective workaround and a useful tool in a bag of tricks, but is slightly clumsier since one has to remember to open RStudio this way each time.

Related

Running R via terminal in Windows and leaving R session open

Suppose that I have a R script called test.R, stored at C:\, with the following content:
x <- "Hello Stackoverflowers"
print(x)
To run it via terminal one could simply call:
Rscript C:\test.R
And as expected, the result will be:
However, what I wonder is whether there is a way to run test.R via Windows console but after that staying within the executed R session instead of closing and going back to the console cursor? That is, staying inside the R session instead of going back, in the image above, to C:\R\R-3.4.1\bin>.
For instance, when compiling Python code with python.exe I can easily accomplish a similar thing by passing the -i parameter to the python.execall.
How could I do that with R?
Add this to your .Rprofile:
STARTUP_FILE <- Sys.getenv("STARTUP_FILE")
if (file.exsts(STARTUP_FILE)) source(STARTUP_FILE)
and then set the indicated environment variable outside of R and then run R. e.g. from the Windows cmd line:
set STARTUP_FILE=C:\test.R
R
... R session ...
q()
Variations
There are many variations of this. For example, we could make a copy of the .Rprofile file in a specific directory such as ~/test, say, and add this code to that copy
source("~/test/test.R")
in which case R would only run test.R if R were started in that directory.

Rgui command-line for sourcing R file

What command line option to use behind Rgui.exe for immediately sourcing an R source file? Instead of having to type source("c:\MyGreatSource.R") manually afterwards. Something like:
Rgui.exe --source "c:\MyGreatSource.R"
Sounds like a simple question answered in any beginner's manual, but I couldn't find such an option anywhere.
I found a workable solution, maybe others are interested. Again, what I like to do is to start the Rgui and work there. All my work environment and functions are defined in an R source file, which I constantly develop further during working. So each of my commands in the GUI starts with Load1(); where Load1 is a function which simply sources my R file, to update the changes I have just made. Obviously, Load1 is also defined in my R file, so I need to get it in the first place, without much effort. I have set the command-line options for neither loading nor saving the workplace; I don't like my old mess from the previous session with test variables and so.
However, my solution now is to just create a workplace RData file which only contains the definition of my Load1 function. This workplace file is easily loaded at every start by just adding its path into the command-line options "D:\MyLoad1.RData"
I use a AutoHotkey Script
run,C:\Program Files\R\R-3.3.3\bin\x64\Rgui.exe
WinWait,RGui (64-bit)
WinWaitActive,RGui (64-bit)
Sleep 100
Send,source("%1%")
Send,{enter}

Running a windows executable within R using wine in ubuntu

I am trying to execute a windows only executable called (groundfilter.exe from FUSION) within Rstudio on Ubuntu.
I am able to run groundfilter.exe from a terminal using wine as follows:
wine C:/FUSION/groundfilter.exe /gparam:0 /wparam:1 /tolerance:1 /iterations:10 test_Grnd.las 1 test.las
The executes fine and produces file test_Grnd.las OK.
But when i try to do this from within Rstudio using system() it doesn't quite work, and no output file is produced (unlike from terminal). I do this:
command<-paste("wine C:/FUSION/groundfilter.exe",
"/gparam:0 /wparam:1 /tolerance:1 /iterations:10",
"/home/martin/Documents/AUAV_Projects/test_FUSION/test_FUSION/test_GroundPts.las",
"1",
"/home/martin/Documents/AUAV_Projects/test_FUSION/test_FUSION/test.las",sep=" ")
system(command)
The executable appears to be called OK in Rstudio console, but run as if no file names were supplied. The output( truncated ) is:
system(command)
GroundFilter v1.75 (FUSION v3.60) (Built on Oct 6 2016 08:45:14) DEBUG
--Robert J. McGaughey--USDA Forest Service--Pacific Northwest Research Station
Filters a point cloud to identify bare-earth points
Syntax: GroundFilter [switches] outputfile cellsize datafile1 datafile2 ...
outputfile Name for the output point data file (stored in LDA format)
This is the same output from the terminal if the file names are left off, so somehow my system call in R is not correct?
I think wine will not find paths like /home/martin/....
One possibility would be to put groundfilter.exe (and possibly dlls it needs) into the directory you want to work with, and set the R working directory to that directory using setwd().
The other possibility I see would be to give a path that wine understands, like Z:/home/martin/....
This is not an authoritative answer, just my thoughts, so please refer to the documentation for the real story.

How to drop into R shell after executing commands from file in R

In Python, running the interpreter with the -i flag first executes the script, then drops back into the interpreter
python -i hello.py
Hello world
>>> print("Python ftw")
Python ftw
>>>
which allows me to type commands and reach the variables after execution.
With R, this seems to be of great difficulty. I have been searching online for some time, and am surprised to see there is not so many results with the keywords "R run file shell interpreter".
With R, you can use
$ R -f myfile.R which executes and then exits the interpreter
$ Rscript myfile.R which still does the same thing.
Even worse, it does not plot when run like this and just exits without showing any signs that something has been plotted.
So, to repeat my question:
How do I make R to drop into the R shell after running commands from a file, a.k.a. a script?
Concurrently, how can I make R really plot the plots and not close them off immediately?
I can do these with Python, MATLAB, Octave, Ruby and many others, and should be able to do with R too.
I will answer your two questions separately:
How do I drop into a shell after my script has executed?
The function "browser" called with no arguments will allow to to drop into a shell on the line that it's called. Appending this to your script should do the trick.
How do I save graphics when not run in interactive mode?
First, check that there isn't a pdf file being created in your working directory. Depending on how you're running R, I believe it may be named "Rplots.pdf". Personally, however, I prefer to explicitly save graphics to a particular file, as such:
pdf("temp.pdf")
plot(rnorm(100))
dev.off()
which will save the plot in a new file called temp.pdf (and will overwrite any existing file by that name, so watch out).
Functions analagous to "pdf" exist for other image formats if you would prefer that.

Installing program (dot) on system search path in R

I'm a social scientist having a hard time struggling through the technical instructions for integrating the graphing program dotdocumented here with the sem package documented here.
I have successfully run the command pathDiagram in the sem package, which outputs code that is presumably supposed to be read into dot. The documentation for the sem package says
"To obtain graphics output directly, the dot program must be on the system search path."
I'm sure that this is a totally mundane question, but I'd really appreciate help. I'm lost.
In R execute this command:
Sys.which("dot")
This will tell you whether a system call will be able to find your 'dot' installation. You did install 'dot', right?
The Graphviz pkg file for MacOS greater than or equal to 10.6 (and your ML is 10.8) can be obtained from AT&T labs: http://www.graphviz.org/Download.php
After installing from its .pkg file (with the same Graphviz version 2.30.1 on OSX 10.6.8) I get:
> Sys.which(c("dot"))
dot
"/usr/local/bin/dot"
This does NOT open up dot. It merely confirms that the dot program can be found on by R. Running the first example on hte ?pathDiagram page only outputs a source file to the console which could be used for dot to the console, because no file argument is given, but if you add a file argument it creates a pdf file using dot with a message to the console screen:
pathDiagram(sem.dhp, min.rank='RIQ, RSES, RParAsp, FParAsp, FSES, FIQ', file="test",
max.rank='ROccAsp, REdAsp, FEdAsp, FOccAsp', output.type="graphics", graphics.fmt="pdf")
#Running dot -Tpdf -o test.pdf test.dot
This produced a file in my working directory that could be viewed with Preview.app. If you want to use dot on its own, you will need to read its manual and follow the instructions for using it in a standalone manner.
Update as per comments:
If when you type which dot in terminal, you get a response with a path, then it is safe to say that graphviz is installed.
As per pathDiagram's manual (ie, ?pathDiagram), it is not intended to return a value.
Instead, use the file argument to specify where to save the output to.
pathDiagram(model=SomeModelInR, file="path/to/file", ...)
This seems to be more of a general mac os x question, but anyway, here we go:
In your applications > utilities folder, open up terminal.
(i'm assuming dot is called dot if not please modify accordingly)
Type which dot
- this will show you the path to dot, perhaps something like /usr/bin/dot
- in this case, the path you are looking for in the next part is /usr/bin
Type echo $PATH
- this will show you several paths separated by :
- If the path you got with which dot is listed as one of those paths, then you are all set.
- If not, please see the link below on how to modify
Also see:
How To Edit Your PATH Environment Variables On Mac

Resources