[R] Reading data from the console

Doru Cojoc dcojoc at clemson.edu
Fri Jul 11 22:29:20 CEST 2003

I want to be able to write a program in R that does the following:
- it allows the user to enter the dimensions of the matrix from the
- it allows the user then to enter each element of the matrix from the

I am looking for an equivalent for the C++ command read, or read.ln.
read.table would not work, since the data is not in a table and,
furthermore, since the data does not exist prior to the execution of the
program; the user has to be able to introduce the data from the console.
scan would not work either. For example, in the following bit of code

cat('Number of populations:', '\n')
m<-scan("",n=1, quiet=TRUE)
cat('Number of categories:', '\n')
k<-scan("",n=1, quiet=TRUE)
for(i in 1:m) for(j in 1:k) {
 N[i,j]<-scan("",n=1, quiet=TRUE)

scan will take m to be the next command rather than wait for me to
introduce the number I want for m.

Any ideas? Thanks a lot!

Doru Cojoc

More information about the R-help mailing list