0

I have created a function which run well on my local environment, but fail on the server.

function(dataset1,nom_col_lon1,nom_col_lat1,dataset2,nom_col_lon2,nom_col_lat2,nom_col_ID2){

require(geosphere)

mat.dist<- distm(dataset1[,c(nom_col_lon1,nom_col_lat1)], dataset2[,c(nom_col_lon2,nom_col_lat2)], fun=distHaversine)

lon=match(nom_col_lon2,colnames(dataset2))
dataset1$compLon=dataset2[,lon][apply(mat.dist,1,which.min)]
rm(lon)

lat=match(nom_col_lat2,colnames(dataset2))
dataset1$compLat=dataset2[,lat][apply(mat.dist,1,which.min)]
rm(lat)

ID=match(nom_col_ID2,colnames(dataset2))
dataset1$compID=dataset2[,ID][apply(mat.dist,1,which.min)]

dataset1$dist_min=apply(mat.dist,1,min)
rm(mat.dist)

return(dataset1)}

It works well on my local session, but when I call it on the server, I got this error:

ARCEP_SFR_2G=min_dist(ARCEP_SFR_2G,"lon","lat",ANFR_SFR_2G,"lon","lat","ID")
Error in .pointsToMatrix(p1) * toRad : 
  non-numeric argument to binary operator
In addition: Warning messages:
1: In .pointsToMatrix(x) : NAs introduced by coercion
2: In .pointsToMatrix(y) : NAs introduced by coercion

In the two dataset that I use, nom_col_lon and nom_col_lat are numeric. There is no NA. When I check the environment for min_dist, it's "global environment". Any idea of what could be the problem?

EDIT:

When I run

mat.dist<- distm(ARCEP_SFR_2G[,c("lon","lat")], ANFR_SFR_2G[,c("lon","lat")], fun=distHaversine)

it works well. The problem happens only when I call the function.

EDIT2:

Exemple of dataset:

Technologie=c("2G","3G")
Operateur=c("Ope1","Ope2")
lat=c(46.2,46.1)
lon=c(5.34,5.51)
ID=c("ID1","ID2")

x=as.data.table(cbind(Technologie,Operateur,lat,lon,ID))
x$lat=as.numeric(x$lat)
x$lon=as.numeric(x$lon)

The server is a Linux server. My local session is under windows.

Community
  • 1
  • 1
Oolis
  • 146
  • 2
  • 2
  • 13
  • 1
    What kind of "server" are you talking about? How are you loading data? Please try to provide some sort of [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Jun 20 '17 at 14:27
  • Also how do you call it? – amonk Jun 20 '17 at 14:41
  • I simply call it this way: `ARCEP_SFR_2G=min_dist(ARCEP_SFR_2G,"lon","lat",ANFR_SFR_2G,"lon","lat","ID")`. The code for the function is in the same script, and runned before calling it. – Oolis Jun 20 '17 at 14:49

0 Answers0