R scripts often exchange data through CSV files. A script can write a table, read it back, and clean up.

Program

Play the script to watch a temporary CSV become data and then a total.

csv_io.R
Replay: real traced execution (multi-file project)
path <- tempfile(fileext = ".csv")
write.csv(data.frame(x = c(1, 2)), path, row.names = FALSE)
data <- read.csv(path)
total <- sum(data$x)
unlink(path)
cat(total, "\n", sep = "")
  1. path ← /tmp/file.csv

    1path <- tempfile(fileext = ".csv")2write.csv(data.frame(x = c(1, 2)), path, row.names = FALSE)
    values this step/tmp/file.csvpath
  2. file ← 2 rows x 1 col

    1path <- tempfile(fileext = ".csv")2write.csv(data.frame(x = c(1, 2)), path, row.names = FALSE)3data <- read.csv(path)
    values this step2 rows x 1 colfile/tmp/file.csvpath
  3. data ← 2 rows x 1 col

    2write.csv(data.frame(x = c(1, 2)), path, row.names = FALSE)3data <- read.csv(path)4total <- sum(data$x)
    values this step2 rows x 1 coldata/tmp/file.csvpath
  4. total ← 3

    3data <- read.csv(path)4total <- sum(data$x)5unlink(path)
    values this step3total1, 2data$x
  5. file ← removed

    4total <- sum(data$x)5unlink(path)6cat(total, "\n", sep = "")
    values this stepremovedfile/tmp/file.csvpath
  6. cat(total, " ", sep = "")

    5unlink(path)6cat(total, "\n", sep = "")
    output3
    values this step3total
tempfile `tempfile` creates a path for short-lived data.
write.csv `write.csv` writes a data frame to a CSV file.
read.csv `read.csv` loads a CSV file into a data frame.