run windows command from R - r

In windows 7 at run prompt, this command succeeds in launching the .exe with the the optional input file "test2.dat"
c:/Program Files (x86)/IEUBKwin1_1 Build11/IEUBKwin32.exe k:/Project/EPA.Pb.IEUBK/batch.io/input/test2.dat
I want to do the same thing from within R.
In R, this command succeeds in launching the same .exe
shell.exec("c:/Program Files (x86)/IEUBKwin1_1 Build11/IEUBKwin32.exe")
But I've been unable to find a solution within R that will launch the .exe with the optional input file. I've looked at shell(), shell.exec() and system() but I could not find the right incantation that will pass the optional input file to the .exe.
Any thoughts?

Typing into the normal cmd.exe-promt a command including spaces as in C:\Program Files (x86)\... does not work:
The Command "C:\Program" could not be found.
Typing in the same command with double quotes does work. E.g.:
"C:\Program Files (x86)\7-Zip\7z" -a ...
To get it to work in R, you can use single quotes (') to mark a R string and double quotes (") for the command itself. Actually, you have the possibility of three different quotes to use (backtick is the third one `, see here for more information). Or you use escapes as mentioned in the answer of #Frank.
system('"C:/Program Files (x86)/IEUBKwin1_1 Build11/IEUBKwin32.exe" k:/Project/EPA.Pb.IEUBK/batch.io/input/test2.dat')
In addition ?system mentions not just to use shell in windows but also system2 as alternative:
...This means that it cannot be assumed that redirection or piping will work in system (redirection sometimes does, but we have seen cases where it stopped working after a Windows security patch), and system2 (or shell) must be used on Windows.
But for me system works totally fine not using piping or redirection.

shell.exec() is used for opening files associated in your OS.
in your case, the shell command should be preferred but you need to take care of spaces in your filenames and mask quotaion marks.
Please try:
shell("\"c:/Program Files (x86)/IEUBKwin1_1 Build11/IEUBKwin32.exe\" k:/Project/EPA.Pb.IEUBK/batch.io/input/test2.dat")

Related

How to Execute R Script from Windows Command Prompt

I have the following added to my system path on Windows 10: C:\Program Files\R\R-3.4.3\bin\x64
Then I tried running from cmd prompt ~:> R myscript.R
ARGUMENT 'example_batch.R' ignored
It proceeded to an interactive session without any execution.
No idea how it seemed to work in this answer. I would prefer something this simple, as it is in Python.
The following works R -e "source('myscript.R')"
But I'll never remember this when I need it.
Lucas's solution gave me an error when run in Powershell/Windows termianl (which is now the default on Windows 11). It seems that when the executable dir is inside quotation marks (""), Powershell treats it as a string.
To execute Rscript.exe in Powershell, you need to add & before the dir
& "C:\Program Files\R\R-4.2.2\bin\Rscript.exe" "C:\PATH\TO\SCRIPT.r"
The best way I've found is by executing Rscript.exe (it's installed with R.exe inside R directory).
You can do:
"C:\Program Files\R\R-3.6.0\bin\Rscript.exe" D:\path\to\yourRfile.r arg1 arg2 arg3
The quotation marks ("") are necessary because dir name Program Files has space between it and Windows CMD wouldn't recognize it without quotation marks.
If you don't want to write R path, you can put R Path to your windows PATH environment variable. This way you could run like:
Rscript.exe D:\path\to\yourRfile.r arg1 arg2 arg3
If you have doubts how to add R path to Windows PATH environment variable, you can follow these instructions (they are for adding Java to PATH, but you can use for R, the idea is the same).

Space in argument causing errors in Windows Command Prompt

