1

I am creating an API in R with Plumber which takes in typed coordinates, then using the function below to converts the coordinates to a spatial point for use in the API

def_coord <- function(x, y){
  xy <- data.frame(x, y)
  ll_prj <- "+proj=longlat +datum=WGS84" # set projection
  SpatialPoints(xy, proj4string=CRS(ll_prj))
  return(xy)
}

Here is the parameter argument in the api

#* @param y Latitude in decimal degrees.
#* @param x Longitude in decimal degrees.

On execution, the API returns the error below:

 <simpleError in .local(obj, ...): cannot derive coordinates from non-numeric matrix>

What could be the problem?

  • Can you give a complete minimal example of all the code you have and how you are running the API service. – Spacedman Feb 5 at 14:16
1

You need to convert x and y from characters to numeric:

#* @param y Latitude in decimal degrees.
#* @param x Longitude in decimal degrees.
#* @get /coord
def_coord <- function(x, y){
    library(sp)
    x = as.numeric(x)  # this
    y = as.numeric(y)  # and this
    xy <- data.frame(x, y)
    ll_prj <- "+proj=longlat +datum=WGS84" # set projection
    SpatialPoints(xy, proj4string=CRS(ll_prj))
    return(xy)
}

with that in ll.R, the following works:

> r = plumb("./ll.R")
> r$run()
Starting server to listen on port 3317
Running the swagger UI at http://127.0.0.1:3317/__swagger__/

and accessing the server that URL lets me explore the UI and run an example which returns a JSON version of the data frame.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.