ropensci / jqr

R interface to jq
https://docs.ropensci.org/jqr
Other
143 stars 13 forks source link

Some minor changes #28

Closed smbache closed 8 years ago

smbache commented 8 years ago

This PR has the following improvement:

Therefore, e.g.

json %>% keys %>% str

will allow you to inspect the jqr object. Before this would give an error.

(json %>% keys) %>% str

This, however, will execute (since its a nested pipeline) and str will inspect the resulting json. Nothing special about str here, it's just an example.

In addition: I suggest using ... in the generic jq to construct low-level queries (combined with " | "). This will make long queries easier to pass in, but more importantly make a seamless connection between low-level and high-level calls:

j %>% jq("keys") %>% dot
#> ["results","status"] 
j %>% keys %>% jq(".")
#> ["results","status"] 

This example, although not that long query, shows how one can use the ...:

lat_lng <- function(where)
{
  url <- sprintf("https://maps.googleapis.com/maps/api/geocode/json?address=%s",
                 where)

  (readLines(url)
   %>% paste(collapse = "")
   %>% jq(".results[] .geometry .location", 
          ".lat, .lng")
   %>% as.numeric
   %>% setNames(c("lat", "lng"))
  )
}

lat_lng("Copenhagen")
#>      lat      lng 
#> 55.67610 12.56834 
sckott commented 8 years ago

Thanks! Checked it out locally, LGTM

smbache commented 8 years ago

Great!