Reading and writing files

Read project files with read.csv and readLines, and write results under /tmp with write.csv and writeLines.

Runs in Zerethon IDE · R 4.5 Official R documentation ↗

A run sees the project's files in its working folder, read-only. A script may write files under /tmp; they are deleted when the run ends.

main.R
people <- read.csv("data.csv")
print(people$name)
print(mean(people$score))
people$passed <- people$score >= 88
write.csv(people, "/tmp/report.csv", row.names = FALSE)
writeLines(readLines("/tmp/report.csv"))
data.csv
name,score
ada,90
alan,85

Output

[1] "ada"  "alan"
[1] 87.5
"name","score","passed"
"ada",90,TRUE
"alan",85,FALSE
  • read.csv() returns a data frame whose column names come from the first line.
  • Paths are relative to the project root (the working folder): read.csv("data/scores.csv") reads the file shown in the explorer as data/scores.csv.
  • Writing next to your code fails with cannot open the connection (the folder is read-only) — use /tmp/out.csv.
  • Pass row.names = FALSE to write.csv() unless you want the row numbers as a first column.