How to take user input in R?

Filed Under: R Programming

Taking a user input is very simple in R using readline() function. In this tutorial, we will see how we can prompt the user in R.

Interacting with the users for the input is going to be the best part whenever you code. Prompting the user for the input and reacting to that input is way exciting than anything. In this tutorial, we are going to take any user input’s and let’s do some meaningful stuff with it.

Let’s roll!!!

Let’s start with the syntax

readline(): The readline() function will read the lines which is given in the terminal. The syntax is given as,

readline(prompt=" ")

Where: “prompt = ” is a string that is requested by the user as input and it should be enclosed by double-quotes.

Let’s take a simple prompt from the user

Now, let’s take simple user input from the user, and then we are going to print the result as well.

#prompts the user 
x<-readline(prompt = "Enter the organization name: ")

Output = Enter the organization name: Journaldev

Let’s see another sample of prompting the age of the user.

#prompts the user age
x<-readline(prompt = "Enter your age: ")

Output = Enter your age: 22

Multiple user-prompts in R

In R you can take multiple users prompts using readline() function. The feature of multiple user inputs is highly useful as a complete sequence of the questionnaire can be created using this.

After taking the user inputs, we are going to concatenate them in a meaningful way. This will be illustrated below.

Let’s see how this works.

a<-readline(prompt = "Enter your first name: ")

Enter your first name: Prajwal

 b<-readline(prompt = "Enter your last name: ")

Enter your last name: C N

paste("Hello,",a,b,'Welcome to JD')

“Hello, Prajwal C N Welcome to JD”

As shown in the above examples, you can create user prompts and print them in a meaningful way.

Multiple prompts in a single line

In R we have discussed the multiple prompts in the above section. But do you know that you can have multiple prompts for users with a single line of code? Sounds interesting right? Let’s see how it works.

{name <- readline(prompt="Enter your name: ");
+ age <- readline(prompt="Enter your age: ");paste("Howdy,",name,age)}

This prompt will ask the user about their name, age, and then concatenates them with the welcome message as shown in the below output.

Enter your name:   Prajwal
Enter your age:   CN
"Howdy,  Prajwal CN"

Calculating daily wages with user input

This section will be interesting as we are going to prompt the user for the input data and based on that data we are going to calculate the daily wages of the labor.

Let’s see how it works.

#here we are dealing with US dollars
x<-as.numeric(readline(prompt = "Enter number of hours: "))
y<-as.numeric(readline(prompt = "Enter per hour rate: "))
paste("Total wages per day is:$",x*y)

Output =

Enter number of hours:   8
Enter per hour rate:   12.50
Output = "Total wages per day is:    $ 100"

In this modern era, people want faster actions. We want everything to be done in a quick fashion. So I will try to save some time for you by illustrating this example in a single line of code.

Let’s see how it works.

x<-as.numeric(readline(prompt = "Enter the number of hours per day: "));
y=as.numeric(readline(prompt = "Enter per hour rate: "));
paste("Total wages per day is:$",x*y)

Output =

Enter the number of hours per day: 8
Enter per hour rate: 12.50
"Total wages per day is:  $ 100"

As shown above, you can prompt the user for input data and you can compute some meaningful insights such as finding simple interest, calculating daily wages, and many more in merely a single line of code.

Simple login setup using readline()

In this section, we are going to create a simple login set up to prompt the user to give the input or enter the details to log in. After that, we are displaying a welcome message to the user.

Let’s see how it works.

x<-readline(prompt = "Enter your username: ")
y<-readline(prompt = "Enter your password: ")
paste("Welcome to Journaldev, Mr.",x)

Output =

Enter your username:   Prajwal
Enter your password:   Prajwal@jd
"Welcome to Journaldev,   Mr. Prajwal"

Wrapping up

As we have discussed in the above sections, take user input or prompting a user for the input is pretty much easy in R. The readline() function helps to take the user input in R as shown above.

We, humans, love the interaction. We love talking to each other. So for all of us, R has made these things simple with functions like readline(). Thanks to R, its always going to be fun when you interact with the user through your code.

That’s all about taking user input in R. Happy learning!!!

More study: R documentation


  1. Gogol Prasad says:

    Very informative and easy to understand content. It helped me understand the concepts. I found your content to be well structured and to the point.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content