[R] executable file with R

Frank Samuelson gmane0506242.z.cudgel at neverbox.com
Fri Jun 2 18:38:07 CEST 2006


Romain Lorrilliere wrote:
> Hi,
> 
> I made an R function, and I want make an executable applet with it. Do 
> you know how it is possible?
> 


More details about what you want would be helpful.  Here
is what I do and it may be useful to you.
Under *nix,OSX, etc use bash's "here document" feature.
Create a script like the one below and make it executable.

#!/bin/bash
  R --vanilla << "EOF"      #  Pipe all subsequent lines into R.
  ################ Put all your R code here  ###############
  X11(); plot(1:3)
  require(tcltk)
  tkmessageBox(message="hello")
  ###########################end of R code #########################
EOF


Under the M$ XP operating system I attach my code to the end
of the following script and put it in a batch file, like "runthis.bat"
Then just double click (or whatever) on the "runthis.bat" file.


@echo off

:: A batch script for running R scripts in M$ XP
:: Just attach your R script to the end of this batch file (below the
:: last ":::" comment line) and run this script.

:: A portion of this file was taken from Gabor Grothendieck's
:: batchfiles http://cran.r-project.org/contrib/extra/batchfiles/
:: Copyright by Gabor Grothendieck 2005, GPL v2

setlocal
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: I could not get this to work on M$ 2k
ver | findstr XP >NUL
if errorlevel 1 echo Warning: This script only works on Windows XP.

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Find R.
:: use environment variable R_HOME if defined
:: else current folder if bin\rcmd.exe exists
:: else most current R as determined by registry entry
:: else error, not found.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
if not defined R_HOME if exist bin\rcmd.exe set R_HOME=%CD%
if not defined R_HOME for /f "tokens=2*" %%a in (
  'reg query hkcu\software\r-core\r /v InstallPath 2^>NUL ^| findstr InstallPath'
  ) do set R_HOME=%%~b
if not defined R_HOME for /f "tokens=2*" %%a in (
  'reg query hklm\software\r-core\r /v InstallPath 2^>NUL ^| findstr InstallPath'
   ) do set R_HOME=%%~b
if not defined R_HOME echo Error: R not found. Please install R (www.r-project.org). & pause & exit /b

set cmdpath=%R_HOME%\bin\R.exe
set thisfile=%~f0

:: Run R.  Make it parse the commands at the end of this file
echo x=readLines(Sys.getenv('thisfile'));eval(parse(text=x[-(1:grep('Put R code below',x)[2])]))  | "%cmdpath%" --vanilla

endlocal
exit /b

:::::::::::::::::::  Put R code below this line :::::::::::::::::::::

library(tcltk)   # My example R code
windows(); plot(1:3)
tkmessageBox(message="hello")
q()



More information about the R-help mailing list