[R] Supplying function inputs interactively
Gabor Grothendieck
ggrothendieck at gmail.com
Sun Sep 12 01:34:56 CEST 2010
On Sat, Sep 11, 2010 at 7:17 PM, Christofer Bogaso
<bogaso.christofer at gmail.com> wrote:
> Dear all, I am looking for some procedure to send inputs to a function
> interactively. Here is an example:
>
> fn1 <- function(x = 10) {
> y <- 0
> # ask user whether he wants to put some other value for "y"
> # R will show 2 options: 1. y = 2
> # 2. y = 3
> # user will choose either options 1 or 2
> # once user is done, function will over-write the existing value of y,
> with say, option 2. Therefore current value of y is 2. Then R will
> continue calculation
> return(x*y)
> }
>
Try this:
ysquared <- function(y) {
if (missing(y)) y <- as.numeric(readline("Enter y: "))
y*y
}
ysquared()
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list