For a tcltk application, I would like to start an R script without opening a console window or starting a DOS box. I already figured out that by creating a link to RScript.exe I can force the console window to start minimized, but I wonder if I can avoid the console window at all?
You want to "run R in batch mode". It's quite straightforward; there are some instructions here.
EDIT: I don't see a console window; here are the steps I took.
1) I created a file named r.bat containing the line Rterm --vanilla and saved it in the R startup working directory (as given by Sys.getenv("USERPROFILE")).
2) I created a test R script, test.r, that would take several seconds to run (so I'd have chance to see any windows popping up.
n <- 1e3
for(i in 1:10)
{
qr.solve(matrix(runif(n*n), nrow = n), seq_len(n)/(n+1))
}
Obviously you can run any script that you like.
3) I opened a dos command prompt in the same dir as r.bat and typed R <test.r> test.txt.
Related
Let's assume I have an R script located in C:\Users\user\myscript.R. How can I assign a hotkey (e.g., F1) so that every time that I press that hotkey the R script will be executed in the background (i.e., without opening Rstudio)?
Note:
I use Windows 10 and have AutoHotkey installed which might help to bind the script to the key.
This solution involves three steps:
1) Create a .bat file that executes the R script (as suggested by Daniel O):
runscript.bat (located in C:\Users\user\runscript.bat)
"C:\Program Files\R\R-4.0.0\bin\R.exe" CMD BATCH "C:\Users\user\myscript.R"
2) Bind the .bat script to the Home hotkey with the open-source software AutoHotkey by creating a .ahk script (as suggested by D. Pardal):
bindscript.ahk (located in C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp so it will automatically load-up on start-up, see this)
Home::Run, runscript.bat, C:\Users\user
3) Set environment variable for R on windows 10 following this tutorial.
press win+r, type "shell:startup", and press enter. It should bring up file explorer. There, you can create a different script and write this inside it:
F1::
Run, C:\Users\user\myscript.R
This will make the trigger script run at startup, so that whenever you press f1, myscript.r will run
I want to launch Rcmdr as a command from bash (or any unix shell), perhaps as an alias. R accepts the CMD argument and I could also pipe a script in with <. I would like the R console to stay open, and an interactive RCommander session to be started (Rcmdr is a popular GUI for R, for any newbies reading along, and it seems that you start up R, type library(Rcmdr) and then Commander() to start it up).
I am aware of how to add Rcmdr to my profile, and it appears to always start up if I include library(Rcmdr) in my .Rprofile, on my Linux workstation.
If I pipe my input in with < then this script works up to the point where it says that Commander GUI is launched only in interactive sessions:
library(Rcmdr);
Commander();
However if I run R CMD BATCH ./rcommander.r it just starts up and shuts down immediately, probably giving me some warning about interactive sessions that I didn't see, because CMD BATCH puts R into non-interactive mode and is thus useless for the purpose of "injecting" Rcmdr into an interactive R session.
It appears impossible to "source a file on the command line but run interactively" in R. It also appears that there are command line options to ignore the global and the user profile, but not to specify a custom profile like R --profile-custom ./.Rprofile2
Either I would like to specify a profile that means "Right now I want to start up and use RCmdr" and still be able to run R without it sometimes.
Working on an Ubuntu machine here, I was able to use the advice provided by Dirk in this mailing list post:
nathan#nathan-laptop:~/tmp$ cat rcommander.r
#!/bin/bash
r -lRcmdr -e'while(TRUE) Commander();'
nathan#nathan-laptop:~/tmp$ cat rcommander2.r
#!/bin/bash
Rscript --default-packages=Rcmdr -e 'while(TRUE) Commander();'
The first script uses Dirk's littler package, available on CRAN, and the second uses the standard Rscript executable. As noted, you can kill the process with ctrl + c from your terminal.
I'm effectively trying to tack save.image() onto the end of a script without modifying that script.
I was hoping something like Rscript target_script.R | saveR.R destination_path would work, where saveR.R reads,
args.from.usr<-commandArgs(TRUE)
setwd(args.from.usr[1])
save.image(file=".RData")
But that clearly does not work. Any alternatives?
You can write an R script file that takes two parameters: 1, the script file you want to run, and 2, the file you want to save the image to.
# runAndSave.R ------
args.from.usr <- commandArgs(trailingOnly=TRUE)
source(args.from.usr[1])
setwd(args.from.usr[2])
save.image(file=".RData")
And then run it with
Rscript runAndSave.R target_script.R destination_path
You could try to program a task to be done within the OS of that computer. In Linux you will be using the terminal and there is this tool called CRON. In Windows you can use Task Scheduler. If you program the OS to open a terminal and load an script and later save the image, you maybe will get what you need, save the data generated from the script without actually modifying it.
I have created a batch file to launch R scripts in Rterm.exe. This works well for regular weekly tasks. The < PBWeeklyMeetingScriptV3.R > is the R script run by Rterm.
set R_TERM="C:\Program Files\R\R-2.14.0\bin\x64\Rterm.exe"
%R_TERM% --slave --no-restore --no-save --args 20120401 20110403 01-apr-12 03-apr-11 < PBWeeklyMeetingScriptV3.R > PBWeeklyMeetingScriptV3.batch 2> error.txt
I've tried to modify this to launch the R GUI instead of the background process as I'd like to inspect and potentially manipulate and inspect the data.
If I change my batch file to:
set R_TERM="C:\Program Files\R\R-2.14.0\bin\x64\Rgui.exe"
the batch file will launch the R GUI but doesn't start the script. Is there a way to launch the script too?
Alternatively is there a way to save/load the work space image to access the variables that are created in the script?
You can save and load workspaces by using save.image() and load(). I do this all the time when scripting to pass data sets between two separate script files, tied together using Python or bash. At the end of each R script, just add:
save.image("Your_image_name.RData")
The image will be the workspace that existed whenever the command was run (so, if it's the last command in the file, it's the workspace right before the exist of the file). We also use this at my job to create "snapshots" of input and output data, so we can reproduce the research later. (We use a simple naming convention to get the time of run, and then label the files with that).
Not sure about launching and then running the GUI with specific scripts in it; I don't think that's a feature you'll find in R, simply because the whole point of running a batch file is usually to avoid the GUI. But hopefully, you can just save the image to disk, and then look at it or pass it to other programs as needed. Hope that helps!
I'm trying to run interactive R (Windows XP) with an input script that runs a few commands, and then leaves me at the R command line prompt. However, when I run it, it exits.
For example, here's the input file:
test.r:
x = 1
x
Here's what happens when I run it with the input file as a parameter:
C:\>R --file test.t
>x = 1
>x
[1] 1
C:\> <--- exits and returns to prompt
Is there any way to run this without R exiting?
I'd do it the other way around.
Just write a script that starts the normal R GUI or terminal application (per your choice), but then also place a file .Rprofile in the same directory which contains the code you want executed each and every time.
See help(Startup) on details about the files R looks up at startup and this may become clearer.