R Program to take user input and display the values and print version of R

Hello beginner ! as we have tried our testing program Hello World!  next, let's see how input can be taken from the user(like name, and age) and print those values. It is very essential in the programming language to read data from the user. We also help you to print the version of R currently running in your system. Before going to the program let's rewind the basic topics of R.

How to take input from the user and display it?

As in other languages, R also provides functions to read data from the user using the terminal. We will display some messages to the user to input data, the message should be related to the data we need and he will enter data from the keyboard. The user input will be read to variables and we can convert them to required datatypes too. In R data is read as strings. And we use those data in our programs for different operations and calculations.

How do we implement user input reading using R Program?

Here we are explaining how to write an R program to take input from the user and display that values. we can use the readline() function to take input from the user (terminal).

name = readline(prompt="Input your name: ")

Here the variable name will hold the data read from the user through the readline() function and the argument prompt will provide the request message to the user that will show in the terminal.    

In this R program, we accept the user's values into name and age by providing an appropriate message to the user using 'prompt' and print the string after concatenating it with the appropriate text.

How to print the version of the R installation?


Here the built-in argument R.version.string will tell us which version of RStudio is running on our computer. It is given inside the print() function to display the currently running version of R. 


STEP 1: Take user input using readline() into variables name, age by prompting appropriate messages to the user

STEP 2: print the user input along with other text with the help of paste()

STEP 3: print the current version of R using R.version.string

R Source Code

                                          name = readline(prompt="Input your name: ")
age =  readline(prompt="Input your age: ")
print(paste("My name is",name, "and I am",age ,"years old."))


Input your name: Jhon
Input your age: 23
[1] "My name is Jhon and I am 23 years old."
[1] "R version 4.1.2 (2021-11-01)"