cmd - user input in R (Rscript and Widows command prompt) -
i trying figure out, how can run r script, using rscript in windows command prompt , ask user input.
so far, have found answers on how ask user input in r's interactive shell. effort in doing same readline() or scan() has failed.
example:
i have polynomial y=cx x can take more 1 values x1,x2,x3 , on. c variable know, need in order calculate value of y ask user xi values , store them somewhere inside script.
uinput <- function() { message(prompt"enter x1 value here: ") x <- readlines() } is way go? additional arguments? as.numeric help? how return x1? , implementation differ depending on os?
thanks.
that's general way go, implementation needs work: don't want readlines, want readline (yes, name similar. yes, dumb. r filled silly things ;).
what want like:
uiinput <- function(){ #ask user input x <- readline(prompt = "enter x1 value: ") #return return(x) } you want error-handling there, though (i provide x1 value of false, or "turnip"), , type conversion, since readline returns one-entry character vector: numeric input provided should converted to, well, numeric input. nice, user-proof way of doing might be...
uiinput <- function(){ #ask user input x <- readline(prompt = "enter x1 value: ") #can converted? x <- as.numeric(x) #if can't, have problem if(is.na(x)){ stop("the x1 value provided not valid. please provide number.") } #if can be, return - turn if if/else make more #readable, wouldn't make difference in functionality since stop() #means if if-condition met, return(x) never #evaluated. return(x) }
Comments
Post a Comment