[R] using scan to record user's input

Patrick Callier pcallier at stanford.edu
Sun Mar 11 18:49:36 CET 2007


The apple-enter thing works like copy and paste would, as far as I can
tell (I get a similar bug).  The deal is that it's entering each line
in one at a time, so the for-loop starts running after the last brace
and gets as its input the next line sent from the console.

Either one of the solutions you propose (using source or putting it in
a function) should fix your problem.

Best,
Pat

On 3/11/07, Ken Knoblauch <knoblauch at lyon.inserm.fr> wrote:
> I get it on a powerpc Mac, too,
>
> > y <- rep(NA, 3)
> > for (ix in seq( length(y) ) ) {
> +          y[ix] <- scan( n = 1 )
> +         }
> 1: 2
> Read 1 item
> 1: 3
> Read 1 item
> 1: 4
> Read 1 item
> > y
> [1] 2 3 4
> > y <- rep(NA, 3)
> > for (ix in seq( length(y) ) ) {
> +          y[ix] <- scan( n = 1 )
> +         }
> 1: y
> 1: 5
> Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
>         scan() expected 'a real', got 'y'
> +
>
> R version 2.4.1 Patched (2007-01-05 r40386)
> powerpc-apple-darwin8.8.0
>
> locale:
> en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>
> attached base packages:
> [1] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"
> [7] "base"
>
> Thanks.
>
> ken
>
> Patrick Callier wrote:
> > Silly question: Are you 'source'-ing this code or copying and pasting
> > it from somewhere?  I don't have any problems running your above
> > example on a PPC Mac with almost the same version of R (using
> > 'source').
> >
> > Here's my info:
> > R version 2.4.1 (2006-12-18)
> > powerpc-apple-darwin8.8.0
> >
> > locale:
> > en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
> >
> > attached base packages:
> > [1] "stats"     "graphics"  "grDevices" "utils"     "datasets"
> > "methods"   "base"
> >
> > other attached packages:
> >       MASS KernSmooth
> >   "7.2-30"  "2.22-19"
> >
> > Otherwise, I'm as stumped as you are.
> >
> > On 3/11/07, Ken Knoblauch <knoblauch at lyon.inserm.fr> wrote:
> >> I'm using scan in a script to record a series of responses of the user
> >> as
> >> a function
> >> of some graphs that I put up on the screen.  A toy version would be,
> >>
> >> y <- rep(NA, 3)
> >> for (ix in seq( length(y) ) ) {
> >>         y[ix] <- scan( n = 1 )
> >>         }
> >>
> >> However, if I include any code after this loop, for example,
> >> y <- rep(NA, 3)
> >> for (ix in seq( length(y) ) ) {
> >>         y[ix] <- scan( n = 1 )
> >>         }
> >> y
> >>
> >> I get an error after I've entered my first number.
> >>
> >> 1: y
> >> 1: 3
> >> Error in scan(file, what, nmax, sep, dec, quote, skip, nlines,
> >> na.strings,  :
> >>         scan() expected 'a real', got 'y'
> >>
> >> scan seems to be trying to read the commands after the loop.  In the
> >> real
> >> case,
> >> the loop is longer and there are more commands after the loop.
> >> If I have more commands, I get the error right away, before the prompt
> >> to
> >> enter a number.
> >>
> >> I didn't see anything in the scan help that would allow me to circumvent
> >> this error.
> >>
> >> Can anyone suggest a way to get around this error.
> >>
> >> Thank you.
> >>
> >> Ken
> >>
> >> Here is my info
> >>
> >> sessionInfo()
> >> R version 2.4.1 Patched (2007-01-23 r40561)
> >> i386-apple-darwin8.8.1
> >>
> >> locale:
> >> C
> >>
> >> attached base packages:
> >> [1] "stats"     "graphics"  "grDevices" "utils"     "datasets"
> >> "methods"
> >> [7] "base"
> >>
> >> other attached packages:
> >>     MASS
> >> "7.2-31"
> >>
> >>
> >> --
> >> Ken Knoblauch
> >> Inserm U846
> >> Institut Cellule Souche et Cerveau
> >> Département Neurosciences Intégratives
> >> 18 avenue du Doyen Lépine
> >> 69500 Bron
> >> France
> >> tel: +33 (0)4 72 91 34 77
> >> fax: +33 (0)4 72 91 34 61
> >> portable: +33 (0)6 84 10 64 10
> >> http://www.pizzerialesgemeaux.com/u846/
> >>
> >> ______________________________________________
> >> R-help at stat.math.ethz.ch 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.
> >>
> >
>
>
> --
> Ken Knoblauch
> Inserm U846
> Institut Cellule Souche et Cerveau
> Département Neurosciences Intégratives
> 18 avenue du Doyen Lépine
> 69500 Bron
> France
> tel: +33 (0)4 72 91 34 77
> fax: +33 (0)4 72 91 34 61
> portable: +33 (0)6 84 10 64 10
> http://www.pizzerialesgemeaux.com/u846/
>
>



More information about the R-help mailing list