Skip to content

Commit

Permalink
fix json parsing error preventing container from working all together
Browse files Browse the repository at this point in the history
using the `geocode()` function inside the script would fail because
ruby would also return a warning; for example:

```r
> geocode("224 Woolper Ave Cincinnati OH 45220")
Error: lexical error: invalid char in json text.
                                      /var/lib/gems/2.5.0/gems/Geocode
                     (right here) ------^

> addr_string <- "224 Woolper Ave Cincinnati OH 45220"
> system2('ruby',
+                    args = c('/root/geocoder/geocode.rb', shQuote(addr_string)),
+                    stderr=TRUE,stdout=TRUE)
[1] "/var/lib/gems/2.5.0/gems/Geocoder-US-2.0.4/lib/geocoder/us/database.rb:633: warning: constant ::Fixnum is deprecated"
[2] "[{\"street\":\"Woolper Ave\",\"zip\":\"45220\",\"city\":\"Cincinnati\",\"state\":\"OH\",\"lat\":39.150131,\"lon\":-84.515313,\"fips_county\":\"39061\",\"score\":0.947,\"prenum\":\"\",\"number\":\"224\",\"precision\":\"range\"}]"
```

this fix now only takes the second system/ruby output if its length is 2
  • Loading branch information
cole-brokamp committed Sep 10, 2020
1 parent 6fe3401 commit f1a01ad
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions geocode.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ geocode <- function(addr_string) {
stopifnot(class(addr_string)=='character')
out <- system2('ruby',
args = c('/root/geocoder/geocode.rb', shQuote(addr_string)),
stderr=TRUE,stdout=TRUE) %>%
jsonlite::fromJSON()
stderr=TRUE,stdout=TRUE)
# some versions of ruby return a warning, so only take the second system2 output string
if (length(out) == 2) out <- out[2]
out <- jsonlite::fromJSON(out)
# if geocoder returns nothing then system will return empty list
if (length(out) == 0) out <- tibble(lat = NA, lon = NA, score = NA, precision = NA)
out
Expand Down

0 comments on commit f1a01ad

Please sign in to comment.