Rstudio Connect Plumber API getting Internal Server 500 error

I have an API that calculated the distance to coast given lat/lon coordinates. I use the shape file found from NOAA Shoreline.

Everything works fine if I run the API locally, but when I publish it to RStudio Connect, I get an internal server 500 error. I also published the default plumber demo and it works just fine, so I'm not sure if it has something to do with the sf package or with the shape file.

The server is set up on Ubuntu 16.04 and I'm using R version 3.4.3. I also believe I have all of the proper Ubuntu installed. I'm not familiar enough with Ubuntu to know where to start debugging. Below is the code I'm using.

library(plumber)
library(sf)
library(tidyverse)

#* Return the distance to coast
#* @param lon is the longitude
#* @param lat is the latitude
#* @post /distance

function(lon,lat){
  coords <- data.frame(Longitude = as.numeric(lon), Latitude = as.numeric(lat)) %>% 
    st_as_sf(coords = c('Longitude','Latitude'))
  shape <- read_sf("us_medium_shoreline.shp")
  dist <- st_nearest_feature(coords,shape)/1609
}

oddly enough, if I create a different way of calculating it, it works just fine:

function(lon,lat){

  coords <- data.frame(Longitude = as.numeric(lon), Latitude = as.numeric(lat))

  data.sf <- coords %>%  st_as_sf(coords = c('Longitude','Latitude')) %>% st_set_crs(4326)
  
  shape <- st_read("us_medium_shoreline.shp")
  
  dist <- geosphere::dist2Line(p = st_coordinates(data.sf), 
                               line = 
                                st_coordinates(shape$geometry)[,1:2])
}

Actually, I think it may be just the st_nearest_feature. I'm using Ubuntu R 3.4.3 and I think st_nearest_feature requires 3.6.1. Can anyone verify this assumption is correct before I update R?

Edit: I updated to 3.6.1 in both Ubuntu 16.04 and my local system and still the API is not working. I do think it's because of the st_nearest_feature function. I also updated the verion of GEOS and it's now geos-3.7.2, but that didn't fix it either.

This was added here

It should be sf from 0.6.4 (see news: Changelog • sf)

sf should work from R 3.3 so not sure it is that. System requirements are

GDAL (>= 2.0.1), GEOS (>= 3.4.0), PROJ (>= 4.8.0)

Can you get the log for your API from the log pane inside RStudio Connect ?
You should have a R error somewhere in that log.

Thanks! I have GEOS 3.5.1, but the API error says 3.6.1 is required.

I built GEOS 3.7 by source, but for some reason it still detects 3.5.1. I'm still new to Ubuntu, so not sure how to fix that.

what is the api error ?

I built GEOS 3.7 by source, but for some reason it still detects 3.5.1. I'm still new to Ubuntu, so not sure how to fix that.

Only hint on that is sf readme for installation on ubuntu

or this setup page that has a lot of infos for spatial software
https://datacarpentry.org/geospatial-workshop/setup.html

I get the error "<Rcpp::exception in CPL_geos_nearest_feature(st_geometry(x), st_geometry(y)): GEOS version 3.6.1 required for selecting nearest features>"

Found how to install GEOS from source here. Turns out I didn't have the new GEOS version in my sources list: https://grasswiki.osgeo.org/wiki/Compile_and_Install_Ubuntu#GEOS

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.