Introduction
In R, you can take input from the user using the readline()
function. This guide will demonstrate how to write an R program that takes different types of input from the user, processes it, and displays the result.
Problem Statement
Create an R program that:
- Prompts the user to enter their first name, last name, email, age, and gender.
- Takes the input from the user for each field.
- Displays the entered data.
Example:
- Input:
Enter your first name: Ramesh
Enter your last name: Fadatare
Enter your email: ramesh.fadatare@example.com
Enter your age: 35
Enter your gender: Male
- Output:
Name: Ramesh Fadatare
Email: ramesh.fadatare@example.com
Age: 35
Gender: Male
Solution Steps
- Prompt the User for Input: Use the
readline()
function to prompt the user to enter their first name, last name, email, age, and gender. - Capture the User Input: Store each piece of input in a separate variable.
- Display the Input: Use the
print()
function to display the captured data back to the user.
R Program
# R Program to Take Input from User # Author: https://www.javaguides.net/ # Step 1: Prompt the user to enter their first name first_name <- readline(prompt = "Enter your first name: ") # Step 2: Prompt the user to enter their last name last_name <- readline(prompt = "Enter your last name: ") # Step 3: Prompt the user to enter their email email <- readline(prompt = "Enter your email: ") # Step 4: Prompt the user to enter their age age <- as.numeric(readline(prompt = "Enter your age: ")) # Step 5: Prompt the user to enter their gender gender <- readline(prompt = "Enter your gender: ") # Step 6: Display the captured information print(paste("Name:", first_name, last_name)) print(paste("Email:", email)) print(paste("Age:", age)) print(paste("Gender:", gender))
Explanation
Step 1: Prompt the User to Enter Their First Name
- The
readline()
function is used to prompt the user for their first name. The input is captured as a character string in thefirst_name
variable.
Step 2: Prompt the User to Enter Their Last Name
- The
readline()
function is used again to prompt the user for their last name, which is stored in thelast_name
variable.
Step 3: Prompt the User to Enter Their Email
- The
readline()
function captures the email input and stores it in theemail
variable.
Step 4: Prompt the User to Enter Their Age
- The
readline()
function prompts the user for their age, which is then converted to a numeric value usingas.numeric()
and stored in theage
variable.
Step 5: Prompt the User to Enter Their Gender
- The
readline()
function captures the gender input and stores it in thegender
variable.
Step 6: Display the Captured Information
- The
print()
function is used to display the user’s full name, email, age, and gender.
Output Example
Example:
Enter your first name: Ramesh Enter your last name: Fadatare Enter your email: ramesh.fadatare@example.com Enter your age: 35 Enter your gender: Male [1] "Name: Ramesh Fadatare" [1] "Email: ramesh.fadatare@example.com" [1] "Age: 35" [1] "Gender: Male"
Conclusion
This R program demonstrates how to take multiple inputs from the user, including their first name, last name, email, age, and gender, and then display that information. This example is useful for beginners learning how to work with different types of user input in R.