1

I have some R command like this

subset(
  (aggregate(cbind(var1,var2)~Ei+Mi+hours,a, FUN=mean)),
  (aggregate(cbind(var1,var2)~Ei+Mi+hours,a, FUN=mean))$Ei == c(1:EXP)
)

I want to do

1) Ask the user to input the var1 and var2

2) Get those variables into the subset command line as shown above and continue with other things.

Note: for reading the user input I have variables like c(ax,bx,cx,dx,ex,fx,gx,hx,ix,jx,kx,lx,mx,nx,ox) = c(1:15) and each variable is mapped to number 1 to 15. So displaying this for user and asking the user to select any number between 1 to 15 and then checking the corresponding variable for the entered number and reading this into the command line is whats the best method, I think.

So how can I implement this?

Regarding the answer: Just wondering there is one possible scenario like , if the user wants to enter multiple of numbers in one go. [ex: 1,2,3]...than how to read this using readlines as said in the answer below using

v1 <- quote(var1 <- as.numeric(readline('Enter Variable 1: ')))
eavl(v1)
xx <- paste0(letters[1:15], 'x')
xx[v1]

How to read multiple variables in this case?

cppiscute
  • 707
  • 2
  • 10
  • 33
  • Why not to ask the user to fill a config file(csv or others) where you set all the parameters? – agstudy May 21 '14 at 23:15
  • Oh that is a easy methode instead of asking the user for options and then dealing with it. Actually I have 5 different options telling the user to select any one so that I can jump to the particular function and carry out the operation (all these 5 types are just different kind of plots, requiring different kind of user inputs), so basically I will be storing the values of csv inside a data frame and reading them accordingly and carrying out operations thats what you mean is it? – cppiscute May 22 '14 at 00:00
  • yes for example. You should also use function with default values parameters. – agstudy May 22 '14 at 01:26

2 Answers2

0

Here's a rough example of the readline interactive prompt. When v1 is evaluated, the user will be prompted to enter a value. That value is then stored as var1.

> v1 <- quote(var1 <- as.numeric(readline('Enter Variable 1: ')))
> eval(v1)
Enter Variable 1: 1000     ## user enters 1000, for example
> 100 + var1 + 50          ## example to show captured output as object
## [1] 1150

So in your case it might go something like

> v1 <- quote(var1 <- as.numeric(readline('Enter a number from 1 to 15: ')))
> eval(v1)
Enter a number from 1 to 15: 7
> var1
## [1] 7
> xx <- paste0(letters[1:15], 'x')
> xx
## [1] "ax" "bx" "cx" "dx" "ex" "fx" "gx" "hx" "ix" "jx" "kx" "lx" "mx" "nx" "ox"
> xx[var1]
## [1] "gx"

I borrowed this idea for a function from this older SO post. You can return the output invisibly and it will still take in the user values.

input.fun <- function(){
     v1 <- readline("var1: ")  
     v2 <- readline("var2: ")
     v3 <- readline("var3: ")
     v4 <- readline("var4: ")
     v5 <- readline("var5: ")
     out <- sapply(c(v1, v2, v3, v4, v5), as.numeric, USE.NAMES = FALSE)
     invisible(out)    
}

> x <- input.fun()
var1: 7
var2: 4
var3: 8
var4: 5
var5: 2
> x
[1] 7 4 8 5 2

In response to your edit: I'm not sure if this is the standard method for reading multiple numbers in one line, but it works.

> xx <- readline('Enter numbers separated by a space: ')
Enter numbers separated by a space: 4 12 67 9 2
> as.numeric(strsplit(xx, ' ')[[1]])
## [1]  4 12 67  9  2
Community
  • 1
  • 1
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
0

Here's a possibility using scan()

#sample data
df<-data.frame(
    ax=runif(50),
    bx=runif(50),
    cx=runif(50),
    dx=runif(50),
    Ei=sample(letters[1:5], 50, replace=T)
)

#get vars
vars<-c(NA,NA)
while(any(is.na(vars))) {
    cat(paste("enter var number", sum(!is.na(vars))+1),"\n")
    cat(paste(seq_along(names(df)), ":", names(df)), sep="\n")
    try(n<-scan(what=integer(), nmax=1), silent=T)
    vars[min(which(is.na(vars)))]<-n
}
#--pause

#use vars
subset(aggregate(df[,vars], df[,c("Ei"), drop=F], FUN=mean), Ei=="a")

It's not super robust, but if you copy the first half (before the pause) it will ask you for two variable numbers, and then if you run the second half, it will use those two values. I've adjusted the aggregate and subset to be more appropriate for variable usage which means not using the formula syntax.

I did not do any error checking. That's left as an exercise for the asker.

MrFlick
  • 195,160
  • 17
  • 277
  • 295