0

I am experimenting with reading input from a user in R. So I am running a simple piece of code:

input <- readLines("stdin", n = 2)

I am expecting to give two lines of input to the console and then I would assume that the connection will close. I am trying to figure out how to give the correct input.

I have tried including \n in my input but is not helping. I know that this question has been asked before here : R readLines from console- how to signal end of input but I did not find the answer to be useful. I get stack with a non-ending connection which I cannot stop by using CTRL-Z , CTRL-D or any other control combination for that matter.

Could someone please explain how to correctly give input to the function, specify EOL, EOF and perhaps the expected format?

Community
  • 1
  • 1

1 Answers1

1

Try this input <- readLines(con=stdin(), n=2). You can terminate each of your input with return.

Imran Ali
  • 2,223
  • 2
  • 28
  • 41
  • Thank you very much, this does work as you mentioned. Could you please explain if you know what is the difference between "stdin" and your solution, what is the EOL character , EOF character, or anything you can to assist me with reading input from "stdin"? I am having a tough time understanding it unfortunately. Thank you! –  Sep 22 '16 at 22:39
  • EOL marker (usually \n) marks the End of Line and EOF marks the end of a file. Functions for reading input from keyboard and from a file on a disk usually provide mechanism to test each of them. stdin() is a function that provides necessary mechanism to read input from keyboard. – Imran Ali Sep 22 '16 at 22:53
  • The solution i provided is the right way of using stdin and you can lookup additional information in the [documentation](https://stat.ethz.ch/R-manual/R-devel/library/base/html/readLines.html) – Imran Ali Sep 22 '16 at 22:54