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

process_file

Read a text file, process the text with a function, and write the text back


Description

Read a text file with the UTF-8 encoding, apply a function to the text, and write back to the original file.

Usage

process_file(file, fun = identity, x = read_utf8(file))

sort_file(..., fun = sort)

Arguments

file

Path to a text file.

fun

A function to process the text.

x

The content of the file.

...

Arguments to be passed to process_file().

Details

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.

Value

If file is provided, invisible NULL (the file is updated as a side effect), otherwise the processed content (as a character vector).

Examples

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
file.remove(f)

xfun

Miscellaneous Functions to Support Packages Maintained by 'Yihui Xie'

v0.22
MIT + file LICENSE
Authors
Yihui Xie [aut, cre, cph] (<https://orcid.org/0000-0003-0645-5666>), Wush Wu [ctb], Daijiang Li [ctb], Xianying Tan [ctb], Salim Brüggemann [ctb] (<https://orcid.org/0000-0002-5329-5987>), Christophe Dervieux [ctb]
Initial release

We don't support your browser anymore

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