how to create a list of httr2::request object for use with httr2::req_perform_parallel() and httr2::req_perform_sequential()

Apparently httr2::request() objects are themselves lists, so how does one create a list of requests for use with httr2::req_perform_parallel() and httr2::req_perform_sequential() since R does not allow storing list objects inside of a list yet both of these functions require an input of a list of requests.

Here is a fictional example using a nonexistent api on "http://www.someapi.com"

library(httr2)
library(magrittr)

request_list <- vector(mode = "list", length = 3)

request_list[1] <- "http://www.someapi.com/API?x=test" %>% request()
#> Warning in request_list[1] <- "http://www.someapi.com/API?x=test" %>%
#> request(): number of items to replace is not a multiple of replacement length
request_list[2] <- "http://www.someapi.com/API?x=confused" %>% request()
#> Warning in request_list[2] <- "http://www.someapi.com/API?x=confused" %>% :
#> number of items to replace is not a multiple of replacement length
request_list[3] <- "http://www.someapi.com/API?x=example" %>% request()
#> Warning in request_list[3] <- "http://www.someapi.com/API?x=example" %>% :
#> number of items to replace is not a multiple of replacement length

Created on 2024-07-19 with reprex v2.1.1

Hi @mccroweyclinton-EPA

the documentation has examples Perform a list of requests in parallel — req_perform_parallel • httr2

Here an example to be self contained as an answer

library(httr2)
req <- request("https://httpbin.org/get") |>
  req_method("GET") |>
  req_headers(accept = "application/json",)
multiReq <- list(req, req, req)
resps <- req_perform_parallel(multiReq, on_error = "continue")
resps

I don't know were you read that - but it is not true.

ok thanks, I saw the example in the documentation but didn't understand it. I guess that I should re-word my question. Why doesn't what I did work?

Why can't I assign a request to a list in the manor that I have tried above? What is the reason that I receive this error. I think if I understand the problem here then I will have a better understanding of how lists work.

This R assignment to list element using single vs double brackets - Stack Overflow sums it up pretty nicely .

So do

request_list[[1]] <- "http://www.someapi.com/API?x=test" %>% request()

or

request_list[1] <- list("http://www.someapi.com/API?x=test" %>% request())

Thank you. I never knew that could be done.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.