I'm trying to run an R script through the command prompt. My operating system is Windows 10. I'm having trouble running the code because there is a space in the file path of my argument. This is what I paste into the command prompt.
"C:\Program Files\R\R-3.4.3\bin\Rscript.exe" "C:\Users\Scott\Google Drive\RScriptsB\Bundle_Runner.R"
I get this error:
The filename, directory name, or volume label syntax is incorrect.
However, when I run it using a file path with no spaces, it runs fine.
"C:\Program Files\R\R-3.4.3\bin\Rscript.exe" "C:\Users\Scott\Desktop\Bundle_Runner.R"
The same behavior happens when I schedule the task through Task Scheduler: it doesn't work with the space, and it works when I remove the space from the file path.
I'm using Google Drive to sync work from multiple computers, so I'd like to be able to run my scripts using the file path with a space.
Any solutions?
FOR %%a IN ("C:\Users\Scott\Google Drive\RScriptsB\Bundle_Runner.R") DO "C:\Program Files\R\R-3.4.3\bin\Rscript.exe" %%~sa
would be my approach - the problem appears to be with R, not cmd.
Try this:
C:/PROGRA~1/R/R-3.4.3/bin/Rscript.exe "C:\Users\Scott\Desktop\Bundle_Runner.R"
Or
C:/PROGRA~1/R/R-3.4.3/bin/Rscript.exe C:/Users/Scott/Google~1/RScriptsB/Bundle_Runner.R
This is possibly related to an error reported at r-devel ("[Rd] Bug in RScript.exe for 3.5.0", https://stat.ethz.ch/pipermail/r-devel/2018-April/075869.html) that has been fixed the next day.
Perhaps the problem was already present in R 3.4.3 (you are using in your question).
Proposed workaround:
...add an extra first argument that has no space in it, e.g. Rscript --vanilla "foo bar.R"
To minimize the impact caused by --vanilla you could use
Rscript --no-save "foo bar.R"
instead (which just does not save the workspace at the end of the session).

Rscript file path with space

I am trying to run the following R script in windows shell:
Rscript C:/Documents/Folder name containing space/myscript.txt
In this case I get the error:
Fatal error: cannot open file 'C:/Documents/Folder': No such file or directory
However when I use quotation marks (tried single double and triple as was suggested in other posts) I get the following error:
Rscript "C:/Documents/Folder name containing space/myscript.txt"
The filename, directory name, or volume label syntax is incorrect.
I can't find a way to get around the space problem and changing the file location so there are no white-spaces is not an option for me.
Any help would be greatly appreciated.
Further Clarifications:
The issue I am having is not directly related to R but rather to to having the file path that contains spaced being passed to Rscript.
In the documentations, Rsript should be used in the following way:
Rscript [options] [-e expr [-e expr2 ...] | file] [args]
It is also noted that:
Spaces are allowed in expression and file (but will need to be protected from the shell in use, if any, for example by enclosing the argument in quotes).
However trying to enclose the file path in quotes results in the error
The filename, directory name, or volume label syntax is incorrect.
To avoid confusion, running Rscript C:/Documents/Folder_name/myscript.txt works fine when the path doesn't contain any spaces as does Rscript "C:/Documents/Folder_name/myscript.txt".
It is a BUG in R version 3.5.0 for Windows.
One workaround, apart from downgrading, is creating an R script with no spaces in its path and run the spaced one with source():
## C:\Documents\Folder-name-no-space\myscript.txt
source("C:/Documents/Folder name containing space/myscript.txt")
Then you run it with:
Rscript C:\Documents\Folder-name-no-space\myscript.txt
or also:
Rscript C:/Documents/Folder-name-no-space/myscript.txt
You may also try the 8.3 filename. You can get it with:
for %I in ("C:/Documents/Folder name containing space/myscript.txt") do #echo %~sI
UPDATE
Since 3.5.1 the problem has been fixed.
Forward slashes work just fine with R, so don't worry about backward slashes. I've just verified and the following works at the CMD.exe terminal of Windows 8.1:
C:\Windows\System32> Rscript "C:/Users/hb/folder with spaces/script.R"
[1] "1+2+3"
C:\Windows\System32>
My best guess is that you've got the pathname incorrect. If it's a non-existing pathname, you do get:
C:\Windows\System32> Rscript --vanilla "C:/Users/hb/folder with spaces/non-existing.R"
Fatal error: cannot open file 'C:/Users/hb/folder with spaces/non-existing.R': No such file or directory
You can validate it from within R, e.g.
> file.exists("C:/Users/hb/folder with spaces/script.R")
[1] TRUE
Simple solution: install a newer version of R.
From the version 3.5.1 release notes, the relevant bug fix is described here:
Allow file argument of Rscript to include space even when it is first on the command line.
Another convenient workaround was to create an alias for the portion of the path which contains spaces, i.e.:
SUBST k: "c:\Folder with Spaces"
rscript k:\scripts\program.R

How to prevent MSYS from converting remote file path for pscp

I'm using msys2 bash 4.3.26 (i686-pc-msys) on Windows, and recently I find that it breaks my pscp and scp (msys1):
When I run pscp req.py hostname:/home/user/xxx/yyy/:
msys will change the second args to hostname;D:\msys\home\user\xxx\yyy\
and then pscp will report: ssh_init: Host does not exist
my scp which is from msys-1.0 (I once used git-for-windows 1.9) reports ssh: Could not resolve hostnamehostname;d: no address associated with name
This is annoying and I have to open cmd.exe to use pscp. Is there any way to prevent MSYS 2 from convert special args?
I tried wrapping an arg in ' or " and failed
http://www.mingw.org/wiki/Posix_path_conversion is not helpful but disappointing
environment: Windows 10 Insider x64 and msys2 i686
the official scp.exe in the same package of msys2 works well
BTW, this problem only occurs on those .exe files which does not use msys2 DLLs. How this difference is being checked?
MSYS2 converts POSIX-style path names to Windows-style path names when it detects you are running a native Windows program like pscp. These conversions use a lot of heuristics and sometimes they get the wrong answer.
You can inhibit conversions of certain arguments by setting the MSYS2_ARG_CONV_EXCL environment variable as you run the command. It is a list of prefixes of arguments for which MSYS2 should not attempt to convert paths. You could just set the variable to the empty string in order to turn off all conversions. However, you might need the first argument to pscp to be converted since it is a path on your local computer. So you should try running your command like this instead:
MSYS2_ARG_CONV_EXCL=hostname pscp req.py hostname:/home/user/xxx/yyy/

Trouble with path specifications when running RStudio from a console

I have R script within a plain text file named "TestFile.R".
I am running RStudio. I want to use the Windows console (cmd.exe) to pipe "TestFile.R" directly into the "R Script" editor of RStudio, without launching a new thread of RStudio.
This command in the console does exactly what I want:
rstudio -f TestFile.R
The contents of "TestFile.R" go straight into the RScript editor of the existing thread of RStudio.
It assumes, however, that "TestFile.R" is in the "bin" folder of RStudio, and also that
cmd.exe is running within that folder.
But I want to be able to do this from anywhere on my computer, using a console command like:
pathToRstudio\rstudio -f pathToTestFile\TestFile.R
To give an example, on my computer, this command fails:
C:\"Statistical packages"\RStudio\bin\rstudio -f E:\"my project"\TestFile.R
By trial and error, I discovered these solutions:
1/ omit the "C:\" part
2/ avoid quotation marks in the pathToTestFile.
So this console command works fine:
"Statistical packages"\RStudio\bin\rstudio -f E:\myproject\TestFile.R
Of course, I still am very restricted ; my default folder has to be "C:\", and I cannot have spaces in the path to TestFile.R, even though spaces within the Rstudio path are apparently OK !?
Could somebody please explain to me how to write this command in a way that is completely generic with regard to path specification?
I want to be able to run it from any folder, and have TestFile.R in any other folder. I do not want hassles about folder names containing spaces.

Resources