[R] Reading Data

Hanke, Alex HankeA at mar.dfo-mpo.gc.ca
Fri Mar 19 19:31:14 CET 2004


The following response by B.Ripley to a similar request may help.
Alex

On Tue, 21 Oct 2003, Ernie Adorio wrote:

> 
> Am using R on a Linux box and am currently writing an interactive R
script.
> 
> 1. How do I ask a user to press any key to continue ? I used a system call
to 
> read but this only works if the Enter key is pressed:
>   print("Press any key to continue")
>   system("read")

You seem over-fond of the system() command!  Try

cat("Press enter key to continue\n")
foo <- readLines(stdin(), 1)

In general R does not have a character-by-character interface with a 
keyboard.

> 2. How do I get a string input from the user? Would like to see an R
function,
> say askget():
> 
>   delay  =  askget("Enter delay in seconds")
>   system(paste( "sleep ", delay))

cat("Enter delay in seconds:\n")
Delay <- scan("", numeric(1))
Sys.sleep(Delay)

will do it (there are other more elegant ways such as

testit <- function()
{
  cat("Enter delay in seconds: ")
  Delay <- as.numeric(readLines(stdin(), 1))
  if(is.na(Delay)) stop("non-numeric input")
  Sys.sleep(Delay)
}

)


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help

-----Original Message-----
From: Kissell, Robert [EQRE] [mailto:robert.kissell at citigroup.com] 
Sent: March 19, 2004 12:47 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Reading Data


Hi,

Quick question on reading data.

I am running simulations but want to allow the user the option to define the
number of simulations. How can I have R read-in user data entered from the
keyboard? Is there a difference for reading in numeric and character data?

For example, I am trying to write the following in R:

Enter Number of Iterations?
<<<the user then enters a number say Y >>>

X <- Y       # i then want to read that number into X


Thanks.

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list