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

The basics of reading and writing files

This lesson covers reading and writing files in R, with the aim of saving and loading text from a program.

writeLines() writes to a file and readLines() reads one. Saving configuration and logs makes this a basic everyday operation.

The sample code saves the contents of a memo with writeLines(c("Hello", "This is line 2"), "memo.txt") and reads the same file back with readLines("memo.txt"). Note that passing a vector of strings writes several lines at once.

A common early stumble is error handling for a missing file or missing write permission. Professional code is expected to anticipate those with tryCatch.

In professional work, file handling is an essential basic capability — writing out analysis results, reading configuration.

๐Ÿ“– 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)