Richie Cotton
Richie Cotton

Reputation: 121057

Starting R and calling a script from a batch file

I have an R-based GUI that allows some non-technical users access to a stats model. As it stands, the users have to first load R and then type loadGui() at the command line.

While this isn't overly challenging, I don't like having to make non-technical people type anything at a command line. I had the idea of writing a .bat file (users are all running Windows, though multi-platform solutions also appreciated) that starts R GUI, then autoruns that command.

My first problem is opening RGui from the command line. While I can provide an explicit path, such as

"%ProgramW6432%\R\R-2.15.1\bin\i386\Rgui.exe"

it will need updating each time R is upgraded. It would be better to retrieve the location of RGui from the %path% environment variable, but I don't know an easy way to parse that.

The second, larger problem is how to call commands for R on startup from the command line. My first thought is that I could take a copy of ~/.Rprofile, append the extra command, and then replace the original copy of the file once R is loaded. This is awfully messy though, so I'd like an alternative.

Running R in batch mode isn't an option, firstly since I can't persuade GUIs to display themselves, and secondly because I would like the R console available, even if the users shouldn't need to use it.

If you want a toy GUI to test your ideas, try this:

loadGui <- function()
{
  library(gWidgetstclck)
  win <- gwindow("test")
  rad <- gradio(letters[1:3], cont = win)
}

Upvotes: 3

Views: 2685

Answers (4)

RobS
RobS

Reputation: 3857

Using the answer from https://stackoverflow.com/a/27350487/41338 and a comment from Richie Cotton above I have arrived at the following solution to keeping a script alive until a window is closed by checking if the pointer to the window is valid.

For a RGtk2 window created and shown using:

library(RGtk2)
mainWindow <- gtkWindow("toplevel", show = TRUE)

Create a function which checks if the pointer to it exists:

isnull <- function(pointer){
  a <- attributes(pointer)
  attributes(pointer) <- NULL
  out <- identical(pointer, new("externalptr"))
  attributes(pointer) <- a
  return(out)
}

and at the end of your script:

while(!isnull(mainWindow)) Sys.sleep(1)

Upvotes: 0

Greg Snow
Greg Snow

Reputation: 49640

I have done similar a couple of times. In my cases the client was using windows so I just installed R on their computer and created a shortcut on their desktop to run R. Then I right click on the shortcut and choose properties to get the propertiest dialog. I then changed the "Start in" folder to the one where I wanted it to run from (which had the .Rdata file with the correct data and either a .First function in the .Rdata file or .Rprofile in the folder). There is also a "Run:" option that has a "Minimized" option to run the main R window minimized.

I had created the functions that I wanted to run (usually a specialized gui using tcltk) and any needed data and saved them in the .Rdata file and also either created .First or .Rprofile to run the comnand that showed the gui. The user double clicks on the icon on the desktop and up pops my GUI that they can work with while ignoring the other parts.

Upvotes: 2

Dominik
Dominik

Reputation: 2813

Take a look at the ProjectTemplate library. It does what you want to do. It loads used libraries from a batch file an run R files automatically after loading as well as a lot of other usefull stuff as well...

Upvotes: 1

Dirk is no longer here
Dirk is no longer here

Reputation: 368201

Problem 1: I simply do not ever install in the suggested default directory on Windows, but rather group R and a few related things in, say, c:/opt/ where I install R itself in, say,c:/opt/R-current so that the path c:/opt/R-current/bin will remain constant. On upgrade, I first renamed to R-previous and then install into a new R-current.

Problem 2: I think I solved that many moons ago with scripts. You can now use Rscript.exe to launch these, and there are tcltk examples for waiting for a prompt.

Upvotes: 4

Related Questions