R/io.R
process_file.Rd
Read a text file with the UTF-8 encoding, apply a function to the text, and write back to the original file if the processed text is different with the original input.
process_file(file, fun = identity, x = read_utf8(file))
sort_file(..., fun = sort)
If file
is provided, invisible NULL
(the file is updated as a
side effect), otherwise the processed content (as a character vector).
sort_file()
is an application of process_file()
, with the processing
function being sort()
, i.e., it sorts the text lines in a file and write
back the sorted text.
f = tempfile()
xfun::write_utf8("Hello World", f)
xfun::process_file(f, function(x) gsub("World", "woRld", x))
xfun::read_utf8(f) # see if it has been updated
#> [1] "Hello woRld"
file.remove(f)
#> [1] TRUE