Ad space (banner)
๐Ÿ“ŠR Lessons
Lesson 58 / 68

Reading a value from standard input

This lesson covers reading standard input with R's readLines, with the aim of writing programs that handle input from a user.

readLines("stdin", n = 1) reads one line of input from the keyboard. Use it when a command-line tool needs the user to type something.

The sample code prompts with cat("Enter your name: "), receives the typed string into name with readLines("stdin", n = 1), and uses it in a greeting.

A common early stumble is that standard input behaves differently depending on how the R script is run. Interactive mode and script mode can differ, so the environment matters.

In professional work, using an input value without validating it leads to unexpected errors, so pairing it with a check is the safe approach.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run R directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)