lverweijen / ODataQuery

Query on R OData

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Passing in filter items from vectors and lists

williamlai2 opened this issue · comments

Hi, I am looking to pass in multiple items to filter a dataset using purrr::map() but am having trouble with this.

This is an example for one item usin. Is there a way of doing this?

# from https://github.com/lverweijen/ODataQuery
library(ODataQuery)

url <- "https://services.odata.org/V4/TripPinServiceRW"
trip_service <- ODataQuery$new(url)
people_entity <- trip_service$path("People")

lastnames <- c("Osborn", "Kemp")

# this works --------
people_entity$
  filter(to_odata(LastName == "Osborn"))$
  all()

# this doesn't -------
people_entity$
  filter(to_odata(LastName == lastname[1]))$
  all()

Error in retrieve_data(next_link, ...) :
Internal Server Error (HTTP 500).

This doesn't work either. Same error:

formula1 <- str2expression(paste("LastName == ", lastnames[1]))

people_entity$
  filter(to_odata(eval(formula1)))$
  all()

One work around would be to query like this. Is it possible somehow using the package?

https://services.odata.org/V4/(S(lp5pwzeiga3wy20vkcoejgvv))/TripPinServiceRW/People?$filter=(LastName%20eq%20%27Osborn%27)

Actually, it is something like this, though works better with the data I am working with.

formula1 <- paste("LastName eq '", lastnames[1], "'")

people_entity$
  filter(formula1)$
  all()

Expressions can be escaped with !! . In that case the inner expression will be evaluated in the context of the environment instead of the context of OData.

So your query can be written as:

lastnames <- c("Osborn", "Kemp")
people_entity$
  filter(to_odata(LastName == !!(lastnames[1])))$
  all()

Thanks for that. I'll try it out.