Getting the question right (in this context, readily understandable to someone who does not have the entire context in mind) is harder than getting an answer.
Every R
problem benefits from being addressed as a problem in school algebra: f(x) = y, where the three objects are x, what is in hand, y, what is desired, and f, the function to transform x to y. (Functions in R
are objects of equal standing with vectors, lists, data frames, etc. In particular, they are first-class objects, capable as serving as arguments to other functions.)
For this case, y is a well-formed query, composed of two or more objects, one of which is a string representing a base url
and the others of which are query strings in the form criteria1=10
, with the strings ?
used to separate URL
from the following query strings, and ,
to separate query strings. url
is composed of substrings and delimiters as well— http
, https
, ftp
, etc., and ://and the
.` domain component.
For a specific application is may not be necessary to make distinctions so fine because the components can be chunked into components if it is expected, for example, that all queries will be directed to http://example.com/
. However, keeping the smallest pieces in mind pays dividends in framing the problem.
www.website_to_query.com?criteria1=10"
serves as a base pattern in which only the ?
delimiter string is constant. Rather than treating it as a literal, however, it can be treated as a variable to advantage.
start_query <- '?'
Assuming base urls are always to be subdomain.domain.topdomain
, and that subdomain
will always be www
the assembly of x can begin with creating a vector.
start_query <- '?'
urls <- paste0(paste0("www.",c("abc.com","def.com","xyz.col")),start_query)
Created on 2020-09-26 by the reprex package (v0.3.0.9001)
Moving to the query strings, it helps to bear in mind that they are character strings that represent key:value pairs—they are not themselves key values pairs.
Constructing a query string is done by composing three components: an string representing the key, the =
symbol string and a string representing the value. From vectors representing key and value, query strings are pasted in like manner to urls.
The following script illustrates the approach of building up from sub-objects. It produces a vector of completed query strings which can be feed to an appropriate function to fetch results.
suppressPackageStartupMessages({
library(dplyr)
library(purrr)
library(stringr)
library(tidyr)})
start_query <- '?'
assign_op <- '='
sep <- ','
urls <- paste0(paste0("www.",
rep(c("abc.com","def.com","ghi.com"),3)),
start_query)
keys <- paste0(c("criterion1","criterion2","criterion3","criterion4","criterion5","criterion6","criterion7","criterion8","criterion9"))
values <- as.character(seq(10,90,10))
kv_pairs <- paste0(keys,assign_op,values)
query_string <- str_c(kv_pairs, collapse = sep)
queries <- paste0(urls,query_string)
queries
#> [1] "www.abc.com?criterion1=10,criterion2=20,criterion3=30,criterion4=40,criterion5=50,criterion6=60,criterion7=70,criterion8=80,criterion9=90"
#> [2] "www.def.com?criterion1=10,criterion2=20,criterion3=30,criterion4=40,criterion5=50,criterion6=60,criterion7=70,criterion8=80,criterion9=90"
#> [3] "www.ghi.com?criterion1=10,criterion2=20,criterion3=30,criterion4=40,criterion5=50,criterion6=60,criterion7=70,criterion8=80,criterion9=90"
#> [4] "www.abc.com?criterion1=10,criterion2=20,criterion3=30,criterion4=40,criterion5=50,criterion6=60,criterion7=70,criterion8=80,criterion9=90"
#> [5] "www.def.com?criterion1=10,criterion2=20,criterion3=30,criterion4=40,criterion5=50,criterion6=60,criterion7=70,criterion8=80,criterion9=90"
#> [6] "www.ghi.com?criterion1=10,criterion2=20,criterion3=30,criterion4=40,criterion5=50,criterion6=60,criterion7=70,criterion8=80,criterion9=90"
#> [7] "www.abc.com?criterion1=10,criterion2=20,criterion3=30,criterion4=40,criterion5=50,criterion6=60,criterion7=70,criterion8=80,criterion9=90"
#> [8] "www.def.com?criterion1=10,criterion2=20,criterion3=30,criterion4=40,criterion5=50,criterion6=60,criterion7=70,criterion8=80,criterion9=90"
#> [9] "www.ghi.com?criterion1=10,criterion2=20,criterion3=30,criterion4=40,criterion5=50,criterion6=60,criterion7=70,criterion8=80,criterion9=90"
Created on 2020-09-26 by the reprex package (v0.3.0.9001)