Question

I am trying to run an R script, using Rscript in Windows command prompt and ask for user's input.

So far, i have found answers on how to implement something similar in R's interactive shell. readline() or scan() doesn't seem to work for the command prompt.

Examples:

I have a polynomial y=cX where X can take more than one values X1,X2,X3 and so on. C variable is know, so what i need in order to calculate the value of y is to ask the user for the Xi values and store them somewhere inside my script.

The script bellow does nothing.

UIinput <- function(){

    #Ask for user input
    x <- readline(prompt = "Enter X1 value: ")

    #Return
    return(x)
}

The second example below just prompts the message and then ends.

FUN <- function(x) {

    if (missing(x)) {
        message("Uhh you forgot to eneter x...\nPlease enter it now.")
        x <- readLines(n = 1)
    }
    x
}
FUN()

Console output:

Uhh you forgot to eneter x...
Please enter it now.
character(0)

Any suggestions?

Thanks in advance

Was it helpful?

Solution

cat("input x: ")
x <- readLines(con="stdin", 1)
cat(x, "\n")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top