[R] Problems with scan

David C. Howell David.Howell at uvm.edu
Tue Nov 4 21:34:46 CET 2008

I have been having problems with using scan(). I searched the archives 
and found someone with the same problem several years ago, but did not 
find a solution.

I want to prompt the user for input, scan in that input, and then go on 
to use it as a variable.

The simplest version of my program is

cat("enter m","\n")
m <- scan(n = 1, quiet = T)

If I enter these lines one at a time, they work perfectly and I get the 
following output, which is just want I want..
 > cat("enter m","\n")
enter m
 > m <- scan(n = 1, quiet = T)
1: 24
 > m
[1] 24

However if I submit the three lines together, as you would when running 
a program, I get

 > cat("enter m","\n")
enter m
 > m <- scan(n = 1, quiet = T)
1: m
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, 
na.strings,  :
  scan() expected 'a real', got 'm'

In this case the program is reading the line after the scan function as 
the input to scan, rather than pausing for my entry. The help archives 
suggested putting this as a function "e.g., getn()" but that gives the 
same result.

The scan help file doesn't address this and the help archives don't give 
me what I need. How do I cause the program to wait for keyboard entry 
from me?

I am running version 2.8 on a Windows machine, but the problem goes back 
to much earlier versions.

While I'm asking, is there a way to suppress the echoing of lines? I 
would like to omit the lines above that begin with ">". I cannot find 
"echo" using help.search and can't think of another name to search under.


Dave Howell

David C. Howell
PO Box 770059
627 Meadowbrook Circle
Steamboat Springs, CO

More information about the R-help mailing list