What is the correct way to ask for user input in an R program?

前端 未结 2 771
一向
一向 2020-12-04 13:21

The my program below(which is in two parts) works if I run them separately – that is, if I paste the first part into the R Console, run it and then paste the second and ru

2条回答
  •  情话喂你
    2020-12-04 14:07

    It's because when you copy and paste all then scan reads pasted lines as input.

    If you copy this tree lines to console

    x <- scan(nmax=1)
    1
    2
    

    x become 1, scan don't wait for your interaction cause it got line to read.

    You have to wrap everything in {}:

    {
     x <- scan(nmax=1)
     1
     2
    }
    

    You have to wrap both parts of your program. To be more clear: when you paste your code to console } should be last sign.

提交回复
热议问题