Reputation: 217
I'm trying to use my prepared Large SpatialPolygonsDataFrame in my Shiny Leaflet App. What I don't understand is how to use my Large SpatialPolygonsDataFrame in my Server.R. When I run the load_data.R script and after that the server.R script, then the Shiny App works perfectly. My question to you is:
How can I get the Shiny App working without running seperately load_data.R, but just with running the server.r?
load_data.R:
library(RSQLite)
library(rgdal)
library(dplyr)
# Use the SQLite database
my_sqdb = src_sqlite("NL_Household_Penetration/Data/dataset.sqlite")
# Extract the main dataset out of the SQLite database
df = data.frame(tbl(my_sqdb, sql("SELECT * FROM df")))
# Extract the stores with their locations out of the SQLite database
Winkels = data.frame(tbl(my_sqdb, sql("SELECT * FROM Winkels")))
# Read the shape-data(polygons) into R
shape <-readOGR("NL_Household_Penetration/PMA_Shape/Polygonen NL Postcodes 4PP.kml", "Polygonen NL Postcodes 4PP")
# Combine the main dataset with the shape data to plot data into zipcode areas
SalesMap <- merge(shape, df, by.x='Description', by.y='POSTCODE')
server.R:
#shiny
library(shiny)
library(shinydashboard)
#define color
library(RColorBrewer)
library(colorspace)
# leaflet map
library(leaflet)
library(htmlwidgets)
library(htmltools)
## Creating leaflet map
pal <- colorNumeric("Reds",SalesMap@data$SALES)
polygon_popup <- paste0("<strong>Postcode: </strong>", SalesMap$Description, "<br>",
"<strong>Waarden: </strong>", SalesMap$SALES)
pop = as.character(Winkels$WINKEL)
Icon <- makeIcon(
iconUrl = "NL_Household_Penetration/Images/image.png",
iconWidth = 100, iconHeight = 78
)
server <- function(input, output, session) {
output$mymap <- renderLeaflet({
leaflet() %>%
addTiles(
urlTemplate = "//{s}.tiles.mapbox.com/v3/jcheng.map-5ebohr46/{z}/{x}/{y}.png",
attribution = 'Maps by <a href="http://www.mapbox.com/">Mapbox</a>'
) %>%
addPolygons(data = SalesMap,
fillColor = ~pal(SalesMap@data$SALES),
fillOpacity = 0.6, ## how transparent do you want the polygon to be?
popup = polygon_popup,
color = "black", ## color of borders between districts
weight = 2.0) %>%
addMarkers(Winkels$Lon, Winkels$Lat, popup=pop, icon=IKEAIcon)
})
}
ui.R
library(shiny)
library(shinydashboard)
library(leaflet)
ui <- bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
leafletOutput("mymap", width = "100%", height = "100%")
)
Thanks in advance!
Yoorizz
Upvotes: 2
Views: 636
Reputation: 12819
When the Shiny app is launched, it looks only for server.R
and ui.R
(see ?shiny::runApp
). load_data.R
is thus not sourced.
Try adding source("load_data.R")
to server.R
.
Upvotes: 0