In R it works perfectly and returns an interactive map containing my raster data plus myshapefile.
Anyone knows what could be happening?
Or, anyone could suggest another way to plot raster + shapefile interactive maps in power BI using R?
Thanks very much, best regards.
I believe that {mapview} is not compatible with Power BI.
Are you absolutely positively determined on using Power BI? Your piece of code looks rather static, and could possibly be rendered as html via leaflet.
What I have done in the past was to create a TopoJSON (not very difficult in {sf} workflow) out of the shapefile and then used it to create a choropleth map. There is a walkthrough to it on the Power BI pages.
It might be something along these lines (note that your example is not quite reproducible, so I can not make sure).
yer_shapefile <- sf::st_read("yer_shapefile.shp") # you will want to be more specific :)
yer_raster <- raster:.raster("your_raster.tif") %>% # original raster
sf:.st_as_sf() # grid - without values - as a sf object
geojsonio::topojson_write(yer_r_object, # the sf object - first raster, then shapefile
file = "whatever.topojson", # or what not...
object_name = "whatever") # or what not...
In Power BI you would need to re-link the data to empty scaffolding created from the topojson.
This should work, but it is kinda hassle, IMHO.
An alternative would be generating the plot in R via {leaflet} and "plotting" it from Power BI as a chunk of HTML.
Hi, thanks for your attention! Well, a choropleth map won't work for what I need to demonstrate...
About the HTML chunk, I think it could work..
Should I generate a Rmardown file and input it in power BI ?
Again, thanks very much!
Hello! Thanks very much for your script..
I'm quite beginner in R and I don't know exactly where should I put my raster data in this script...
should I add another object using the raster library?
Try this code; as I don't have access to your filesystem I am coding blind. So bear with me if there is a missing pipe or the result needs some tweaking to make colors right or what not...
library(tidyverse)
library(htmlwidgets)
library(raster)
library(sf)
# these are your files from code above, so I assume them to work...
pivos <- st_read("california/SJER/vector_data/aneis_anapaula_0904.shp")
ndwi <- raster("california/SJER/vector_data/2017_06_04_NDWI_CLIP.tif")
chrt <- leaflet() %>%
addProviderTiles(providers$CartoDB.Positron) %>% # a basemap; not required but nice...
addPolygons(data = pivos,
fillOpacity = 0,
stroke = T) %>%
addRasterImage(ndwi)
saveWidget(chrt, "chart.html")
Hi, thanks again and again... however, only the basemap is plotted... The raster and shp don't load =(
I received those waring messages only:
Warning messages:
1: sf layer is not long-lat data
2: sf layer has inconsistent datum (+proj=utm +zone=23 +south +ellps=GRS80 +units=m +no_defs).
Need '+proj=longlat +datum=WGS84'
Hi @jlacko ! Now the HTML is perfectly working!
I'm not able to display it in power BI yet, but I will find a way (if you have any, please suggest, haha) .
Anyway, you solved my firts problem, thank you!