Skip to content

Getting scan() -command to work during the program run

5 messages · Bert Gunter, Atte Tenkanen, David Winsemius

#
Hi,

How to get scan(file="") command to ask my input from the keyboard?

If I put the command straight to the console it works

 > X1 <- scan(n=1)
1: 22
Read 1 item

but as a part of a program it just continues without asking my value?

 > X1 <- scan(n=1)
1:
Read 0 items

Yours,

Atte Tenkanen
#
??
1: 3
Read 1 item



-- Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Tue, Dec 20, 2016 at 12:37 AM, Atte Tenkanen <attenka at utu.fi> wrote:
#
Hi Bert,

I tried using the function, but still the same problem. If there is 
something after the scan-line, scan does not let me to enter any input 
but the program just run forward like before.

I have OSX 10.9.5. and R 3.3.2.

Atte

20.12.2016, 17.19, Bert Gunter kirjoitti:

  
    
#
No clue.

-- Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Tue, Dec 20, 2016 at 8:11 AM, Atte Tenkanen <attenka at utu.fi> wrote:
#
As "part of a program" there is no console input file when run in non-interactive mode. Efforts to make a non-interactive session into an interactive session are well documented in the rhelp archives. Search:

http://markmail.org/search/?q=list%3Aorg.r-project.r-help+user+input+interactive+rscript

If you did that inside a function which you ran inside an interactive session and did not return the value there would of course be no value, but that would not give you response you report. You would be expected to use the 'file'-parameter for specifying the source of input when using scan in a script.

If my only code is a single line in a text file named 'Untitled.R':

 X1 <- scan(n=1)

Running just that with Rscript at the system console does give me:

Read 0 items

Now make a data file named 'myfile.txt' and a source file named "Untitled.R" and use it to create a data file named myfile.txt and then read from it.

------
 cat("5", "\n", file="myfile.txt")
 X1 <- scan(file="myfile.txt")
 X1
-------

The source Untitled.R or run with Rscript.