[R] Problems with scan

From: David C. Howell <David.Howell_at_uvm.edu>
Date: Tue, 04 Nov 2008 13:34:46 -0700

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

R-help_at_r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Tue 04 Nov 2008 - 20:38:15 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 04 Nov 2008 - 22:30:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive