Can't plot a R interactive map comtainning my shapefile and my raster data in power BI

It need not be a rmd file: consider this example (drawing heavily on my earlier answer to a related question).

The last line is what matters, the rest is just a piece of reproducible leaflet

Once you have the html file saved you can embed the code into Power BI (there should be a ton of walkthroughs for that around...)

library(tidyverse)
library(leaflet)
library(tmap) # for the Metro dataset
library(sf)

data("metro") # from tmap

metro <- metro %>% 
  select(pop = pop2020) %>% 
  mutate(pop = pop / max(pop)) # normalise the values

mapa <- st_read("./data/maps/ne_50m_admin_0_countries.shp") # replace with your path :)

pal <- colorNumeric(palette = "Reds",  domain = mapa$GDP_MD_EST)

asdf <- 
  leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  setView(lng = 14.46, lat = 50.07, zoom = 1) %>%
  addPolygons(data = mapa, 
              fillColor = ~pal(mapa$GDP_MD_EST),
              fillOpacity = 0.75,
              stroke = F) %>% 
  addCircleMarkers(data = metro,
                   radius = 20 * metro$pop,
                   stroke = F)

htmlwidgets::saveWidget(asdf, "asdf.html") # put something more specific than asdf here :)
2 Likes