I have built an API query for a service and I would like to create a loop that iterates through dates building up multiple end data frames. The code I have, so far, looks like this:
query1 <- "search publications in full_data for \"\\\"Education\\\"\"
where type in [ \"article\" ]
and (category_for.name ~\"Education\")
and date_inserted >= \"2019-01\" and date_inserted < \"2019-02\"
return publications[type + all]"
x1 <- dsApiRequest(token = token, query = query)
m1 <- dsApi2df(D)
What I want to do is increase the dates, 2 months by 2 months building up from query1
, x1
and m1
to queryn
, xn
and mn
. Written in full, for the first 2 passes, it would look like this:
query1 <- "search publications in full_data for \"\\\"Education\\\"\"
where type in [ \"article\" ]
and (category_for.name ~\"Education\")
and date_inserted >= \"2019-01\" and date_inserted < \"2019-02\"
return publications[type + all]"
Y1 <- dsApiRequest(token = token, query = query)
N1 <- dsApi2df(D)
THEN
query2 <- "search publications in full_data for \"\\\"Education\\\"\"
where type in [ \"article\" ]
and (category_for.name ~\"Education\")
and date_inserted >= \"2019-03\" and date_inserted < \"2019-04\"
return publications[type + all]"
Y2 <- dsApiRequest(token = token, query = query)
N2 <- dsApi2df(D)
Note the date must also change with each pass.
I like the base
sprintf
command for things like this, though theglue
package is new and has a nice interface. Withsprintf
you put%s
as a placeholder inside a string, and then you can use additional arguments to replace with values.I've "simplified" your query to focus on the changing dates.
With
glue
, you can put variable names inside{braces}
in your string, and they will automatically be filled in when youglue()
it. (Somewhat confusingly, the result prints without quotes, but it is still a character vector and will still work just fine.) (Using the samestart_dates
andend_dates
as above.)