1

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

1 Answer 1

4
cat("input x: ")
x <- readLines(con="stdin", 1)
cat(x, "\n")
Sign up to request clarification or add additional context in comments.

1 Comment

give me some time please to implement your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.