mdragunski
mdragunski

Reputation: 31

R package Error in get("rgeos", envir = .MAPTOOLS_CACHE) : object 'rgeos' not found

I'm new to R and it's package system, just wrote my first package with the intent to use it with OpenCPU.

while executing this function:

#' Create a PNG which shows interpolated senseMap Data
#' 
#' @export
#' @import jsonlite
#' @import sp
#' @import gstat
#' @import rgeos
#' @import maptools
#' @param JSON

inteRidwTest <- function(input){
  ### JSON to data.frame ###
  oSeM_df <- input
  ### data.frame to spatialPointsDataFrame ###
  coordinates(oSeM_df) =~longitude+latitude
  ### adding CRS to the data ###
  proj4string(oSeM_df)="+proj=longlat +datum=WGS84"
  project_df=spTransform(oSeM_df, CRS("+proj=longlat +datum=WGS84")) 
  ### creating a bounding box ###
  bbox <- bbox(oSeM_df)
  ### creating a grid based on the bbox ###
  x.range <- as.numeric(c(floor(bbox[1]), ceiling(bbox[3]))) # min/max longitude of the interpolation area
  y.range <- as.numeric(c(floor(bbox[2]), ceiling(bbox[4])))# min/max latitude of the interpolation area  
  grd <- expand.grid(x = seq(from = x.range[1], to = x.range[2], by = 0.1), y = seq(from = y.range[1], to = y.range[2], by = 0.1))
  coordinates(grd) <- ~x + y
  gridded(grd) <- TRUE
  grdSp <- as(grd, "SpatialPolygons")
  ### adding CRS to grid ###
  proj4string(grdSp)="+proj=longlat +datum=WGS84"
  grd_df=spTransform(grdSp, CRS("+proj=longlat +datum=WGS84")) 
  ### setting up basegrid for the png ###
  grdSp.union <- unionSpatialPolygons(grd_df, rep("x", length(slot(grd_df,"polygons"))))
  llGRD <- GE_SpatialGrid(grdSp.union)
  llGRD_in <- over(llGRD$SG, grdSp.union)
  llSGDF <- SpatialGridDataFrame(grid = slot(llGRD$SG,"grid"), proj4string = CRS(proj4string(llGRD$SG)), data = data.frame(in0 = llGRD_in))
  llSPix <- as(llSGDF, "SpatialPixelsDataFrame")
  ### IDW ###
  llSPix$pred <- idw(value ~ 1, oSeM_df, llSPix)$var1.pred
  return(llSPix$pred)
#   ### create the png ###
#   png(file = "idw.png", width = llGRD$width,height = llGRD$height, bg = "transparent")
#   par(mar = c(0, 0, 0, 0), xaxs = "i", yaxs = "i")
#   image(llSPix, "pred", col = bpy.colors(20, alpha=0.7))
#   dev.off()
}

with this input: https://gist.githubusercontent.com/mdragunski/643a8557a4a0e37d30a1/raw/02d26c3ce83ee0c0c704b7c53984c640002c331f/oSeM-Data-processed.json

following Error is thrown:

OpenCPU error HTTP 400 object 'rgeos' not found

In call: get("rgeos", envir = .MAPTOOLS_CACHE)

the same Error is thrown while trying to use this package and function locally. If the rgeos package is installed and added before my inteRsense package it works as intended.

The Error seems to be connected to the rgeos and maptools packages which my package imports, I would like to change the order in which these packages are imported while using my package. But I cant figure out how to do this in the NAMESPACE file:

# Generated by roxygen2: do not edit by hand

export(helloWorld)
export(inteRidw)
export(inteRidwTest)
import(gstat)
import(jsonlite)
import(maptools)
import(rgeos)
import(sp)

So I guess my question here is: How to change the NAMESPACE file in an R package? But really Im grateful for any help.

Upvotes: 3

Views: 1471

Answers (1)

D-Lorenz
D-Lorenz

Reputation: 176

How to change the NAMESPACE file in an R package?

To change the NAMESPACE file in an R package, you have to:

  1. download the package source *.tar or *.zip from an available source (es. maptools package link)
  2. Edit the NAMESPACE file and add a line stating the package or function/package to import
  3. Edit the DESCRIPTION file and add the package to the 'depends' or 'imports' field
  4. Re-Build the package

The problem is that the change will be local and you have to distribute the modified package by yourself.

In this case maybe the best solution is to contact the maintainer of the package and see if it could fix the problem and update the package on the cran repository.

However I am experiencing the same issue. I get this error:

Error in get("rgeos", envir = .MAPTOOLS_CACHE) : object 'rgeos' not found

when the package I am developing calls the maptools function 'unionSpatialPolygons'. The package I am working on imports both the maptools and the rgeos packages but still fails. If the maptools package is manually loaded by the user everything works fine.

So another temporary solution is to load the maptools package after you load your package.

UPDATE

I edited the DESCRIPTION file of the package I am working on. I moved the 'maptools' package from the "imports" field to the "depends" field and the problem seems to be gone!

Upvotes: 2

Related Questions