What is the best way to build a request URL with parameters in R? Thus far I came up with this:
library(magrittr)
library(httr)
library(data.table)
url <- list(hostname = "geo.stat.fi/geoserver/vaestoalue/wfs",
scheme = "https",
query = list(service = "WFS",
version = "2.0.0",
request = "GetFeature",
typename = "vaestoalue:kunta_vaki2017",
outputFormat = "application/json")) %>%
setattr("class","url")
request <- build_url(url)
What I like about the code that I have now, is that I can easily change parameter values and rebuild the URL.
Also, the resulting url is properly html encoded:
https://geo.stat.fi/geoserver/vaestoalue/wfs/?service=WFS&version=2.0.0&request=GetFeature&typename=vaestoalue%3Akunta_vaki2017&outputFormat=application%2Fjson
But loading the data.table library, only to build an url, just doesn't feel right. Is there a better to do this?
You absolutely don't need data.table
to build URLs. As José noted, it was loaded to use a single convenience function you can just mimic with:
set_class <- function(o, v) { class(o) <- v ; invisible(o) }
Also, unless the goal is to have a URL vs just read data from a site, you can also just use httr
verbs:
httr::GET(
url = "https://geo.stat.fi/geoserver/vaestoalue/wfs",
query = list(
service = "WFS",
version = "2.0.0",
request = "GetFeature",
typename = "vaestoalue:kunta_vaki2017",
outputFormat = "application/json"
)
) -> res
dat <- httr::content(res)
str(dat, 1)
## List of 5
## $ type : chr "FeatureCollection"
## $ totalFeatures: int 311
## $ features :List of 311
## $ crs :List of 2
## $ bbox :List of 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With