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.
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.
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.
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 variable's name, age by prompting appropriate messages to the user
name = readline(prompt="Input your name: ") age = readline(prompt="Input your age: ") print(paste("My name is",name, "and I am",age ,"years old.")) print(R.version.string)
Input your name: Jhon Input your age: 23  "My name is Jhon and I am 23 years old."  "R version 4.1.2 (2021-11-01)"