Execute wincsp command line arguments from within R - r

I have some command line batch code which I can run in my windows command prompt just fine. I'm basically pushing a local file text file to a remote server using WinSCP command line arguments - https://winscp.net/eng/docs/commandline. These are the commands I use, in order:
to open up winscp command line:
winscp
then to open connection to my server through ssh:
open sftp://myUserName:myPassword#theRemoteServer.net
upload file to remote server:
put directoryMyLocalFileIsIn\fileToUpload.csv /locationOnRemoteServer/whatToNameFileOnRemoteServer.csv
then close connection:
close
This all works fine. But, I want to run this all from within RStudio.. My issue -
after I run:
shell.exec("winscp")
I can see the winscp shell is opened up. But when I try and run my next commands like these:
shell.exec("open sftp://myUserName:myPassword#theRemoteServer.net")
It just doesnt run in that winscp shell that opened up.. I've also used all sorts of combinations of R commands like shell, system2 and shell.
Again, I can open up the winscp shell successfully from within R. But I cant figure out how to then run commands in that shell. Anyone know how to do this?
Thank you.

You need to call WinSCP and specify all commands using a single call from R. The best way to do this is to save your WinSCP commands in a single text file, e.g. myscript.txt:
open sftp://myUserName:myPassword#theRemoteServer.net
put directoryMyLocalFileIsIn\fileToUpload.csv /locationOnRemoteServer/whatToNameFileOnRemoteServer
exit
Then, from the command line, you can call WinSCP as follows (see the WinSCP documentation):
winscp.com /script=myscript.txt
(you might need to specify the exact path for WinSCP and myscript.txt)
Finally, to accomplish this from R, use the system2 command as follows:
system2(
"winscp.com",
args = c("/script=myscript.txt"))
Again, you might need to specify the paths to winscp.com and myscript.txt.

Related

Error that says Rscript is not recognized as an internal or external command, operable program or batch file [duplicate]

