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
Is this what you want to do:> x.f <- function(){+ x <- list() + for (i in 1:10){ + z <- readline(paste("Parameter ", i, ": ", sep='')) + # just store the input in a list with the same key + x[[z]] <- z + } + x + }> x.f()Parameter 1: 12 Parameter 2: 2 Parameter 3: asdf Parameter 4: 33 Parameter 5: scs Parameter 6: sdaf2 Parameter 7: 234 Parameter 8: gfd Parameter 9: 123 Parameter 10: aa $`12` [1] "12" $`2` [1] "2" $asdf [1] "asdf" On Thu, Apr 3, 2008 at 1:18 PM, Christopher Marcum <cmarcum at uci.edu> wrote:> 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Your example runs fine at my pc, except from the readLines("File with 30 ....) part. But this is probably a striped code, so maybe you should take a look at flush.console(). Bart Christopher Marcum wrote:> > 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. > >-- View this message in context: http://www.nabble.com/iterative-loop-with-user-input--tp16481541p16489236.html Sent from the R help mailing list archive at Nabble.com.
Possibly Parallel Threads
- String manipulation, insert delim
- [FileCheck] RFC: Add support for line anchors.
- [LLVMdev] "anchor" method policy, request for clarification
- [LLVMdev] "anchor" method policy, request for clarification
- [cfe-dev] [FileCheck] RFC: Add support for line anchors.