Menu
Coddy logo textTech

Reading Input with readline()

Part of the Fundamentals section of Coddy's R journey — lesson 29 of 78.

So far, you've learned how to display output. Now it's time to make your programs interactive by reading input from the user. The readline() function lets you capture what the user types.

name <- readline()
cat("Hello,", name, "\n")

When this code runs, R waits for the user to type something and press Enter. Whatever they type gets stored in the name variable. If the user types "Alice", the output would be: Hello, Alice

One important thing to know: readline() always returns a character string, even if the user types a number. If someone enters "25", it's stored as the text "25", not the number 25. You'll learn how to convert between types in the next lesson.

You can read multiple inputs by using readline() multiple times:

first <- readline()
second <- readline()
cat("First:", first, "\n")
cat("Second:", second, "\n")

Each readline() call waits for a separate line of input from the user.

When you need to join strings together without any spaces between them, use paste0(). Unlike cat(), which adds a space between each item, paste0() concatenates values directly:

city <- readline()
cat(paste0("Welcome to ", city, "!"), "\n")

Here, paste0() builds the string Welcome to Paris! with no unwanted spaces before the !, and then cat() prints it.

challenge icon

Challenge

Easy

Read two lines of input and display a personalized greeting message.

You will receive two inputs:

  1. A first name
  2. A city name

Read both inputs using readline() and display a message in the following format:

Welcome, [name] from [city]!

Use cat() to produce the output. Make sure to include the exclamation mark at the end and a newline character \n.

For example, if the inputs are Emma and Paris, the output should be:

Welcome, Emma from Paris!

Cheat sheet

To read user input, use the readline() function:

name <- readline()
cat("Hello,", name, "\n")

The readline() function waits for the user to type something and press Enter. The input is stored as a character string.

To read multiple inputs, use readline() multiple times:

first <- readline()
second <- readline()
cat("First:", first, "\n")
cat("Second:", second, "\n")

Note: readline() always returns a character string, even if the user types a number.

Use cat() to print values separated by spaces. Use paste0() to join values together with no spaces between them:

name <- readline()
cat(paste0("Hello, ", name, "!"))

In this example, paste0() combines "Hello, ", the value stored in name, and "!" into a single string with no extra spaces.

Try it yourself

# Read input
con <- file("stdin", "r")
name <- suppressWarnings(readLines(con, n = 1))
city <- suppressWarnings(readLines(con, n = 1))

# TODO: Write your code below to display the greeting message
# Use cat() to output: Welcome, [name] from [city]!
# Don't forget the newline character \n at the end
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals