Utility that collects data from the HTTP reply into lines and calls user-provided function.
chunkToLineReader.RdWhen one provides an R function to process the body of the R rep
Details
This constructs a closure and then processes each chunk as they are passed to the read function. It strips away any text that does not form a complete line at the end of the chunk and holds this to be added to the next chunk being processed.
Value
A list with two components
- read
the function that will do the actual reading from the HTTP response stream and call the function
fon each step (assuming the chunk has a line marker.- comp2
Description of 'comp2'
...
References
Curl homepage https://curl.se/
See also
getURI and the write argument.
getForm, postForm
curlPerform
Examples
# Read a rectangular table of data into R from the URL
# and add up the values and the number of values read.
summer =
function()
{
total = 0.0
numValues = 0
list(read = function(txt) {
con = textConnection(txt)
on.exit(close(con))
els = scan(con)
numValues <<- numValues + length(els)
total <<- total + sum(els)
""
},
result = function() c(total = total, numValues = numValues))
}
s = summer()
tryCatch(
getURL("https://aitap.grebedoc.dev/RCurl/matrix.data", write = chunkToLineReader(s$read)$read),
GenericCurlError = identity
)
#> function (txt)
#> {
#> if (nzchar(leftOvers)) {
#> txt = paste(leftOvers, txt, sep = "")
#> leftOvers <<- ""
#> }
#> leftOvers <<- gsub("^.*\n", "", txt)
#> if (verbose && nchar(leftOvers))
#> cat("Left over:", leftOvers, "\n")
#> Lines = strsplit(txt, "\n")[[1]]
#> if (nchar(leftOvers))
#> Lines = Lines[-length(Lines)]
#> if (length(Lines))
#> f(Lines)
#> nchar(txt)
#> }
#> <bytecode: 0x55c5b60b0c40>
#> <environment: 0x55c5b60b3180>