1

I want to use readLines function for input from console of variable number of lines and store it to a vector:

v <- readLines()

How do I signal the end of input? Control-c cancels the process and no 'v' object is formed. Control-Z stops R program altogether. Typing 'EOL' or 'EOF' do not work.

I tried following function but it gives error:

getinput = function(){
    v=""
    while(TRUE){
        line = readLines(n=1)
        if(line=="") break
        v = v+line
    }
    v
}

> getinput()
firstentry
Error in v + line : non-numeric argument to binary operator
> 

I am using R on Debian Linux. Thanks for your help.

rnso
  • 23,686
  • 25
  • 112
  • 234
  • http://stackoverflow.com/questions/16493669/how-to-read-input-from-the-terminal-using-dev-stdin-and-read-csv may help (that being said, CRTL D twice doesn't work for me through RStudio / RGui on windows – mnel Aug 06 '14 at 02:13

1 Answers1

1

<CTRL-D> will signal EOF. If you're using ess, try C-c C-c. Hope that helps and good luck. Leave a comment if you need further assistance.

hd1
  • 33,938
  • 5
  • 80
  • 91
  • 1
    In Linux running native R, control-D works, but while running windows R package in Linux with wine, control-D does not work. Instead, control-Z works there. – rnso Aug 06 '14 at 03:46
  • In your question, you said "I am using R on Debian Linux", but thanks for the information. – hd1 Aug 06 '14 at 03:52
  • I am using both native R (from Debian stable repository) and Windows R under wine in same Linux. This is because latest packages are not available in native R in Debian Stable. Your answer was very helpful. Thanks. – rnso Aug 06 '14 at 03:57