d. sarthi maheshwari
2007-Mar-01 10:53 UTC
[R] R (Input from Keyboard): How do I remove error?
Hi, I am trying to read some value from keyboard in one of my R program. I want to do manipulation on data based on this value. But I am facing a problem which is described below with the help of an example: My code is similar to: : : cat("\n","Enter value:: ","\n") y<-scan(n=1) a <- b*y cat("\n","Enter new value::","\n") y<-scan(n=1) : : Now if I run the whole script then something like this happens: : : cat("\n","Enter value::","\n") Enter value::> y<-scan(n=1)1: a <- b*y Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : scan() expected 'a real', got 'a'> cat("\n","Enter new value::","\n")Enter new value::> y<-scan(n=1)1: 2 Read 1 item : : Is there any solution to this problem or shall I execute my script in two parts: First part: : : cat("\n","Enter value:: ","\n") y<-scan(n=1) and second part: a <- b*y cat("\n","Enter new value::","\n") y<-scan(n=1) : : Any help will be greatly appreciated. -- Thanks & Regards Sarthi M. [[alternative HTML version deleted]]
Petr Klasterecky
2007-Mar-01 11:34 UTC
[R] R (Input from Keyboard): How do I remove error?
?readline Note that this gives you a character vector, so you might want to use as.numeric() after that. Petr d. sarthi maheshwari napsal(a):> Hi, > > I am trying to read some value from keyboard in one of my R program. I want > to do manipulation on data based on this value. But I am facing a problem > which is described below with the help of an example: > > My code is similar to: > > : > : > cat("\n","Enter value:: ","\n") > y<-scan(n=1) > a <- b*y > cat("\n","Enter new value::","\n") > y<-scan(n=1) > : > : > > Now if I run the whole script then something like this happens: > > : > : > cat("\n","Enter value::","\n") > > Enter value:: >> y<-scan(n=1) > 1: a <- b*y > Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, > : > scan() expected 'a real', got 'a' >> cat("\n","Enter new value::","\n") > > Enter new value:: >> y<-scan(n=1) > 1: 2 > Read 1 item > : > : > > Is there any solution to this problem or shall I execute my script in two > parts: > First part: > > : > : > cat("\n","Enter value:: ","\n") > y<-scan(n=1) > > and second part: > > a <- b*y > cat("\n","Enter new value::","\n") > y<-scan(n=1) > : > : > > Any help will be greatly appreciated.-- Petr Klasterecky Dept. of Probability and Statistics Charles University in Prague Czech Republic