Debian Codegraph @

man/process_file.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/io.R
3 \name{process_file}
4 \alias{process_file}
5 \alias{sort_file}
6 \title{Read a text file, process the text with a function, and write the text back}
7 \usage{
8 process_file(file, fun = identity, x = read_utf8(file))
9
10 sort_file(..., fun = sort)
11 }
12 \arguments{
13 \item{file}{Path to a text file.}
14
15 \item{fun}{A function to process the text.}
16
17 \item{x}{The content of the file.}
18
19 \item{...}{Arguments to be passed to \code{process_file()}.}
20 }
21 \value{
22 If \code{file} is provided, invisible \code{NULL} (the file is updated as a
23 side effect), otherwise the processed content (as a character vector).
24 }
25 \description{
26 Read a text file with the UTF-8 encoding, apply a function to the text, and
27 write back to the original file if the processed text is different with the
28 original input.
29 }
30 \details{
31 \code{sort_file()} is an application of \code{process_file()}, with the processing
32 function being \code{\link[=sort]{sort()}}, i.e., it sorts the text lines in a file and write
33 back the sorted text.
34 }
35 \examples{
36 f = tempfile()
37 xfun::write_utf8("Hello World", f)
38 xfun::process_file(f, function(x) gsub("World", "woRld", x))
39 xfun::read_utf8(f) # see if it has been updated
40 file.remove(f)
41 }