使用 Spatstat 从数据帧渲染点图案在 R/Shiny 中执行时间太长
我正在构建一个闪亮的应用程序来使用 spatstat 包执行空间分析。我没有任何令人兴奋的代码问题。但是渲染地图、点和轮廓需要相当长的时间才能加载到地图上。该数据框有近 87,000 个数据点需要渲染。
我加载 shapefile 并创建 ppp 数据格式的代码如下:
library(sf)
library(spatstat)
library(sp)
library(rgdal)
library(maptools)
library(ggmap)
library(RColorBrewer)
s.sf <- readShapeSpatial("india.shp")
s.owin <- as(s.sf, "owin")
breakpoints <- c(-50,-40,-30,-20,-10,0,100,200,300,400,500,600,800,1000,1200)
colors<- c("#08519c","#3182bd","#9ecae1","#c6dbef","#f7f7f7","#fff5f0","#fee0d2","#fcbba1","#fc9272","#fb6a4a","#ef3b2c","#cb181d","#a50f15","#67000d")
# The data here has 87,000 rows post filtering
df <- read.csv("data_illness.csv")
df <- df[df$condition == 1,]
# taking look long to create ppp data format
data.ppp <- ppp(x = df$Longitude, y = df$Latitude, window = s.owin, marks = df$condition)
plot(density(data.ppp, sigma = 1), col=colors, breaks = breakpoints, pch=".")
contour(density(data.ppp, 1), axes = F, add=TRUE, pch=".")
plot(s.owin, add=TRUE, las=1, pch=".")
我需要一种替代/有效的方式来运行代码,以便在闪亮的应用程序上快速加载地图。
我被这个问题困住了,真的需要帮助!
I am building a shiny app to perform spatial analysis using the spatstat package. I do not have any issue exciting the code. But rendering the map, points and contours are taking quite a long time to load on the map. The data frame has almost 87,000 data points to render.
My code to load the shapefile and create ppp data format is as below:
library(sf)
library(spatstat)
library(sp)
library(rgdal)
library(maptools)
library(ggmap)
library(RColorBrewer)
s.sf <- readShapeSpatial("india.shp")
s.owin <- as(s.sf, "owin")
breakpoints <- c(-50,-40,-30,-20,-10,0,100,200,300,400,500,600,800,1000,1200)
colors<- c("#08519c","#3182bd","#9ecae1","#c6dbef","#f7f7f7","#fff5f0","#fee0d2","#fcbba1","#fc9272","#fb6a4a","#ef3b2c","#cb181d","#a50f15","#67000d")
# The data here has 87,000 rows post filtering
df <- read.csv("data_illness.csv")
df <- df[df$condition == 1,]
# taking look long to create ppp data format
data.ppp <- ppp(x = df$Longitude, y = df$Latitude, window = s.owin, marks = df$condition)
plot(density(data.ppp, sigma = 1), col=colors, breaks = breakpoints, pch=".")
contour(density(data.ppp, 1), axes = F, add=TRUE, pch=".")
plot(s.owin, add=TRUE, las=1, pch=".")
I need an alternative/efficient way to run the code so the loading of the map is quick on the shiny app.
I am stuck with this and really need help here!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您自己的数据可能位于地理坐标(经度,纬度)中,
这可能会给你的结果带来一些失真。在 spatstat 中一切
假设是平面的,所以你应该真正投影你的多边形和点
在转换为 spatstat 格式之前。
由于我们无法访问您的数据,我在下面制作了一些虚假数据以供参考
一些改进建议。
现在,
X1
是一个在多边形窗口中包含 87000 个点的ppp
。命令密度.ppp
通过核密度估计来估计点的强度。您应该只执行此命令一次并保存结果以进行绘图:
如果转换为
ppp
如果需要更快,您可以添加check = FALSE
你已经知道所有点都在观察窗口内,所以
代码不需要检查所有 87000 点。比较这些:
我在代码中没有看到绘制实际点的任何地方。可以
您使示例可重现并显示结果并清楚地解释
哪一部分你需要更快。
It appears that your own data may be in geographic coordinates (lon,lat),
which may give a bit of distortion to your results. In spatstat everything
is assumed planar, so you should really project your polygon and points
before converting to spatstat format.
Since we don’t have access to your data I make some fake data below to give
a few suggestions for improvements.
Now
X1
is appp
with 87000 points in a polygonal window. The commanddensity.ppp
estimates the intensity of points by kernel density estimation.You should only excecute this command once and save the results for plotting:
If the conversion to
ppp
needs to be faster you can addcheck = FALSE
ifyou already know that all points are inside the window of observation, so the
code doesn’t need to check this for all 87000 points. Compare these:
I don’t see anywhere in your code where you plot the actual points. Could
you make your example reproducible and show the results and explain clearly
which part you need to be faster.
尝试使用
data.table::fread("data_illness.csv")
而不是read.csv("data_illness.csv")
,因为读取较大的文件通常速度更快。另外,预先过滤您的 csv,使其仅包含您使用的列(例如条件、经度、纬度)
您需要加载所有 87,000 个数据点吗?你能先总结一下它们吗,例如,对特定的坐标平方求平均?
Try
data.table::fread("data_illness.csv")
instead ofread.csv("data_illness.csv")
as it is often faster to read larger files.Also, pre-filter your csv to only contain the columns you use (e.g. condition, Longitude, Latitude)
Do you need to load all 87,000 data points? Can you summarise them first, for example, averaging specific coordinate squares?