I would like to use the directory of my bat file to run my R script.
My R script is in the same directory as my bat file, I tried:
"C:\Program Files\R\R-3.1.2\bin\x64\RScript.exe"
"%CD%\script_to_run.R"
but the cmd immediately closes.
This works if I specify the entire path of my script instead of using %CD%.
Can I have some help, please?
The simplest fix is to use:
"C:\Program Files\R\R-3.1.2\bin\x64\RScript.exe" "%~dp0script_to_run.R"
Where %0 references the running batch script and %~dp0 references the drive and path of the running batch script, (ending with a trailing back slash).
To start, or run a program in a batch script, you have to right start at the beginning. It would look like this,
start yourfilepath
I hope this helps, if not, tell me and I will try to help.
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 am trying to understand the working of "make" command (just started on this command). I have an ".sh" file which has a script to execute "make" command as shown below:
source /somepath/environment-setup-cortexa9hf-vfp-neon-poky-linux-gnueabi
make arch=arm toolchainPrefix=arm-poky-linux-gnueabi- xeno=off mode=Debug all
The directory where the script file is located has a file named "makefile". but there is nothing specified in the script file above regarding this "makefile". After executing the script file, all the script withing "makefile" is executed automatically. Can someone explain the working of "make xyz all" command in few words.
Thanks
As often with UNIX systems the command works to some degree by conventions. make (the GNU version of make at least) will search the working directory for files called GNUmakefile, makefile, and Makefile in that order or you can use the -f (or --file) option to give it a specific file.
I created an AutoIt script to install my executable. But when I run it, nothing is executed. My script:
Run("agent.exe", "C:\temp")
After saving and compiling (using Ctrl + F7), nothing is executed. Why?
Try this:
Run("C:\temp\agent.exe")
Your code is telling it to run agent.exe in the current directory and telling agent.exe to use C:\temp as its working directory.
It is recommended to use absolute paths. Otherwise you may end up in a situations like this one.
By using just a filename "agent.exe" your script assumes that the file is in the current working directory. This is fine as far as the working dir doesn't change.
Use this For example:
Run (#ScriptDir & "\agent.exe", #HomeDrive & "\temp")
I've been digging into several places for 2 simple needs but couldn't find a final answer.
I'm running an R script in batch mode. Not sure whether my solution is the best one but I'm using R CMD BATCH as per http://stat.ethz.ch/R-manual/R-patched/library/utils/html/BATCH.html included in a bat file.
First I'd like to have the directory where the R script is saved set up as the working directory rather than where the bat file is saved.
Secondly I'd like to divert all the output from the R script (csv files and charts) to a specific directory other than the working directory. I cannot find any options for such basic requirement.
The final idea is to be able to run the bat file across different computers no matter where the R script is saved.
Thanks
You don't give code so my answer will be just an advise or what I would do for such job.
Use Rscript.exe it is the way to go for batch script. R CMD is a sort of legacy tool.
You don't need to set or change the working directory. It is a source of problems
You can launch you bat file where you want and within it you go to R script location using cd for example you bat file can be like:
cd R_SCRIPT_PATH
Rscript youscript.R arg1 arg2
You can use one of the script argument as an output directory for your result files. For example inside your script you do somehing like this:
args <- commandArgs(trailingOnly = TRUE)
resultpath <- as.character(args[1])
.....
write.table(res1, file=paste(resultpath,'res1.csv',sep='/')
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