[英]Long/Lat data points not found though they are provided for interactive map in Shiny
[英]How to create R shiny interactive map without lat and long in the dataset
using leaflet package to create interactive map using r shiny. 我的数据集提供了国家的名称,但没有提供经纬度。 我如何在 map 可以通过国家名称检测的地方创建这个
您可以使用 geojson map 和readOGR function,您可以在 Internet 上找到 map 并将您的数据集链接到它。
rgdal::readOGR(dsn = "GeoJSON map",
layer = "OGRGeoJSON",
stringsAsFactors = FALSE)
以下是基于此stackoverflow 帖子的示例,用于在 R Shiny 上制作交互式 map。 所选国家/地区位于click.list$ids中,请参见下面的output$selected_var 。
您的数据
## packages ##
packages <- c("leaflet", "shiny", "shinydashboard")
newPackages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(newPackages)) install.packages(newPackages)
lapply(packages, library, character.only = TRUE)
remove(packages, newPackages)
## map & data ##
Europe <- rgdal::readOGR(dsn = "https://data.opendatasoft.com/explore/dataset/european-union-countries@public/download/?format=geojson&timezone=Europe/Berlin",
layer = "OGRGeoJSON",
stringsAsFactors = FALSE)
data <- data.frame("name" = c("Austria", "Belgium", "Bulgaria", "Croatia", "Cyprus", "Czechia", "Denmark", "Estonia",
"Finland", "France", "Germany", "Greece", "Hungary", "Iceland", "Ireland", "Italy", "Latvia",
"Liechtenstein", "Lithuania", "Luxembourg", "Malta", "Netherlands", "Norway", "Poland", "Portugal",
"Romania", "Slovakia", "Slovenia", "Spain", "Sweden", "Switzerland", "Turkey", "United Kingdom"),
"polcapita" = c(0.0087928207, 0.0100464969, 0.0075375536, 0.0049040898, 0.0097860082, 0.0119440135, 0.0087740252,
0.0080851042, 0.0063462331, 0.0064707328, 0.0107846429, 0.0085740997, 0.0059612600, 0.0409884683,
0.0138830047, 0.0067616652, 0.0049423915, 0.0053782730, 0.0053461017, 0.0165884166, 0.0046052235,
0.0116079951, 0.0052533159, 0.0100049243, 0.0075509189, 0.0047028415, 0.0067531703, 0.0077087169,
0.0064795469, 0.0008881585, 0.0053907055, 0.0053085690, 0.0069728195))
# example if you need to change the name of a country
Europe@data[Europe@data$name == "Czech Rep.", ]$name <- "Czechia"
# example if you want to add datas to the map
Europe@data$polcapita <- merge(x = Europe@data, y = data, sort = FALSE)$polcapita
pal <- colorNumeric(c("Green", "Red"), Europe$polcapita)
界面部分
## create the UI ##
ui <- fluidPage(
# place the contents inside a box
shinydashboard::box(
width = 12,
title = "Click on the map!",
# separate the box by a column
column(width = 2,
shiny::actionButton(inputId = "clearHighlight",
icon = icon( name = "eraser"),
label = "Clear the Map",
style = "color: #fff; background-color: #D75453; border-color: #C73232")),
# separate the box by a column
column(width = 10,
leaflet::leafletOutput(outputId = "myMap", height = 850)),
column(width = 5,
textOutput("selected_var"))
)
)
服务器部分
## create the server ##
server <- function( input, output, session ){
# create foundational map
foundational.map <- shiny::reactive({
leaflet(Europe) %>%
fitBounds(-20, 65, 20, 39) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addPolygons(data = Europe,
layerId = Europe$name,
color = ~pal(polcapita),
group = "click.list",
weight = 2,
fillOpacity = 0.3,
opacity = 1,
smoothFactor = 0.2,
stroke = FALSE)
})
output$myMap <- renderLeaflet({
foundational.map()
})
# store the list of clicked polygons in a vector
click.list <- shiny::reactiveValues(ids = vector())
# add countries to selection
shiny::observeEvent(input$myMap_shape_click, {
click <- input$myMap_shape_click
if(click$id %in% click.list$ids){
click.list$ids <- click.list$ids[!click.list$ids%in%click$id]
leaflet::leafletProxy(mapId = "myMap") %>%
clearGroup("lin")
} else{
click.list$ids <- c(click.list$ids, click$id)
}
lines.of.interest <- Europe[ which(Europe$name %in% click.list$ids), ]
leaflet::leafletProxy(mapId = "myMap") %>%
addPolylines(data = lines.of.interest,
layerId = lines.of.interest$ids,
color = "#6cb5bc",
weight = 2,
opacity = 1,
group = "lin")
})
# Clear the map button
shiny::observeEvent( input$clearHighlight, {
output$myMap <- leaflet::renderLeaflet({
click.list$ids <- NULL
foundational.map()
})
})
# selected countries
output$selected_var <- renderText({
paste("You have selected", click.list$ids)
})
}
运行服务器
## run shinyApp ##
shiny::shinyApp(ui, server)
我希望它能回答你的问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.