[R] iterative loop with user input?

From: Christopher Marcum <cmarcum_at_uci.edu>
Date: Thu, 03 Apr 2008 11:18:21 -0700 (PDT)


Hello R-Users,

I would like to use an iterative loop to collect user input from within a function. I'm sure that this would be some combination of "for","break", and "next" but have not been able to get the syntax down.

I would like to print some text to the screen at each step in the loop, ask the user for input which is saved in an object, and then advance the loop.

Here is an example:

#anchor is a file with unique ids

anchor<-rep(1:30)
anchor<-paste(anchor,"uid",sep="")

#codelist is where I would like to store user input
codelist<-NULL

for(i in 1:30) {
#tell the user which ID is being coded

print(paste("You are coding unique ID",anchor[i],sep=": "))
#Read a line from a text file:

print(readLines(file="file_with_30_lines.txt",warn=F)[i])
#Ask the user for input

codelist[i]<-readline(paste("Select one of the following: \n \t please enter 1, 2, or 3: \n Enter Your Response HERE: ",sep="")) }

Ideally, loop should work from inside a function.

Any tips? Thanks in advance for your time and patience.

Best,
Chris Marcum
Sociology/Cal(it)^2
University of California-Irvine



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 03 Apr 2008 - 18:37:29 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 Fri 04 Apr 2008 - 09:30:26 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