如何从用户编写的函数正确返回SpatialPolygonDataFrame?

时间:2019-06-05 07:30:07

标签: r maps

我正在尝试编写一个函数,该函数导入形状文件,打印myshapefile@data的列名,然后将shapefile存储为命名对象。但是,return()不会返回名称对象,而是在控制台中打印shapefile数据,而不是将其另存为环境中的对象。

这是草稿函数,其中shp.path是一个字符串,其中包含shapefile的文件路径:

get.map <- function(shp.path) {

  country.map <- rgdal::readOGR(shp.path)
  message("You will need to know which column has state names:")
  names(country.map@data)
  return(country.map)
}

return()内可能需要一个函数来定义对象类,但是添加return(SpatialPolygonsDataFrame(country.map))会返回错误:

Error in nrow(data) : argument "data" is missing, with no default 

有关使用什么的任何建议?完全省略return()意味着该对象仅在本地定义,不能在下一个函数中调用。

0 个答案:

没有答案