Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

write-this

Write into or over a file


Description

Helpers to write into or over a new or pre-existing file. Designed mostly for for internal use. File is written with UTF-8 encoding.

Usage

write_union(path, lines, quiet = FALSE)

write_over(path, lines, quiet = FALSE)

Arguments

path

Path to target file. It is created if it does not exist, but the parent directory must exist.

lines

Character vector of lines. For write_union(), these are lines to add to the target file, if not already present. For write_over(), these are the exact lines desired in the target file.

quiet

Logical. Whether to message about what is happening.

contents

Character vector of lines.

Value

Logical indicating whether a write occurred, invisibly.

Functions

  • write_union: writes lines to a file, taking the union of what's already there, if anything, and some new lines. Note, there is no explicit promise about the line order. Designed to modify simple config files like .Rbuildignore and .gitignore.

  • write_over: writes a file with specific lines, creating it if necessary or overwriting existing, if proposed contents are not identical and user is available to give permission.

Examples

write_union("a_file", letters[1:3])
readLines("a_file")
write_union("a_file", letters[1:5])
readLines("a_file")

write_over("another_file", letters[1:3])
readLines("another_file")
write_over("another_file", letters[1:3])
## Not run: 
## will error if user isn't present to approve the overwrite
write_over("another_file", letters[3:1])

## End(Not run)

## clean up
file.remove("a_file", "another_file")

usethis

Automate Package and Project Setup

v2.0.1
MIT + file LICENSE
Authors
Hadley Wickham [aut] (<https://orcid.org/0000-0003-4757-117X>), Jennifer Bryan [aut, cre] (<https://orcid.org/0000-0002-6983-2759>), RStudio [cph, fnd]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.