R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
In R, the primary function to take input from the user is readline()
. It allows users to enter information during script execution or from the console. The entered information is always read as a character string, so if you're expecting numerical input, you'll have to convert it using functions like as.numeric()
.
This tutorial will guide you on how to take input from a user in R:
Using readline()
, you can prompt the user for input:
user_input <- readline(prompt="Please enter your name: ") print(paste("Hello,", user_input, "!"))
When the script reaches the readline()
function, it will pause, display the provided prompt, and wait for the user to type in a response.
By default, readline()
reads input as a character string. To get a number, you'd convert the string to a numeric type:
age_string <- readline(prompt="Please enter your age: ") age_numeric <- as.numeric(age_string) print(paste("You are", age_numeric, "years old."))
You can use readline()
in combination with a switch
statement or conditional statements to handle different user choices:
choice <- readline(prompt="Choose an option (A, B, C): ") switch(choice, A = print("You chose option A!"), B = print("You chose option B!"), C = print("You chose option C!"), print("Invalid choice."))
To take input repeatedly, perhaps until the user gives a specific response, use a loop:
repeat { ans <- readline("Enter 'q' to quit: ") if (ans == 'q') { print("Exiting...") break } }
If you need to read a password or any sensitive data without showing the input, use the getPass
package:
install.packages("getPass") library(getPass) password <- getPass("Enter your password: ") print("Password stored safely!")
If you want to read multiple lines of input from the user, you might need a more complex setup using cat()
and readLines()
:
cat("Enter your address (finish with an empty line): \n") address <- readLines(con = stdin(), n = -1) print("Your address is:") print(address)
Always validate user input. Since users can enter anything, ensure the input is in the format and type you expect before proceeding.
If taking input for critical operations, provide a clear prompt so users know exactly what to provide.
Taking user input in R scripts can make them interactive and more adaptable to various scenarios. Whether you're looking for simple inputs, passwords, or multiple lines, R provides the tools to interact with users efficiently.
Read User Input in R:
# Example: Read user input user_input <- readline("Enter something: ")
R readline
Function:
readline
function captures user input from the console.# Example: Using readline user_input <- readline("Enter your name: ")
Getting Input from the Console in R:
readline
or scan
to get input from the console.# Example: Getting input from the console user_input <- readline("Enter a value: ")
User Prompts in R:
# Example: User prompt user_input <- readline(prompt = "Enter a number: ")
Interactive User Input in R:
# Example: Interactive user input user_name <- readline("Enter your name: ") cat("Hello, ", user_name, "!\n")
R scan
Function for User Input:
scan
allows reading multiple values from the console.# Example: Using scan for user input user_input <- scan(n = 1, what = "")
Handling User Input Errors in R:
# Example: Handling input errors user_input <- as.numeric(readline("Enter a number: "))
Reading Numbers from the User in R:
as.numeric
to convert user input to numeric values.# Example: Reading numbers from the user user_number <- as.numeric(readline("Enter a number: "))
Reading Strings from the User in R:
readline
.# Example: Reading strings from the user user_string <- readline("Enter a string: ")
R Input Validation Techniques:
# Example: Input validation user_input <- readline("Enter a positive number: ") while (!grepl("^\\d+\\.?\\d*$", user_input)) { cat("Invalid input. Please enter a positive number: ") user_input <- readline() }
Using cat
and scan
for Interactive Input in R:
cat
and scan
for a more interactive input process.# Example: Using cat and scan cat("Enter two numbers separated by a space: ") user_input <- scan(n = 2, what = "")
Console Input and Output in R:
# Example: Console input and output user_name <- readline("Enter your name: ") cat("Hello, ", user_name, "!\n")
Reading Data Interactively in R:
# Example: Reading data interactively data <- data.frame() while (TRUE) { row <- scan(n = 2, what = "") if (length(row) == 0) break data <- rbind(data, row) }
R Shiny App for User Input:
# Example: Shiny app for user input library(shiny) ui <- fluidPage( textInput("user_input", "Enter something:"), verbatimTextOutput("output") ) server <- function(input, output) { output$output <- renderText({ input$user_input }) } shinyApp(ui, server)