file_writer: Lazy File Writer

View source: R/writer.R

file_writerR Documentation

Lazy File Writer

Description

Generates a closure that writes binary (raw) data to a file.

Usage

file_writer(path, append = FALSE)

Arguments

path

file name or path on disk

append

open file in append mode

Details

The writer function automatically opens the file on the first write and closes when it goes out of scope, or explicitly by setting close = TRUE. This can be used for the data callback in multi_add() or curl_fetch_multi() such that we only keep open file handles for active downloads. This prevents running out of file descriptors when performing thousands of concurrent requests.

Value

Function with signature writer(data = raw(), close = FALSE)

Examples

# Doesn't open yet
tmp <- tempfile()
writer <- file_writer(tmp)

# Now it opens
writer(charToRaw("Hello!\n"))
writer(charToRaw("How are you?\n"))

# Close it!
writer(charToRaw("All done!\n"), close = TRUE)

# Check it worked
readLines(tmp)

curl documentation built on Sept. 30, 2024, 9:35 a.m.