在 R Shiny 中显示欧洲地图

Display Europe map in RShiny

我正在创建地图并想 plot/display 在 RShiny 中使用此地图。地图创建者:

library(ggplot2)
library(grid)
library(rworldmap)

## Get the world map: ##
worldMap <- getMap()

## Define vector with all European countries: ##
v.europe <- c("Norway", "Sweden", "Finland", "Denmark", "United Kingdom","Ireland", "Greece",
              "Belgium", "Netherlands", "France", "Spain", "Portugal", "Luxembourg", "Croatia",
              "Germany", "Switzerland", "Austria", "Slovenia", "Italy", "Bulgaria", "Romania",
              "Czech Rep.", "Slovakia", "Hungary", "Poland", "Bosnia Hercegovina", "Serbia",
              "Turkey", "Ukraine", "Moldova", "Belarus", "Estonia", "Latvia", "Lithuania",
              "Montenegro", "Albania", "Macedonia")

## Select only the index of countries of Europe: ##
indEU <- which(worldMap$NAME%in%v.europe)


## Extract longitude and latitude border's coordinates of countries: ##
df.europeCoords <- lapply(indEU, function(i){
  df <- data.frame(worldMap@polygons[[i]]@Polygons[[1]]@coords)
  df$region = as.character(worldMap$NAME[i])
  colnames(df) <- list("long", "lat", "region")
  return(df)
})
df.europeCoords <- do.call("rbind", df.europeCoords)
names(df.europeCoords) <- c("longitude", "latitude", "country")

## Deletes/Removes borders of PLOT: ##
ax <- list(
  title = "",
  zeroline = FALSE,
  showline = FALSE,
  showticklabels = FALSE,
  showgrid = FALSE
)

## Plot the map: ##
p <- ggplot(data = df.europeCoords, aes(x = longitude, y = latitude, group = country, 
                                        text = paste("<b>", country, '</b>\n')), 
            color = "grey50", size = 0.1) + 
     geom_polygon() +
     coord_map(xlim = c(-13, 35),  ylim = c(32, 71)) +
     theme_classic() +
     theme(axis.text.x = element_blank(), axis.text.y = element_blank(), axis.ticks.x = element_blank(),
           axis.ticks.y = element_blank(), axis.title = element_blank(), legend.position = "none",
           plot.margin = unit(0 * c(-1.5, -1.5, -1.5, -1.5), "lines"))

## Create plot_ly() object: ##
EuropePlot <- plotly::ggplotly(p, tooltip = "text") %>%
              layout(xaxis = ax, yaxis = ax)

地图在 RStudio 的查看器中正确显示,但如果我想在 RShiny 中显示它,我会收到以下错误消息:

Warning: Error in UseMethod: not applicable method for 'plotly_build' applied to object of class "shiny.tag"

服务器端的地图调用如下所示:

# PLOT-Output: #
    output$maPPInfoPLOT <- renderPlotly({
          plotly::ggplotly(EuropePlot, dynamicTicks = TRUE)
    })

备注:

该图是在函数外部创建的,并由 return(EuropePlot) 提供。在 server.R 中,我使用 output$maPPInfoPLOT <- renderPlotly({...}),其中 UI.R 如下所示:

plotlyOutput (outputId = "maPPInfoPLOT", width = "900px", height = "600px")

以下对我有用:

library(shiny)
library(ggplot2)
library(grid)
library(rworldmap)
library(plotly)
library(mapproj)

getEuropePlot <- function(){

  ## Get the world map: ##
  worldMap <- getMap()
  
  ## Define vector with all European countries: ##
  v.europe <- c("Norway", "Sweden", "Finland", "Denmark", "United Kingdom","Ireland", "Greece",
                "Belgium", "Netherlands", "France", "Spain", "Portugal", "Luxembourg", "Croatia",
                "Germany", "Switzerland", "Austria", "Slovenia", "Italy", "Bulgaria", "Romania",
                "Czech Rep.", "Slovakia", "Hungary", "Poland", "Bosnia Hercegovina", "Serbia",
                "Turkey", "Ukraine", "Moldova", "Belarus", "Estonia", "Latvia", "Lithuania",
                "Montenegro", "Albania", "Macedonia")
  
  ## Select only the index of countries of Europe: ##
  indEU <- which(worldMap$NAME%in%v.europe)
  
  
  ## Extract longitude and latitude border's coordinates of countries: ##
  df.europeCoords <- lapply(indEU, function(i){
    df <- data.frame(worldMap@polygons[[i]]@Polygons[[1]]@coords)
    df$region = as.character(worldMap$NAME[i])
    colnames(df) <- list("long", "lat", "region")
    return(df)
  })
  df.europeCoords <- do.call("rbind", df.europeCoords)
  names(df.europeCoords) <- c("longitude", "latitude", "country")
  
  ## Deletes/Removes borders of PLOT: ##
  ax <- list(
    title = "",
    zeroline = FALSE,
    showline = FALSE,
    showticklabels = FALSE,
    showgrid = FALSE
  )
  
  ## Plot the map: ##
  p <- ggplot(data = df.europeCoords, aes(x = longitude, y = latitude, group = country, 
                                          text = paste("<b>", country, '</b>\n')), 
              color = "grey50", size = 0.1) + 
    geom_polygon() +
    coord_map(xlim = c(-13, 35),  ylim = c(32, 71)) +
    theme_classic() +
    theme(axis.text.x = element_blank(), axis.text.y = element_blank(), axis.ticks.x = element_blank(),
          axis.ticks.y = element_blank(), axis.title = element_blank(), legend.position = "none",
          plot.margin = unit(0 * c(-1.5, -1.5, -1.5, -1.5), "lines"))
  
  ## Create plot_ly() object: ##
  EuropePlot <- plotly::ggplotly(p, tooltip = "text") %>%
    layout(xaxis = ax, yaxis = ax)
}

ui <- fluidPage(
  plotlyOutput (outputId = "maPPInfoPLOT", width = "900px", height = "600px")
)

server <- function(input, output, session) {
  # PLOT-Output: #
  EuropePlot <- getEuropePlot()
  output$maPPInfoPLOT <- renderPlotly({
    plotly::ggplotly(EuropePlot, dynamicTicks = TRUE)
  })
}

shinyApp(ui, server)