shell_exec("Rscript C:\R\R-3.2.2\bin\code.R ");
This is the call to script.On calling the above script, the error occurs.
I am trying to call my R script from the above path but no output is being shown. While checking the error logs of PHP, it says 'Rscript' is not recognized as an internal or external command, operable program or batch file.' The script is working fine on the Rstudio but not running on the command line.
Add the Rscript path to your environment variables in Windows:
Go to Control Panel\System and Security\System and click Advanced System Settings, then environment variables, click on path in the lower box, edit, add "C:\R\R-3.2.2\bin"
Restart everything. Should be good to go. Then you should be able to do
exec('Rscript PATH/TO/my_code.R')
instead of typing the full path to Rscript. Won't need the path to your my_code.R script if your php file is in the same directory.
You need to set the proper path where your RScript.exe program is located.
exec ("\"C:\\R\\R-3.2.2\\bin\\Rscript.exe\"
C:\\My_work\\R_scripts\\my_code.R my_args";
#my_args only needed if you script take `args`as input to run
other way is you declare header in your r script (my_code.r)
#!/usr/bin/Rscript
and call it from command line
./my_code.r
If you are running it in Git Bash terminal, you could follow a revised version of the idea suggested by #user5249203: in the first line of your file my_code.R, type the following
#!/c/R/R-3.2.2/bin/Rscript.exe
I assumed that your path to Rscript.exe is the one listed above C:\R\R-3.2.2\bin. For anyone having a different path to Rscript.exe in Windows, just modify the path-to-Rscript accordingly. After this modification of your R code, you could run it in the Git Bash terminal using path-to-the-code/mycode.R. I have tested it on my pc.
I faced the same problem while using r the first time in VS Code, just after installing the language package (CRAN).
I restart the application and everything worked perfectly. I think restarting would work for you as well.

How to temporarily Turn Off zscaler using Windows Command Line?

I am trying to automate a docker command in R. However, in order to run the command, zscaler must be turned off for it to work.
The R script is as follows:
shell(cmd = "docker some command", shell = "CMD.EXE")
It creates an error because zscaler is on but works when it's off.
I would like to write a command that turns off zscaler before this R script and then another one after to turn it back on. Is such a command possible?

Write a file using System Command

I am trying to create a file using the system command but it does not work for some reason. It just echoes the command back.
system("echo 'Hello2222, world.' >foo2.txt");
Hello2222, world. >foo2.txt
When I run the echo command in CMD, the file is created.
Notice that the documentation for system says
On Windows, system does not use a shell and there is a separate
function shell which passes command lines to a shell.
shell("echo 'Hello2222, world.' >foo2.txt")
will do what you want.

How to import data file for a Windows executable file from R?

I am trying to run a Windows executable file from R. I have managed to open the executable file from R using the command:
system2('cmd', args=c('/c', '"C:/Program Files (x86)/UIFormModel.exe"'))
To run the executable file, I need to import an Excel file by clicking on the button “Load Settings File” (see figure below). Thus, the button is part of the executable file.
How can I do this from R ?
I have tried this command but it does not work:
system2('cmd', args=c('/c', '"C:/Program Files (x86)/UIFormModel.exe"', '"F:/template_LHS1.xlsx"'))
'C:/Program' is not recognized as an internal or external command,
operable program or batch file.
Warning message:
running command '"cmd" /c "C:/Program Files (x86)/UIFormModel.exe" "F:/template_LHS1.xlsx"' had status 1
Several points:
normally one does not have .exe files directly under C:\Program Files (X86) so first double check the path name. I suspect it is not as in the question but is something like the one shown below.
note the quoting used below
are you sure that the program accepts a command line argument as an alternative to its GUI? Can you run it from Windows cmd line? If not you could write an autohotkey (see AutoHotKey section below) or autoit script to control it and launch that from R.
if it does accept an argument ensure that the program allows forward slashes -- if not use backslashes
cmd line argument accepted
If the program accepts a command line argument then try this code:
system2("C:/Program Files (x86)/UIFormModel/UIFormModel.exe", "F:/template_LHS1.xlsx")
Another way is:
shell('"C:/Program Files (x86)/UIFormModel/UIFormModel.exe" F:/template_LHS1.xlsx')
For example, using the free CSVfix I was able to run the following and found that they worked:
system2("C:/Program Files (x86)/CSVfix/CSVfix.exe", "help")
shell('"C:/Program Files (x86)/CSVfix/CSVfix.exe" help')
See ?system2 and ?shell for other arguments you may wish to use.
AutoHotKey
If the program does not accept command line arguments then this sample AutoHotKey script launches WinMerge, a free file comparison GUI for Windows, comparing the WinMerge executable to itself. This has the effect of generating a popup warning and the AutoHotKey script dismisses that warning message by sending an Enter keystroke to the WinMerge window.
Be sure AutoHotKey and WinMerge are installed, that the script below is in a text file, test.ahk, in the current directory, the one given by getwd(), and then from the R console run:
shell("test.ahk")
Here is the test.ahk script:
exe = "C:\Program Files (x86)\winmerge\winmergeu.exe"
Run %exe% %exe% %exe%
WinWait WinMerge
; dismiss the dialog warning that both files are the same
Send {Enter}
If you go this route this script will need to be modified or rewritten in ways that will be specific to your program.
I've been trying several alternatives, but I've not successfully called an executable with a parameter. Nevertheless I can suggest a workaround using a .bat file:
system2("cmd.exe", input='"D:/workspace/execute.bat"')
Where execute.bat may contain something like:
C:/Program Files (x86)/UIFormModel.exe F:/template_LHS1.xlsx

Unable to run R script through .bat files in Windows Server

I'm trying to run a R script through a .bat file. When I run myself the commands line by line it works. But when I try to run the .bat file, it doesn't works.
This is the .bat file
cd "C:\Program Files\R\R-3.1.2\bin"
R CMD BATCH "C:\Users\Administrator\Downloads\testa_vps.R"
This is the R script
setwd('C:\Users\Administrator\Documents')
file.create('mycsv.csv')
I'm not an expert with Windows and generally try to stick to Unix-like systems for things like this, but I have found that using programs non-interactively (e.g. via .bat files) is usually less error-prone when you add the appropriate directories to your (user) PATH variable, rather than cding into the directory and calling the executable from within the .bat file. For example, among other things, my user PATH variable contains C:\PROGRA~1\R\R-3.0\bin\; - the directory that contains both R.exe and Rscript.exe - (where PROGRA~1 is an alias for Program Files which you can use in an unquoted file path, since there are no spaces in the name).
After you do this, you can check that your PATH modification was successful by typing Rscript in a new Command Prompt - it should print out usage information for Rscript rather than the typical xxx is not recognized as an internal or external command... error message.
In the directory C:\Users\russe_000\Desktop\Tempfiles, I created test_r_script.r, which contains
library(methods)
setwd("C:\Users\russe_000\Desktop\Tempfiles")
file.create("mycsv.csv")
and test_r.bat, which contains
Rscript --vanilla --no-save "C:\Users\russe_000\Desktop\Tempfiles\test_r_script.r"
Clicking on the Windows Batch File test_r ran the process successfully and produced mycsv.csv in the correct folder.
Before running test_r.bat:
After running test_r.bat:
I've never worked with a Windows server, but I don't see why the process would be fundamentally different than on a personal computer; you just may need your sysadmin to modify the PATH variable if you don't have sufficient privileges to alter environment variables.
As already suggested by #nrussel in the comments you should use RScript.exe for this.
Create a file launcher.bat with the following content:
cd C:\Users\Administrator\Documents
Rscript testa_vps.R
In addition, add C:\Program Files\R\R-[your R version]\bin\x64; or C:\Program Files\R\R-[your R version]\bin\i386to the System PATH variable in the Environment Variables menu depending if you run R on a 64-bit or 32-bit system.
I just tested the approach above successfully on a Windows Server 2008 64-bit system and mycsv.csv got created as expected.
EDIT
One important point I forgot to mention is the following: You need to specify the path in your R file in the setwd() call using \\ instead of \.
setwd('C:\\Users\\Administrator\\Documents')
Here is a screenshot of the successful run on the Windows 2008 server:
Note: I added cmd /k to the .bat file so that the cmd window stays open after clicking on the file.

Resources