user3734010
user3734010

Reputation: 35

Readline for user input overwrites previous entry in R

I'm trying to create a user input to create a list of stocks to be used to retrieve data from the internet. I'm using readline to request the input, but each time I enter a symbol it overwrites the previous entry. Is there a way to get the user to input and store the 5 symbols to be used in another package? Below is what I'm using, any help is greatly appreciated.

##Input stock symbols to create a data series to optimize
Stk.List <- NULL
n <- 0:5
#for (i in seq (along=n)) {
for (i in seq(5)) {
  if (n[i] < 5) {
    Stk.List <- c(readline(prompt = "Input Stock Symbol: "))
  }
}

Upvotes: 0

Views: 144

Answers (2)

WheresTheAnyKey
WheresTheAnyKey

Reputation: 878

I would use append.

 ##Input stock symbols to create a data series to optimize
 n <- 0:5
 Stk.List <- c()
 for (i in seq(5)) {
    if (n[i] < 5) {
    newstock <- readline(prompt = "Input Stock Symbol: ")
    Stk.List <- append(Stk.List, newstock)
    }
 }

Upvotes: 1

gagolews
gagolews

Reputation: 13056

You can always use readLines() to input a desired number of character strings, e.g.

cat('Input 5 stock symbols:\n')
readLines(n=5)

Otherwise, in your for loop use e.g. Stk.List <- c(Stk.List, readline(prompt = "Input Stock Symbol: "))

Upvotes: 1

Related Questions