Debian Codegraph @

man/file_writer.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/writer.R
3 \name{file_writer}
4 \alias{file_writer}
5 \title{Lazy File Writer}
6 \usage{
7 file_writer(path, append = FALSE)
8 }
9 \arguments{
10 \item{path}{file name or path on disk}
11
12 \item{append}{open file in append mode}
13 }
14 \value{
15 Function with signature \code{writer(data = raw(), close = FALSE)}
16 }
17 \description{
18 Generates a closure that writes binary (raw) data to a file.
19 }
20 \details{
21 The writer function automatically opens the file on the first write and closes when
22 it goes out of scope, or explicitly by setting \code{close = TRUE}. This can be used
23 for the \code{data} callback in \code{multi_add()} or \code{curl_fetch_multi()} such
24 that we only keep open file handles for active downloads. This prevents running out
25 of file descriptors when performing thousands of concurrent requests.
26 }
27 \examples{
28 # Doesn't open yet
29 tmp <- tempfile()
30 writer <- file_writer(tmp)
31
32 # Now it opens
33 writer(charToRaw("Hello!\n"))
34 writer(charToRaw("How are you?\n"))
35
36 # Close it!
37 writer(charToRaw("All done!\n"), close = TRUE)
38
39 # Check it worked
40 readLines(tmp)
41 }