简体   繁体   中英

Is there any restriction in R programming to take user input in switch case statement

I am trying to create a list with string, numbers, vector and logical values so I used the concept of do-while loop which I implemented using repeat but I am getting unexpected symbol error

l<-list()
c<-1
cat("1.To enter string\n2.To enter numbers\n3.To enter vector\n4.To enter logical values\n5.To 
terminate")
repeat({ 
    ch<-as.integer(readline(prompt="Enter your choice : "))
    result<-switch(
        ch,
             1<-
                l[c]<-as.character(readline(prompt="Enter a string : "))
                c<-c+1,
             2<-  
                l[c]<-as.numeric(readline(prompt="Enter a number : "))
                c<-c+1,
             3<-
                len<-as.numeric(readline(prompt="Enter the length of the vector : "))
                vec<-vector("character",len)
                for(i in 1:len){
                   vec[i]<-as.numeric(readline(prompt=paste("Enter the value for vec[",i,"] : ")))
                }
                l[[c]]<-vec
                c<-c+1,
             4<-
                l[c]<-as.logical(readline(prompt="Enter a logical value : "))
                c<-c+1,
             5<-
                print("Terminate")
     
        )

 }; if (ch==5) {break} )
 cat("\nList:")
 print(l)

this is the error ~/.active-rstudio-document:10:12: unexpected symbol 9: l[c]<-as.character(readline(prompt="Enter a string: ")) 10: c ^

There are several things going wrong here, namely the syntax used in the switch (ex. 1 <-... ) and the multi-line instructions for each option. A few suggestions:

  1. I think this would be more clear using a for-loop rather than repeat. Especially since you are just trying to do something 5 times.
  2. You are trying to do too many things at once. It hurts readability and makes your code much more difficult to interpret. I suggest breaking this into functions that have a specific action.

One way to implement this is below.

prompts = c("Enter a string: ",
            "Enter a number: ",
            "Enter a vector: ",
            "Enter a logical value: ")

prompt_user <- function(index) {
  prompt <- prompts[index]
  readline(prompt = prompt)
}

handle_response <- function(answer, handler) {
  handler(answer)
}

create_vector <- function(...) {
  len <- as.numeric(readline(prompt = "Enter the length of the vector: "))
  vec <- vector("character", len)
  for (i in 1:len) {
    vec[i] <- as.numeric(readline(prompt = paste("Enter the value for vec[", i, "]: ")))
  }
  vec
}

l <- list()
for (i in 1:5) {
  ch <- as.integer(readline(prompt = "Enter your choice: "))
  result <- switch(ch,
                   l[i] <- handle_response(prompt_user(1), as.character),
                   l[i] <- handle_response(prompt_user(2), as.numeric),
                   l[[i]] <- handle_response(prompt_user(3), create_vector),
                   l[i] <- handle_response(prompt_user(4), as.logical),
                   print("Terminate")
  )
}

In fact, you can do away with the switch entirely with a small modification.

handlers <- c(
  as.character,
  as.numeric,
  create_vector,
  as.logical
)

create_list_element <- function(list_index, choice) {
  handle_response(prompt_user(choice), handlers[[i]])
}

for (i in 1:5) {
  ch <- as.integer(readline(prompt = "Enter your choice: "))
  if (i == 5) {
    print("Terminate")
  } else {
    l[[i]] <- create_list_element(i, ch)
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM