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!