Run windows CMD in R - r

I am a beginner at R. If this question sounds childish to you then pardon me.
I need to run the following command in R studio by using Windows CMD.
copy *.txt combinedfiles.txt
This command actually combines all the files into one file in Windows CMD.
I tried using system() and system2(). But I could not get the results.
e.g.:
system(cmd.exe copy *.txt combinedfiles.txt)
Kindly help me out with this problem.

copy is not a standalone program, but a builtin command of the Windows commandline interpreter. To run this, use shell in R.
shell("copy *.txt combinedfiles.txt")

Related

"Command not found" error when trying to run scripts from R (Windows 10 and Git Bash)

I have been trying to learn how to use my R scripts in a bash like Git, so I can run said script in a pipeline, but I am having trouble getting started. I am using a Windows 10 system, and using the Git Bash as my mock Unix bash.
Here is the link to the tutorial I am trying to learn off of:
http://swcarpentry.github.io/r-novice-inflammation/05-cmdline/index.html
As a super simple example of a script, I made a script called session-info.R in RStudio. The only code it contained was:
sessionInfo()
I then went to Git Bash, and making sure that I was in the same directory as the script that I just wrote, I typed this into the command line:
Rscript session-info.R
To which I got the error: "bash: Rscript: command not found"
Does anyone have any ideas to how I could fix this? I feel like this is a really beginner error, but I haven't been able to figure out for a little while. Thank you for your assistance.

How can I call a Windows Terminal inside an R script?

I need to call a Windows Terminal inside a R script,
I asked a similar question before, but using a Linux Terminal:
How to call linux terminal code from R script
I am using cdo (Climate Data Operators) in Linux, inside a R code
system(paste0("cd ~/Data/; cdo -f nc copy file1.grb2 file2.nc;"))
The answer in Linux is so simple, and it did the trick perfect. How can I do this in Windows?

How to implement this solution to make R scripts interactive?

I have found this answer to a problem I had discussed previously here: How to get dialog/message box working in executable R file
The answer is here:
https://thesquareplanet.com/blog/interactive-r-scripts/
Specifically this bit:
We can use this to provide an interactive R script executor in /usr/local/bin/Rint: #!/bin/sh f=$1; shift; env "R_PROFILE_USER=$f" "ARGS=$#" R --no-save -q
My problem is that I know nothing about the command prompt or batch files. So what does this line mean, and do I just paste it into the command prompt once and then add "#!/usr/local/bin/Rint" to the top of my executable R script?
This line is not for a Windows cmd.exe shell. This is for a UNIX/Linux shell.
We can use this to provide an interactive R script executor in /usr/local/bin/Rint:
#!/bin/sh
f=$1;
shift;
env "R_PROFILE_USER=$f" "ARGS=$#"
R --no-save -q
For anyone still looking, this is the best resource I can find about executable R GUIs in Windows:
http://oddhypothesis.blogspot.com.au/2014/04/deploying-self-contained-r-apps-to.html
Still working through this tutorial, will update if it answers the question.

Execute R script in R studio using batch file

I want to execute a R script in R Studio using batch file. I know how to execute R script using batch file in R though. When I try to execute using the following:
"C:\Program Files\RStudio\bin\rstudio.exe" CMD BATCH --vanilla --slave "C:\Users\kpappala\Desktop\R schedule\task.R"
It just opens R studio but doesn't execute. Is there a way?
Thanks!
Rstudio is an IDE for R. It isn't R though. It doesn't really even make sense to run it in batch through rstudio.
If you're just saying you want to run a file from within Rstudio that's different and you can just source it or run it in batch via system using a call to R CMD BATCH.
This question was answered here:
batch execute R script.
In short and as Dason said, you Rstudio is just a shell, and doesn't actually run the code. For that use R. Try this instead:
PATH C:\Program Files\R\R-3.1.0\bin;%path%
Rscript "C:\Users\kpappala\Desktop\R schedule\task.R"

How to run a R language(.r) file using Batch file?

I want to run a R script file (.r) using batch file.
If R.exe is in your PATH, then your windows batch file (.bat) would simply consist of one line:
R CMD BATCH your_r_script.R
otherwise, you need to give the path of R.exe, so for example:
"C:\Program Files\R\R-2.13.0\bin\R.exe" CMD BATCH your_r_script.R
you can add certain input arguments to the BATCH command, such as --no-save, --no-restore
so it would be
R CMD BATCH [options] your_r_script.R
more info on options, etc at http://stat.ethz.ch/R-manual/R-devel/library/utils/html/BATCH.html
Note: R uses the command "BATCH" to non-interactively evaluate a script located in a file. Here we are running the command "BATCH" from a windows .BAT file, but that's merely a coincidence.
An answer to another question suggests using Rscript.exe, so your batch file would contain:
"C:\Program Files\R\R-3.0.2\bin\i386\Rscript.exe" your_script.R
pause
It is a good idea to add R to the windows environment path. In a comment in this question #chase gave a link that explains how to set the path on windows 7. Once R is added to the windows path, your batch file should become simply :
Rscript.exe your_script.R
pause
You can also directly call a R command by using the -e flag. For example this batchfile will tell R to set its current working directory to Documents, then it will print the working directory:
Rscript.exe -e setwd('Documents');getwd()
pause
I struggled with the syntax with the answers below, but this worked for me in the .bat file:
C:\Windows\System32\cmd.exe /k ""path to Rscript.exe"
"path to .R script""
Be sure to place both the path to Rscript.exe and the script in "" together and separately as above.
I doubt you will be able to run it using a batch file.
http://www.fileinfo.com/extension/r
Most known programs that use .r files do so for source code files it looks like. You will probably have to compile it using the program it was written for. I guess you could use a command line compiler from a batch file, but I don't know what language or applications you are using.
If you post the script file or give more information about it, we could probably help you better.

Resources