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

binaryBuffer

Create internal C-level data structure for collecting binary data


Description

This is the constructor function for creating an internal data structure that is used when reading binary data from an HTTP request via RCurl. It is used with the native routine R_curl_write_binary_data for collecting the response from the HTTP query into a buffer that stores the bytes. The contents can then be brought back into R as a raw vector and then used in different ways, e.g. uncompressed with the Rcompression package, or written to a file via writeBin.

Usage

binaryBuffer(initialSize = 5000)

Arguments

initialSize

a number giving the size (number of bytes) to allocate for the buffer. In most cases, the size won't make an enormous difference. If this is small, the R_curl_write_binary_data routine will expand it as necessary when more daat is received than would fit in it. If it is very large, i.e. larger than the resulting response, the consequence is simply unused memory. One can determine the appropriate size by performing the HTTP request with nobody = TRUE and looking at the resulting size indicated by the headers of the response, i.e. getCurlInfo(handle) and then using that size and repeating the request and receiving the body. This is a trade-off between network speed and memor consumption and processing speed when collecting the .

Value

An object of class RCurlBinaryBuffer which is to be treated as an opaque data for the most part. When passing this as the value of the file option, one will have to pass the ref slot.

After the contents have been read, one can convert this object to an R raw vector using as(buf, "raw").

Author(s)

Duncan Temple Lang

References

Curl homepage http://curl.haxx.se

See Also

R_curl_write_binary_data

Examples

if(url.exists("http://www.omegahat.net/RCurl/xmlParse.html.gz")) {
  buf = binaryBuffer()

     # Now fetch the binary file.
  getURI("http://www.omegahat.net/RCurl/xmlParse.html.gz",
         write = getNativeSymbolInfo("R_curl_write_binary_data")$address,
         file = buf@ref)

   # Convert the internal data structure into an R raw vector
  b = as(buf, "raw")

  if (getRversion() >= "4")
    txt = memDecompress(b, asChar = TRUE)
  ## or txt = Rcompression::gunzip(b)
 }

RCurl

General Network (HTTP/FTP/...) Client Interface for R

v1.98-1.3
BSD_3_clause + file LICENSE
Authors
CRAN Team [ctb, cre] (de facto maintainer since 2013), Duncan Temple Lang [aut] (<https://orcid.org/0000-0003-0159-1546>)
Initial release

We don't support your browser anymore

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