I would like to invoke R script by using a .bat file.
For testing, I saved an R file on the desktop in the name print_test.R
The code in this file is:
p<-500
write.csv(p,"print.csv")
I wrote the .bat file :
#echo off
R CMD BATCH C:\Users\User\Desktop\print_test.R
UPDATE:
After adding pause to the .bat file I got the following text:
"'R' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . ."
When I click on the bat file (also saved on the desktop) there is a blink of the CMD window but the print.csv file is not created.What is wrong?
Consider using the automated utility, Rscript.exe, as the R CMD BATCH might be considered a legacy command. See post: R.exe, Rcmd.exe, Rscript.exe and Rterm.exe: what's the difference?
Rscript as an environment PATH variable:
#echo off
Rscript "C:\Users\User\Desktop\print_test.R"
Rscript not as a environment PATH variable (where you specify full path of the executable -usually in bin folder of installation):
#echo off
"C:\Path\To\Rscript" "C:\Users\User\Desktop\print_test.R"
OR if paths do not have spaces:
#echo off
C:\Path\To\Rscript C:\Users\User\Desktop\print_test.R
I followed Sumedh's tip :
here is the new code:
#echo off
"C:\Program Files\R\R-3.2.4\bin\x64\r.exe" CMD BATCH "C:\Users\User\Desktop\print_test.R"
Related
I am creating an R script myscript.R which manipulates an excel file by means of XLConnectpackage.
The point is it refers to several external files: the excel file itself and another R file (functions library), so I need to set a working directory in the location of the script (so that relative paths to external files work properly).
I am using the following in my script
new_wd <- dirname(sys.frame(1)$ofile)
setwd(new_wd)
When I source the script from my RStudio it gets the job done. The problem is that the script is to be used by non-programmers, non-Rstutio-users, so I create .bat file (which I want to turn into an .exe one)
"C:\Program Files\R\R-4.0.3\bin\Rscript.exe" "C:\my\path\to\myscript.R"
It executes the script line by line but sys.frame(1) only works when sourcing.
How could I solve it?
Thanx
I have found a solution and it works properly.
From CMD command line or from a .bat file one can add an argument -e to the command, so that you can use a R language.
absolute\path\to\Rscript.exe -e "source('"relative\path\to\myscript.R"')"
It worked for me.
Besides, as Compo commented, I think there's no need for a .exe file, since a .bat does the job.
I want to execute an R script myscript.R by using Rscript.exe on a Windows machine. However, my default home folder is configured to be \\\\<domain>/home/m/myname/Documents. I've made a wrapper DOS batch script (run.bat) on the same folder where myscript.Rresides (actually a folder in a pendrive) in order to make it the current working directory:
#ECHO OFF
REM This batch file is an interface to run R scripts from the command line
SET RSCRIPT="c:\Program Files\R\R-3.4.3\bin\Rscript.exe"
echo "Changing drive and directory to %~dp0"
pushd "%~dp0"
REM we change "\" to "/"
setlocal enabledelayedexpansion
set f=%*
set "f=!f:\=/!"
%RSCRIPT% %f%
popd
I call the script by run.bat "myscript.R" --dir ../AnotherFolder
So far, so good. However, the problem is that the script needs to change the working folder to a folder one level up (../AnotherFolder) but the setwd() R function fails. By debugging, I can see that the problem is that Rscript.exe loads the script myscript.R while still having the default directory setup to the home folder.
The question is: how the heck I can make Rscript.exe to ignore the default home directory and take the script directory as the current working directory.
Ok, I've found my own answer after scratching my head the whole day. I've modified the wrapper batch file and add an extra command line argument --drive to be processed by my R script:
#ECHO OFF
REM This batch file is an interface to run R scripts from the command line
SET RSCRIPT="c:\Program Files\R\R-3.4.3\bin\Rscript.exe"
echo "Changing drive and directory to %~dp0"
pushd "%~dp0"
REM we change "\" to "/"
setlocal enabledelayedexpansion
set f=%*
set "f=!f:\=/!"
set P=%~dpnx1
%RSCRIPT% %f% --drive "%P:~0,2%"
popd
Then, my R script must execute setwd(drive) where drive contains the value passed through the --drive option (e.g. "F:") before executing a setwd() on a relative path to the current folder.
I have a Matlab function that finds the path where this function is within my pc and then runs a bat file on that same directory. This bat file is meant to execute an R script but for a strange reason is failing to do so.
This is my Matlab function:
function [] = myFunction(arg)
% Find the directory of the executing script
thisDir = fileparts(mfilename('fullpath'));
% Save arg as a csv on this directory, this will be read by my R script
tmpDir = strcat(thisDir,'/tmp.csv');
csvwrite(tmpDir,arg);
% Specify the command to run
dosCommand = ['call "' thisDir '/runRscript.bat"'];
dos(dosCommand);
end
The bat file has the following code:
"C:\Program Files\R\R-3.2.2\bin\x64\R.exe" CMD BATCH runRScipt.R
When I run the function in Matlab I get the below message:
C:\Users\...mypath...>"C:\Program Files\R\R-3.2.2\bin\x64\R.exe" CMD BATCH
runRscript.R
Since I get this message in Matlab I have no doubt it is finding and reading the batch file, but it fails to execute the R script. I know the bat file works as expected since I can run it through the command line (with the command that should be the "dosCommand" on the Matlab script) or by clicking twice on the .bat file.
I found the answer. For a strange reason the dos() command would not work, but the system() command will do the job. Then the code will look like this:
function [] = myFunction(arg)
% Find the directory of the executing script
thisDir = fileparts(mfilename('fullpath'));
% Save arg as a csv on this directory, this will be read by my R script
tmpDir = strcat(thisDir,'/tmp.csv');
csvwrite(tmpDir,arg);
% Specify the command to run
sysCommand = ['call "' thisDir '/runRscript.bat"'];
system(sysCommand);
end
And the batch file:
"C:\Program Files\R\R-3.2.2\bin\x64\R.exe" CMD BATCH runRScipt.R
Instead of R.exe please try Rscript.exe. R.exe runs R code in interactive mdoe while Rscript runs the code in batch mode. Ideally you should find Rscript executable in the same path as in R executable (i.e. "C:\Program Files\R\R-3.2.2\bin\x64" in your case)
I am using Notepad++ for creating scripts and opening my active files from menu "Run" -> "Open current dir cmd". This works fine and the result is:
c:\scripts>
What I would like to have is the filename that I am working on currently so that I could try testing it right away. In my scripts I use parameters to define input and output files. Therefore the script shouldn't be run while opening, rather to have the script typed into console:
c:\scripts>edit_text.pl
I would then add manually the needed input and output files
c:\scripts>edit_text.pl input.txt output.txt
How do I make this possible in Notepad++ "Run" -feature?
Currently it is defined in shortcuts.xml as
cmd /K cd $(CURRENT_DIRECTORY)
I suspect that it would be something like this:
cmd /K cd $(CURRENT_DIRECTORY) $(FILE_NAME)
The problem is that this will "execute" the filename as well. I would like to have it waiting on the console for my actions.
Thanks in advance!
The final code line would mean the CD and the script invocation being treated as one command. Separating then with && should help.
cmd /K cd $(CURRENT_DIRECTORY) && $(FILE_NAME)
However, that would do the CD then execute the command. I do not know of any way to enter a command but not execute it.
A poor solution would use the command below. You could copy and paste the echoed command then add in any parameters needed. Setting "Quick edit" mode on the window would make the copy and paste quicker.
cmd /K cd $(CURRENT_DIRECTORY) && ECHO $(FILE_NAME)
I have adopted a different approach for my own scripts although they do not have parameters that I need to enter. Edit the file (but not with Notepad++ as it overwrites the file just before it exits):
C:\Users\AdrianHHH\AppData\Roaming\Notepad++\shortcuts.xml
I have added some lines to the <UserDefinedCommands> section:
<NotepadPlus>
... unchanged
<UserDefinedCommands>
... unchanged
<Command name="Open containing folder" Ctrl="no" Alt="no" Shift="no" Key="0">explorer $(CURRENT_DIRECTORY)</Command>
<Command name="Open current dir cmd" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /K cd /d $(CURRENT_DIRECTORY)</Command>
<Command name="Run as command" Ctrl="no" Alt="no" Shift="no" Key="0">cmd /C "cd /d $(CURRENT_DIRECTORY) && $(FULL_CURRENT_PATH)"</Command>
<Command name="Explorer with selection" Ctrl="no" Alt="no" Shift="no" Key="0">explorer $(CURRENT_WORD)</Command>
</UserDefinedCommands>
... unchanged
</NotepadPlus>
I want to run a jar file with a .bat (the jar file doesn't seem to want to open on its own but thats a different issue for now) but as the java file runs for a long time, the command prompt remains open (while the .bat/.jar is still running)
I do not want this.
I read somewhere that you can use a .cmd file and the command(s):
cmd /c bat.bat
exit
To run a bat file without a command prompt. But that isn't working for me. When I click the .cmd program it just opens a command promopt and keeps printing "cmd /c bat.bat exit" over and over in a loop.
What am I doing wrong, was my .cmd command wrong? Is there another way to run a .bat without a command prompt remaining open?
Thanks alot.
From here:
Save the following as wscript, for instance, hidecmd.vbs after
replacing "testing.bat" with your batch file's name.
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c testing.bat"
oShell.Run strArgs, 0, false
The reference is here
http://msdn.microsoft.com/en-us/library/d5fk67ky.aspx