R Program to take input from the user and display the values

How to take input from the user and display it

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). Here the prompt argument can choose to display an appropriate message for the user. In this values are displayed using print with argument R.Version.string which returns a list with character-string components.

How user input reading is implemented in R Program

We are using readline() function for taking the user’s input. Given below are the steps which are used in the R program to read and display values. In this R program, we accept the user’s values into name and age by providing an appropriate message to the user using ‘prompt’

ALGORITHM

  • STEP 1: prompting appropriate messages to the user
  • STEP 2: take user input using readline() into variables name, age
  • STEP 3: print the user input along with other text.
  • STEP 4: use R.Version.string for returning list with character-string components

Code

name = readline(prompt = "Input your name: ")
## Input your name:
age =  readline(prompt = "Input your age: ")
## Input your age:
print(paste("My name is", name, "and I am", age ,"years old."))
## [1] "My name is  and I am  years old."
print(R.version.string)
## [1] "R version 4.1.2 (2021-11-01)"