Using R and with the Terminal - r

I have combed the internet looking for a tutorial on how to use R through the terminal and I can't find anything that exactly fits what I'm looking for. I have a group of R scripts. They need to be run in a specific order. Each produces a set of .csv files to be used as inputs for the next script that will be executed.
I have used R CMD BATCH filename.R and it makes an output file called filename.Rout
But then when I try to use cat filename.Rout, I get an error Fatal error: cannot open file 'filename.R': No such file or directory
Also I'm not sure where my output (.csv files) should be written to. In my R script, there are some write.csv lines pointing the new datasets to be written to a specific directory but when I run R CMD BATCH filename.R in the Terminal, I don't see the files where they should be. I see a filename.Rout in another directory but I can't open it.

Related

Can I prevent R from creating .RData files?

I am trying to share some R code with colleagues who don't know R. I have created a batch file so they can just double-click it and run the R script without even opening R. But it creates a .RData file.
My question is, can I prevent R from creating the .RData file?
I've read here Disable saving history that I could disable it through RStudio global options but my colleagues are installing just R and won't need to ever open it, so I am looking for some kind of solution of the likes of options(...) that I can just put in my Rscript, or maybe something that could be speficied in the batch file call.
For anyone curious, I figured it out thanks to #r2evans and this post: https://es.stackoverflow.com/questions/166211/evitar-que-r-cree-ficheros-r-data-y-r-history.
I just had to add --no-save to my batch call like this:
R CMD BATCH --no-save "%file%" NUL
where %file% is the Rscript path. The NUL part is so a .Rout file won't be created after running the batch file.
Also, doing
Rscript.exe "%file%"
runs the Rscript and doesn't produce .RData or .Rout files. The difference is that R messages get printed on the command window as they would in an interactive R session.

Run .R functions via CMD - missing execute all command?

Good day,
I want to automate data cleaning and merging via R. I have created multiple folders to which you upload excel files. Than with R I clean the data and merge them to a final file.
I would like to do this process without running R or Rstudio. To accomplish this I run R via CMD (.bat), however, the code does not run.
In my R script I have multiple functions calling from other files, I assume that I need a simple "Run all command" to execute the code via CMD?
Example of Combine_all_excel_files.R
setwd("C:\\Code source\\")
source("Seperate_input_folder_by_years.R")
Seperate_input_folder_by_years()
Merge data.bat file:
start "" "C:\Program Files\R\R-3.3.3\bin\x64\Rscript.exe" CMD BATCH "C:\Code source\Combine_all_excel_files.R"
pause
When I run the .bad file, the code does not run. What I am doing wrong?

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

Does Rscript on Linux take a snapshot of the file before executing the script?

If I use the shell command Rscript MyFile.R to run my rscript, and then, while it is running, I change and save MyFile.R, will the rscript execute based on the file at the time of execution? Or does it read one line at a time of the file and execute the updated file?
The source file is being read line-by-line, so it is not safe to edit the source file while it is being executed by R. Technically, Rscript executes R --file, the reading of the input is then implemented in platform-dependent code. On Unix, the input is read using fgets, line-by-line, when a file was given.
Whether and when R will actually read the modified source file depends on the OS (buffering by the I/O library, should be line-by-line) and on the editor (e.g. if the editor lets the user edit a copy of the file and then rename at the end, R will keep reading the old file). I can observe both behaviours (changes edited in joe not impacting execution, changes appended by cat impacting execution).

When trying to run R script from CMD, getting "Fatal error: cannot open file 'CMD': No such file or directory"

I'm trying to create a batch file to run an R script for me automatically on Windows 7.
Batch file:
"C:\Program Files\R\R-3.2.3\bin\Rscript.exe" CMD BATCH "C:\Users\<my username>\Documents\R\Script Testing\script.R"
pause
R script:
write.csv("hello", "automatic_output.txt")
This script works when I run it in R, but when I try to run the .bat, I get this error:
Fatal error: cannot open file 'CMD': No such file or directory
This happens regardless of whether I paste the code into the command prompt, run the .bat as administrator, or schedule the task.
I tried following the advice given in this thread:
Exporting .csv from R & Batch file
but am seeing the same behavior.
I also used the Windows search to try to find "automatic_output.txt" in case it was being stored in an unexpected location but no results came up.
Lastly,
https://stackoverflow.com/a/23514987 seemed to suggest that I could omit the "CMD BATCH", and when I do I no longer get an error, but automatic_output.txt is still not produced.
Any ideas?
As Thomas pointed out, I needed to change "Rscript.exe" to "R.exe". For future googlers, here is the line of code that worked:
"C:\Program Files\R\R-3.2.3\bin\R.exe" CMD BATCH "C:\Users\<my username>\Documents\R\Script Testing\script.R"

Resources