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])
}