我正在尝试在我的Shiny Leaflet App中使用我准备好的Large SpatialPolygonsDataFrame。我不明白的是如何在我的Server.R中使用我的Large SpatialPolygonsDataFrame。当我运行load_data.R脚本,然后运行server.R脚本,然后Shiny App工作正常。我的问题是:
如何在不单独运行load_data.R的情况下让Shiny App正常运行,但只需运行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%")
)
提前致谢!
Yoorizz
答案 0 :(得分:0)
启动Shiny应用后,它只会显示server.R
和ui.R
(请参阅?shiny::runApp
)。因此load_data.R
不是来源。
尝试将source("load_data.R")
添加到server.R
。