如何在数据集中创建没有lat和long的R闪亮交互式地图
使用传单包创建交互式地图使用r。我的数据集提供国家的名称,但不提供lat或long。如何创建地图可以通过国家名称进行检测的位置如何在数据集中创建没有lat和long的R闪亮交互式地图,r,shiny,leaflet,R,Shiny,Leaflet,使用传单包创建交互式地图使用r。我的数据集提供国家的名称,但不提供lat或long。如何创建地图可以通过国家名称进行检测的位置 您可以使用带有readOGR函数的geojson映射,您可以在internet上找到映射并将数据集链接到它 rgdal::readOGR(dsn = "GeoJSON map", layer = "OGRGeoJSON", stringsAsFactors = FALSE) 下面是
您可以使用带有readOGR函数的geojson映射,您可以在internet上找到映射并将数据集链接到它
rgdal::readOGR(dsn = "GeoJSON map",
layer = "OGRGeoJSON",
stringsAsFactors = FALSE)
下面是一个基于此在R上创建交互式地图的示例。所选国家/地区位于单击状态。列出$ID,请参阅下面的输出$selected\u 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)