Demo

library(ODataQuery)

Introduction

Here I’ll show some examples to use this package

Create service

service <- ODataQuery$new("https://services.odata.org/V4/TripPinServiceRW")
service$all()
#>                name           kind
#> 1            Photos      EntitySet
#> 2            People      EntitySet
#> 3          Airlines      EntitySet
#> 4          Airports      EntitySet
#> 5                Me      Singleton
#> 6 GetNearestAirport FunctionImport
#>                                                                                             url
#> 1            http://services.odata.org/V4/(S(5nc4yxfk4uf0zsocmrcuuq1j))/TripPinServiceRW/Photos
#> 2            http://services.odata.org/V4/(S(5nc4yxfk4uf0zsocmrcuuq1j))/TripPinServiceRW/People
#> 3          http://services.odata.org/V4/(S(5nc4yxfk4uf0zsocmrcuuq1j))/TripPinServiceRW/Airlines
#> 4          http://services.odata.org/V4/(S(5nc4yxfk4uf0zsocmrcuuq1j))/TripPinServiceRW/Airports
#> 5                http://services.odata.org/V4/(S(5nc4yxfk4uf0zsocmrcuuq1j))/TripPinServiceRW/Me
#> 6 http://services.odata.org/V4/(S(5nc4yxfk4uf0zsocmrcuuq1j))/TripPinServiceRW/GetNearestAirport

Entity set

people_entity <- service$path("People")

# Get first page
people_entity$retrieve(count=TRUE)
#> $`@odata.count`
#> [1] 20
#> 
#> $`@odata.nextLink`
#> [1] "https://services.odata.org/V4/(S(j0yfx2ikz4gvkb3ufawsoaiz))/TripPinServiceRW/People?%24count=true&%24skiptoken=8"
#> 
#> $value
#>           UserName FirstName  LastName                                   Emails
#> 1     russellwhyte   Russell     Whyte [email protected], [email protected]
#> 2     scottketchum     Scott   Ketchum                        [email protected]
#> 3      ronaldmundy    Ronald     Mundy   [email protected], [email protected]
#> 4     javieralfred    Javier    Alfred   [email protected], [email protected]
#> 5    willieashmore    Willie   Ashmore   [email protected], [email protected]
#> 6 vincentcalabrese   Vincent Calabrese [email protected], [email protected]
#> 7       clydeguess     Clyde     Guess                        [email protected]
#> 8    keithpinckney     Keith  Pinckney     [email protected], [email protected]
#>                                             AddressInfo Gender  Concurrency
#> 1             187 Suffolk Ln., United States, Boise, ID   Male 6.386847e+17
#> 2       2817 Milton Dr., United States, Albuquerque, NM   Male 6.386847e+17
#> 3                                                  NULL   Male 6.386847e+17
#> 4 89 Jefferson Way Suite 2, United States, Portland, WA   Male 6.386847e+17
#> 5                                                  NULL   Male 6.386847e+17
#> 6         55 Grizzly Peak Rd., United States, Butte, MT   Male 6.386847e+17
#> 7                                                  NULL   Male 6.386847e+17
#> 8                                                  NULL   Male 6.386847e+17

# Get all pages
people_entity$select("UserName", "FirstName", "LastName")$all()
#>            UserName FirstName  LastName
#> 1      russellwhyte   Russell     Whyte
#> 2      scottketchum     Scott   Ketchum
#> 3       ronaldmundy    Ronald     Mundy
#> 4      javieralfred    Javier    Alfred
#> 5     willieashmore    Willie   Ashmore
#> 6  vincentcalabrese   Vincent Calabrese
#> 7        clydeguess     Clyde     Guess
#> 8     keithpinckney     Keith  Pinckney
#> 9     marshallgaray  Marshall     Garay
#> 10    ryantheriault      Ryan Theriault
#> 11    elainestewart    Elaine   Stewart
#> 12    salliesampson    Sallie   Sampson
#> 13      jonirosales      Joni   Rosales
#> 14   georginabarlow  Georgina    Barlow
#> 15     angelhuffman     Angel   Huffman
#> 16     laurelosborn    Laurel    Osborn
#> 17      sandyosborn     Sandy    Osborn
#> 18     ursulabright    Ursula    Bright
#> 19  genevievereeves Genevieve    Reeves
#> 20       kristakemp    Krista      Kemp

Singleton

russellwhyte <- people_entity$get("russellwhyte")
first_person <- people_entity$top(1)$one()
head(first_person)
#> $UserName
#> [1] "russellwhyte"
#> 
#> $FirstName
#> [1] "Russell"
#> 
#> $LastName
#> [1] "Whyte"
#> 
#> $Emails
#> [1] "[email protected]" "[email protected]"
#> 
#> $AddressInfo
#>           Address City.CountryRegion City.Name City.Region
#> 1 187 Suffolk Ln.      United States     Boise          ID
#> 
#> $Gender
#> [1] "Male"

