library(readr)Reading from and Writing to Files in R
Fundamentals of Data Science
The R/tidyverse library readr is the main tool for reading various types of data files.
file <- read_file("data/gettysburg.txt")line <- read_lines("data/gettysburg.txt", n_max = 1)lines <- read_lines("data/gettysburg.txt")text <- c("Now is the time", "for us to rise up", "against our robot overlords")
write_lines(text, "data/robots.txt", sep = "-")# write_file overwrites. Use the append flag to append
write_file(paste0(text, collapse = " ")[1], "data/robots.txt", append = TRUE)File operations
The fs library allows you to do file manipulations.
library(fs)
# file_create
# file_move
# file_copy
# file_ls to list directory
# file_exists
# file_show