File: writing-options.R

package info (click to toggle)
r-cran-crul 1.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,620 kB
  • sloc: sh: 13; makefile: 2
file content (71 lines) | stat: -rw-r--r-- 2,175 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#' Writing data options
#'
#' @name writing-options
#' @examples \dontrun{
#' # write to disk
#' (x <- HttpClient$new(url = "https://httpbin.org"))
#' f <- tempfile()
#' res <- x$get("get", disk = f)
#' res$content # when using write to disk, content is a path
#' readLines(res$content)
#' close(file(f))
#'
#' # streaming response
#' (x <- HttpClient$new(url = "https://httpbin.org"))
#' res <- x$get('stream/50', stream = function(x) cat(rawToChar(x)))
#' res$content # when streaming, content is NULL
#'
#'
#' ## Async
#' (cc <- Async$new(
#'   urls = c(
#'     'https://httpbin.org/get?a=5',
#'     'https://httpbin.org/get?foo=bar',
#'     'https://httpbin.org/get?b=4',
#'     'https://httpbin.org/get?stuff=things',
#'     'https://httpbin.org/get?b=4&g=7&u=9&z=1'
#'   )
#' ))
#' files <- replicate(5, tempfile())
#' (res <- cc$get(disk = files, verbose = TRUE))
#' lapply(files, readLines)
#'
#' ## Async varied
#' ### disk
#' f <- tempfile()
#' g <- tempfile()
#' req1 <- HttpRequest$new(url = "https://httpbin.org/get")$get(disk = f)
#' req2 <- HttpRequest$new(url = "https://httpbin.org/post")$post(disk = g)
#' req3 <- HttpRequest$new(url = "https://httpbin.org/get")$get()
#' (out <- AsyncVaried$new(req1, req2, req3))
#' out$request()
#' out$content()
#' readLines(f)
#' readLines(g)
#' out$parse()
#' close(file(f))
#' close(file(g))
#'
#' ### stream - to console
#' fun <- function(x) print(x)
#' req1 <- HttpRequest$new(url = "https://httpbin.org/get"
#' )$get(query = list(foo = "bar"), stream = fun)
#' req2 <- HttpRequest$new(url = "https://httpbin.org/get"
#' )$get(query = list(hello = "world"), stream = fun)
#' (out <- AsyncVaried$new(req1, req2))
#' out$request()
#' out$content()
#'
#' ### stream - to an R object
#' lst <- list()
#' fun <- function(x) lst <<- append(lst, list(x))
#' req1 <- HttpRequest$new(url = "https://httpbin.org/get"
#' )$get(query = list(foo = "bar"), stream = fun)
#' req2 <- HttpRequest$new(url = "https://httpbin.org/get"
#' )$get(query = list(hello = "world"), stream = fun)
#' (out <- AsyncVaried$new(req1, req2))
#' out$request()
#' lst
#' cat(vapply(lst, function(z) rawToChar(z$content), ""), sep = "\n")
#' }
NULL