readline()
readline(prompt = NULL) Returns:
character · Updated March 13, 2026 · Base Functions interactive input base console
readline() reads a line of text from the console (interactive session) or from a connection. It pauses execution until the user enters text and presses Enter. This function is essential for creating interactive R scripts and user prompts.
Syntax
readline(prompt = NULL)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
prompt | character | NULL | A character string to display as the prompt before reading input |
Examples
Basic usage
# Interactive prompt for user name
name <- readline("What is your name? ")
# User types: Alice
# Output: "Alice"
# Using the input
paste("Hello,", name)
# Output: "Hello, Alice"
Getting numeric input
# Reading and converting to numeric
age <- readline("Enter your age: ")
age <- as.numeric(age)
# User types: 25
# Output: 25
Multiple prompts in a script
# Creating an interactive data entry workflow
cat("=== New Entry ===\n")
name <- readline("Name: ")
email <- readline("Email: ")
department <- readline("Department: ")
data <- data.frame(
name = name,
email = email,
department = department
)
print(data)
Common Patterns
Input validation: Combine readline() with while loops to validate user input.
get_valid_choice <- function() {
choice <- readline("Enter 1, 2, or 3: ")
while (!choice %in% c("1", "2", "3")) {
choice <- readline("Invalid. Enter 1, 2, or 3: ")
}
return(choice)
}
Interactive yes/no prompts:
confirm <- readline("Continue? (y/n): ")
if (tolower(substr(confirm, 1, 1)) == "y") {
print("Proceeding...")
} else {
print("Cancelled.")
}