| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

RTip - Batchfiles

Page history last edited by PBworks 17 years, 11 months ago

Here is a little example for a batch file under MS Windows XP.

Copy and paste the code into a text file an call it something like Runthis.bat.

This shows how you could develop a little application for users without any R knowledege at all, just a doubleclick on the icon and that's it.

 

 

@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();

x=seq(-4,4,0.1)

plot(sin(x)~x, t="l", ylab="sin(x)", xlab="x")

tkmessageBox(message="Hello World!")

q()

Comments (1)

Anonymous said

at 5:10 pm on Nov 15, 2006

Couldn't get this to work. Any tips?

You don't have permission to comment on this page.