Skip to content
Back to formatted view

Raw Message

Message-ID: <D1A40F68-8E0A-4257-BCDD-554E92F5D4E7@comcast.net>
Date: 2016-12-20T17:55:51Z
From: David Winsemius
Subject: Getting scan() -command to work during the program run
In-Reply-To: <5858EDE4.6040409@utu.fi>

> On Dec 20, 2016, at 12:37 AM, Atte Tenkanen <attenka at utu.fi> wrote:
> 
> 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)

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.