自行编写的软件包:spatialPolygonsDataFrame的载荷图方法 [英] Self-authored package: load plot method for spatialPolygonsDataFrame
问题描述
我正在编写自己的R包,并希望绘制一个spatialPolygonsDataFrame
对象.如果我将其编写为脚本,则只需使用library()
加载必要的程序包(maptools
,rgdal
和rgeos
),并使用plot(x)
进行绘图.
I'm writing my own R package and would like to plot a spatialPolygonsDataFrame
object. If I were writing it as a script I would simply load the necessary packages (maptools
, rgdal
, and rgeos
) with library()
and plot with plot(x)
.
在编写使用library()
构建的软件包时,不建议使用 ,通常是通过将软件包添加到NAMESPACE
中的Imports:
来加载软件包.如果执行此操作,则会收到以下错误:
When writing a package to build using library()
is not advised, instead it is usual to load the package by adding it to Imports:
in the NAMESPACE
. If I do this I receive the following error:
as.double(y)中的错误: 无法将类型"S4"强制转换为类型"double"的向量
Error in as.double(y) : cannot coerce type 'S4' to vector of type 'double'
这是在编写脚本时通过将maptools
程序包与library()
一起加载进行了更正.
This is is corrected by loading the maptools
package with library()
if writing a script.
我知道您可以在NAMESPACE
中的ImportMethodsFrom
中加载单个方法,因此尝试使用此方法从maptools
导入绘图方法,但是没有运气.当我查看maptools
包的NAMESPACE
时,找不到导出的plot方法.我已经看到有一个plot.Spatial
函数,我试图将其导入到我的NAMESPACE
中却没有成功:
I know you can load individual methods with ImportMethodsFrom
in the NAMESPACE
so have tried to import a plot method from maptools
using this approach but have had no luck. When I looked in the NAMESPACE
of the maptools
package I couldn't find a plot method exported. I've seen there is a plot.Spatial
function which I have tried to import to my NAMESPACE
without success:
在"maptools"中找不到请求的方法:plot.Spatial
No methods found in "maptools" for requests: plot.Spatial
最后,我尝试在我的NAMESPACE
中将maptools
添加到Depends:
而不是Imports:
,这确实可行.这是做到这一点的规范方法吗?为一种方法附加整个程序包似乎有些矫kill过正(另外,我不知道已屏蔽了哪些功能,等等).加载必要的工具以在自定义函数中绘制地图的最佳方法是什么?
Finally, I have tried adding maptools
to Depends:
instead of Imports:
in my NAMESPACE
and this does work. Is this the canonical way to do this? It seems overkill to attach a whole package for one method (plus I don't know what functions have been masked, etc.). What is the best way to load the necessary tools to plot maps within a self-authored function?
针对@ Hack-R的问题,我不知道是否plot.Spatial是我唯一需要的方法,即使它是正确的方法.据我所知,这将使我能够绘制spatial
对象.
Edit 1: In response to @Hack-R's question, I don't know if plot.Spatial is the only method I need, or even if it's the correct one. It's my educated guess that this will enable me to plot spatial
objects.
推荐答案
@mdsumner的回答为我指明了正确的方向,并且就其本身而言是有益的讨论.
@mdsumner's answer pointed me in the right direction and was a useful discussion in its own right.
我对绘制spatialPolygonsDataFrame
对象的特定查询的答案是将sp
添加到Imports:
并调用sp::plot()
The answer to my specific query to plot spatialPolygonsDataFrame
objects was to add sp
to Imports:
and call sp::plot()
这篇关于自行编写的软件包:spatialPolygonsDataFrame的载荷图方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!