[R] readline() and Rterm in Windows

Mikkel Grum mi2kelgrum at yahoo.com
Thu Nov 3 09:35:09 CET 2005


I've tried your proposal in a number of ways, and
there must be something I'm not understanding. If I
run your script (using source() in RGui, or ctrl-R
from the R Editor, I get:

> conout <- file('CONOUT$','w')
Error in file("CONOUT$", "w") : unable to open
connection
In addition: Warning message:
cannot open file 'CONOUT$', reason 'Permission denied'

>

so I added the path as in:

conout <- file('C:\\R\\R-2.2.0\\CONOUT$','w')
conin <- file('C:\\R\\R-2.2.0\\CONIN$', 'r')
cat('Please enter an ID:', file=conout)
flush(conout)
id <- readLines(conin, 1)
print(id)


Using RGui and ctrl-R from the R Editor, I get

> conout <- file('C:\\R\\R-2.2.0\\CONOUT$','w')
> conin <- file('C:\\R\\R-2.2.0\\CONIN$', 'r')
Error in file("C:\\R\\R-2.2.0\\CONIN$", "r") : 
        unable to open connection
In addition: Warning message:
cannot open file 'C:\R\R-2.2.0\CONIN$', reason 'No
such file or directory' 
> cat('Please enter an ID:', file=conout)
> flush(conout)
> id <- readLines(conin, 1)
Error in readLines(conin, 1) : object "conin" not
found

and with
> source("foo.R")
Error in file("C:\\R\\R-2.2.0\\CONIN$", "r") : 
        unable to open connection
In addition: Warning message:
cannot open file 'C:\R\R-2.2.0\CONIN$', reason 'No
such file or directory' 
> 

When I create a batch file with the following command
:
C:\R\R-2.2.0\bin\Rterm.exe --vanilla
<C:\R\R-2.2.0\foo.R> C:\R\R-2.2.0\foo.out

and double click on the batch file, the out file gives
me:

R : Copyright 2005, The R Foundation for Statistical
Computing
Version 2.2.0  (2005-10-06 r35749)
ISBN 3-900051-07-0
. . .
Type 'q()' to quit R.

> conout <- file('C:\\R\\R-2.2.0\\CONOUT$','w')
> conin <- file('C:\\R\\R-2.2.0\\CONIN$', 'r')

and nothing else. In none of the situations do I get
prompted for input. What am I doing hopelessly wrong?

Mikkel

--- Duncan Murdoch <murdoch at stats.uwo.ca> wrote:

> Mikkel Grum wrote:
> > Duncan and Prof, thanks for your comments and
> > apologies for not being more specific. I'm not
> getting
> > the same results you get from the steps you
> propose.
> > 
> > If I write a script foo.R with two lines
> > 
> > 	id <- readline("Please enter an ID: ")
> > 	id
> > 
> > and then use source("foo.R") (either at the Rterm
> > prompt, or in RGui) it is true that get prompted,
> but
> > the second line does not visibly run, i.e. I get
> > 
> >>source("id.r")
> > 
> > Please enter an ID: 5
> > 
> > 
> > and if I then type id, I get
> > 
> >>id
> > 
> > [1] "id"
> > 
> > If I cut and paste the two lines in RGui (in one
> go),
> > I get
> > 
> >>id <- readline("Please enter an ID: ")
> > 
> > Please enter an ID: id
> > 
> > 
> > What I really want is a batch file on the desktop
> with
> > the following commands:
> > 
> >    c:\r\R-2.2.0\bin\Rterm.exe --no-save
> --no-restore
> > <script.R> script.out 2>&1
> >    c:\texmf\miktex\bin\latex
> > \nonstopmode\input{blue.tex}
> > 
> > 
> > and script.R reads something like:
> > 
> >    id <- readline("Please enter an ID: ")
> >    id
> >    Sweave("blue.Rnw")
> > 
> > I said that script.R didn't run, which was an
> > incorrect description. It runs without prompting
> for
> > the ID, and gives error messages all through
> because
> > blue.Rnw needs the id.
> > 
> > This is a very simplified version of what I'm
> doing,
> > but if I use only the first line of the batch file
> and
> > the first two lines of the script and could get
> that
> > to work, I could figure out the rest.
> 
> It won't work so simply.  You're redirecting stdin,
> so user input would 
> be taken from there; you're redirecting stdout and
> stderr, so the prompt 
> won't be visible to the user.
> 
> You need to open new handles to the console.  The
> code below will do it 
> in Windows; the syntax to specify the console in
> Unix-alikes will be 
> different (but I don't know what it is).
> 
> conout <- file('CONOUT$','w')
> conin <- file('CONIN$', 'r')
> cat('Please enter an ID:', file=conout)
> flush(conout)
> id <- readLines(conin, 1)
> print(id)
> 
> Duncan Murdoch
>




More information about the R-help mailing list