一切都是可重复的,包括要绘制的数据
我正在绘制基本地图,但是服务器未读取数据。R:
它没有加载我用来绘制地图的geojson文件,我需要手动Ctrl +输入此行以使其起作用:
如果没有,R会说找不到对象秘鲁:
peru <- geojsonio::geojson_read("https://raw.githubusercontent.com/juaneladio/peru-geojson/master/peru_departamental_simple.geojson", what = "sp")
怎么了?
library(shiny)
library(dplyr)
library(DT)
require(leaflet)
library(readr)
ab <- reactive({
ventas_agosto_16 <- data.frame("FECHA_PED" = 1:25, "DEPARTAMENTO" = c("LIMA", "AREQUIPA", "LA LIBERTAD",
"MOQUEGUA","HUANUCO","CUSCO",
"PIURA","CALLAO","CAJAMARCA",
"UCAYALI","JUNIN","ICA",
"LAMBAYEQUE","AMAZONAS","ANCASH",
"AYACUCHO","LORETO","SAN MARTIN",
"TACNA","MADRE DE DIOS","PUNO",
"TUMBES","PASCO","APURIMAC",
"HUANCAVELICA"),
"TOTAL_PEDIDO" = runif(n = 25, min = 2, max = 25))
peru <- geojsonio::geojson_read("https://raw.githubusercontent.com/juaneladio/peru-geojson/master/peru_departamental_simple.geojson", what = "sp")
peru$ventas <- ventas_agosto_16$TOTAL_PEDIDO
peru
})
server <- function(input, output) {
output$mymap <- renderLeaflet({
pal <- colorBin("YlOrRd", domain = peru$ventas, bins = 5)
labels <- sprintf(
"<strong>%s</strong><br/>%g soles",
peru$NOMBDEP, peru$ventas
) %>% lapply(htmltools::HTML)
leaflet(peru) %>%
setView(-75, -10, 4) %>%
addProviderTiles("MapBox", options = providerTileOptions(
id = "mapbox.light",
accessToken = Sys.getenv('MAPBOX_ACCESS_TOKEN'))) %>%
addPolygons(
fillColor = ~pal(ventas),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.7,
highlight = highlightOptions(
weight = 3,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")) %>%
addLegend(pal = pal, values = ~ventas, opacity = 0.7, title = NULL,
position = "bottomright")
})
}
谢谢!
答案 0 :(得分:1)
您快到了,只需将reactive
移到服务器函数中,然后将“调用” ab()
重新分配到peru
中的renderLeaflet
,即可顺利执行其余代码
ui <- fluidPage(
leafletOutput(outputId='mymap', width = "100%", height = 400)
)
server <- function(input, output, session) { #session is an important argument for server function
ab <- reactive({ ....
})
output$mymap <- renderLeaflet({
#browser()
peru <- ab()
....
}
shinyApp(ui, server)