EVOLUTION-MANAGER
Edit File: basicTextGatherer.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Cumulate text across callbacks (from an HTTP response)</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for basicTextGatherer {RCurl}"><tr><td>basicTextGatherer {RCurl}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Cumulate text across callbacks (from an HTTP response)</h2> <h3>Description</h3> <p>These functions create callback functions that can be used to with the libcurl engine when it passes information to us when it is available as part of the HTTP response. </p> <p><code>basicTextGatherer</code> is a generator function that returns a closure which is used to cumulate text provided in callbacks from the libcurl engine when it reads the response from an HTTP request. </p> <p><code>debugGatherer</code> can be used with the <code>debugfunction</code> libcurl option in a call and the associated <code>update</code> function is called whenever libcurl has information about the header, data and general messages about the request. </p> <p>These functions return a list of functions. Each time one calls <code>basicTextGatherer</code> or <code>debugGatherer</code>, one gets a new, separate collection of functions. However, each collection of functions (or instance) shares the variables across the functions and across calls. This allows them to store data persistently across the calls without using a global variable. In this way, we can have multiple instances of the collection of functions, with each instance updating its own local state and not interfering with those of the others. </p> <p>We use an S3 class named <code>RCurlCallbackFunction</code> to indicate that the collection of funcions can be used as a callback. The <code>update</code> function is the one that is actually used as the callback function in the CURL option. The <code>value</code> function can be invoked to get the current state that has been accumulated by the <code>update</code> function. This is typically used when the request is complete. One can reuse the same collection of functions across different requests. The information will be cumulated. Sometimes it is convenient to reuse the object but reset the state to its original empty value, as it had been created afresh. The <code>reset</code> function in the collection permits this. </p> <p><code>multiTextGatherer</code> is used when we are downloading multiple URIs concurrently in a single libcurl operation. This merely uses the tools of <code>basicTextGatherer</code> applied to each of several URIs. See <code><a href="getURIAsynchronous.html">getURIAsynchronous</a></code>. </p> <h3>Usage</h3> <pre> basicTextGatherer(txt = character(), max = NA, value = NULL, .mapUnicode = TRUE) multiTextGatherer(uris, binary = rep(NA, length(uris))) debugGatherer() </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>txt</code></td> <td> <p>an initial character vector to start things. We allow this to be specified so that one can initialize the content. </p> </td></tr> <tr valign="top"><td><code>max</code></td> <td> <p>if specified as an integer this controls the total number of characters that will be read. If more are read, the function tells libcurl to stop!</p> </td></tr> <tr valign="top"><td><code>uris</code></td> <td> <p>for <code>multiTextGatherer</code>, this is either the number or the names of the uris being downloaded and for which we need a separate writer function. </p> </td></tr> <tr valign="top"><td><code>value</code></td> <td> <p>if specified, a function that is called when retrieving the text usually after the completion of the request and the processing of the response. This function can be used to convert the result into a different format, e.g. parse an XML document, read values from table in the text.</p> </td></tr> <tr valign="top"><td><code>.mapUnicode</code></td> <td> <p>a logical value that controls whether the resulting text is processed to map components of the form \uxxxx to their appropriate Unicode representation.</p> </td></tr> <tr valign="top"><td><code>binary</code></td> <td> <p>a logical vector that indicates which URIs yield binary content</p> </td></tr> </table> <h3>Details</h3> <p>This is called when the libcurl engine finds sufficient data on the stream from which it is reading the response. It cumulates these bytes and hands them to a C routine in this package which calls the actual gathering function (or a suitable replacement) returned as the <code>update</code> component from this function. </p> <h3>Value</h3> <p>Both the <code>basicTextGatherer</code> and <code>debugGatherer</code> functions return an object of class <code>RCurlCallbackFunction</code>. <code>basicTextGatherer</code> extends this with the class <code>RCurlTextHandler</code> and <code>debugGatherer</code> extends this with the class <code>RCurlDebugHandler</code>. Each of these has the same basic structure, being a list of 3 functions. </p> <table summary="R valueblock"> <tr valign="top"><td><code>update</code></td> <td> <p>the function that is called with the text from the callback routine and which processes this text by accumulating it into a vector</p> </td></tr> <tr valign="top"><td><code>value</code></td> <td> <p>a function that returns the text cumulated across the callbacks. This takes an argument <code>collapse</code> (and additional ones) that are handed to <code><a href="../../base/html/paste.html">paste</a></code>. If the value of <code>collapse</code> is given as <code>NULL</code>, the vector of elements containing the different text for each callback is returned. This is convenient when debugging or if one knows something about the nature of the callbacks, e.g. the regular size that causes iit to identify records in a natural way. </p> </td></tr> <tr valign="top"><td><code>reset</code></td> <td> <p>a function that resets the internal state to its original, empty value. This can be used to reuse the same object across requests but to avoid cumulating new input with the material from previous requests.</p> </td></tr> </table> <p><code>multiTextGatherer</code> returns a list with an element corresponding to each URI. Each element is an object obtained by calling <code>basicTextGatherer</code>, i.e. a collection of 3 functions with shared state. </p> <h3>Author(s)</h3> <p>Duncan Temple Lang</p> <h3>References</h3> <p>Curl homepage <a href="http://curl.haxx.se">http://curl.haxx.se</a></p> <h3>See Also</h3> <p><code><a href="getURL.html">getURL</a></code> <code><a href="dynCurlReader.html">dynCurlReader</a></code> </p> <h3>Examples</h3> <pre> if(url.exists("http://www.omegahat.net/RCurl/index.html")) withAutoprint({ txt = getURL("http://www.omegahat.net/RCurl/index.html", write = basicTextGatherer()) h = basicTextGatherer() txt = getURL("http://www.omegahat.net/RCurl/index.html", write = h$update) ## Cumulate across pages. txt = getURL("http://www.omegahat.net/index.html", write = h$update) headers = basicTextGatherer() txt = getURL("http://www.omegahat.net/RCurl/index.html", header = TRUE, headerfunction = headers$update) ## Now read the headers. cat(headers$value()) headers$reset() ## Debugging callback d = debugGatherer() x = getURL("http://www.omegahat.net/RCurl/index.html", debugfunction = d$update, verbose = TRUE) cat(names(d$value())) d$value()[["headerIn"]] ## This hangs on Solaris uris = c("http://www.omegahat.net/RCurl/index.html", "http://www.omegahat.net/RCurl/philosophy.html") g = multiTextGatherer(uris) txt = getURIAsynchronous(uris, write = g) names(txt) # no names this way nchar(txt) # Now don't use names for the gatherer elements. g = multiTextGatherer(length(uris)) txt = getURIAsynchronous(uris, write = g) names(txt) nchar(txt) }) ## Not run: Sys.setlocale(,"en_US.latin1") Sys.setlocale(,"en_US.UTF-8") uris = c("http://www.omegahat.net/RCurl/index.html", "http://www.omegahat.net/RCurl/philosophy.html") g = multiTextGatherer(uris) txt = getURIAsynchronous(uris, write = g) ## End(Not run) </pre> <hr /><div style="text-align: center;">[Package <em>RCurl</em> version 1.98-1.2 <a href="00Index.html">Index</a>]</div> </body></html>