R: Read a text file, process the text with a function, and write...

process_file {xfun} | R Documentation

## 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))

### Arguments

`file` | Path to a text file.
`FUN` | A function to process the text.
`x` | The content of the file.

### 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)

[Package xfun version 0.20 Index]