russellwhyte_friends <- russellwhyte$path("Friends")
russellwhyte_friends$all()
#>       UserName FirstName LastName                                 Emails
#> 1 scottketchum     Scott  Ketchum                      [email protected]
#> 2  ronaldmundy    Ronald    Mundy [email protected], [email protected]
#> 3 javieralfred    Javier   Alfred [email protected], [email protected]
#> 4 angelhuffman     Angel  Huffman                      [email protected]
#>                                             AddressInfo Gender  Concurrency
#> 1       2817 Milton Dr., United States, Albuquerque, NM   Male 6.386847e+17
#> 2                                                  NULL   Male 6.386847e+17
#> 3 89 Jefferson Way Suite 2, United States, Portland, WA   Male 6.386847e+17
#> 4                                                  NULL Female 6.386847e+17

Function call

get_nearest_airport <- service$func('GetNearestAirport')
get_nearest_airport(lat = 33, lon = -118)
#> $IcaoCode
#> [1] "KLAX"
#> 
#> $Name
#> [1] "Los Angeles International Airport"
#> 
#> $IataCode
#> [1] "LAX"
#> 
#> $Location
#> $Location$Address
#> [1] "1 World Way, Los Angeles, CA, 90045"
#> 
#> $Location$City
#> $Location$City$CountryRegion
#> [1] "United States"
#> 
#> $Location$City$Name
#> [1] "Los Angeles"
#> 
#> $Location$City$Region
#> [1] "California"
#> 
#> 
#> $Location$Loc
#> $Location$Loc$type
#> [1] "Point"
#> 
#> $Location$Loc$coordinates
#> [1] -118.4081   33.9425
#> 
#> $Location$Loc$crs
#> $Location$Loc$crs$type
#> [1] "name"
#> 
#> $Location$Loc$crs$properties
#> $Location$Loc$crs$properties$name
#> [1] "EPSG:4326"

Querying

people_query <- people_entity$
  top(5)$
  select('FirstName', 'LastName')$
  filter(Concurrency.gt = 500)$
  expand('Friends($count=true)')$
  orderby('LastName')

people_query$all()
#>   FirstName  LastName [email protected]
#> 1    Javier    Alfred                   3
#> 2    Willie   Ashmore                   2
#> 3  Georgina    Barlow                   1
#> 4    Ursula    Bright                   1
#> 5   Vincent Calabrese                   2
#>                                                                                                                                                                                                                                                                                                                                                               Friends
#> 1 willieashmore, vincentcalabrese, georginabarlow, Willie, Vincent, Georgina, Ashmore, Calabrese, Barlow, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], 55 Grizzly Peak Rd., United States, Butte, MT, Male, Male, Female, 638684672126977408, 638684672126977408, 638684672126977408
#> 2                                                      javieralfred, vincentcalabrese, Javier, Vincent, Alfred, Calabrese, [email protected], [email protected], [email protected], [email protected], 89 Jefferson Way Suite 2, United States, Portland, WA, 55 Grizzly Peak Rd., United States, Butte, MT, Male, Male, 638684672126977408, 638684672126977408
#> 3                                                                                                                                                                                                               javieralfred, Javier, Alfred, [email protected], [email protected], 89 Jefferson Way Suite 2, United States, Portland, WA, Male, 638684672126977408
#> 4                                                                                                                                                                                                                                                                      keithpinckney, Keith, Pinckney, [email protected], [email protected], Male, 638684672126977408
#> 5                                                                                                             javieralfred, willieashmore, Javier, Willie, Alfred, Ashmore, [email protected], [email protected], [email protected], [email protected], 89 Jefferson Way Suite 2, United States, Portland, WA, Male, Male, 638684672126977408, 638684672126977408

Other endpoints

Statistics, the Netherlands

opendata_service <- ODataQuery$new("http://beta-odata4.cbs.nl/")
entity_81589NED  <- opendata_service$path("CBS", "81589NED", Observations")
dataset_81589NED <- entity_81589NED$all()

Northwind (OData v2)

Connecting with older OData v2 works, but some features have been changed.

northwind_service <- ODataQuery$new("https://services.odata.org/V2/Northwind/Northwind.svc/")
customer_entity <- northwind_service$path("Customers")
customer_entity$
  select("CompanyName", "Address", "Phone")$
  filter(Country.eq = "Germany", City.eq = "Berlin")

The Hague

So far, I haven’t been able to figure out how their data model works, but at least we can connect to it.

denhaag_service <- ODataQuery$new("https://denhaag.incijfers.nl/jiveservices/odata/")
denhaag_service$path('DataSources')