Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

plotly scattermapbox R visual cannot plot map on Power BI online Service

I built a R customised visual using the plotly library and "scattermapbox" type. this visual works properly in Power BI desktop. However, when I publish it online, the map could not be plotted.  

source code in script.r:

source('./r_files/flatten_HTML.r')

############### Library Declarations ###############
libraryRequireInstall("dplyr");
libraryRequireInstall("plotly");

####################################################

################### Actual code ####################
p <- plot_ly(
type = 'scattermapbox',
lat = 48.05176,
lon = -122.17708,
mode="markers",
marker=list(size = 10, color = "red")
) %>%
layout(
mapbox = list(
style="carto-positron",
center= list(lon=-122.17708,lat = 48.05176)))
###################################################

############# Create and save widget ###############

internalSaveWidget(config(p, displayModeBar = FALSE), 'out.html')
####################################################

screenshot in desktop:

image.png

 

screenshot in Power BI online service:

image.png

Status: New
Comments
v-qiuyu-msft
Community Support

Hi @Anonymous, 

 

Could you please provide the file used by source('./r_files/flatten_HTML.r')?

It would be better if you could provide pbix file for us. Please remove sensitive data in the report, upload pbix file to your OneDrive for Business then paste the share link here.

 

Best Regards,
Qiuyun Yu

Anonymous
Not applicable

Hi Qiuyun

please refer to the code of flatten_HTML.r below:

It seems that our organisation do not allow me to share external users via onedrive business. any other approaches to share this pbi

 
############### Utility functions ###############
libraryRequireInstall = function(packageName, ...)
{
if(!require(packageName, character.only = TRUE)) 
warning(paste("*** The package: '", packageName, "' was not installed ***", sep=""))
}
libraryRequireInstall("XML")
libraryRequireInstall("htmlwidgets")
internalSaveWidget <- function(widget, fname)
{
tempFname = paste(fname, ".tmp", sep="")
htmlwidgets::saveWidget(widget, file = tempFname, selfcontained = FALSE)
FlattenHTML(tempFname, fname)
}
FlattenHTML <- function(fnameIn, fnameOut)
{
# Read and parse HTML file
# Embed all js and css files into one unified file

if(!file.exists(fnameIn))
return(FALSE)

dir = dirname(fnameIn)
html = htmlTreeParse(fnameIn, useInternal = TRUE)
top = xmlRoot(html)

# extract all <script> tags with src value
srcNode=getNodeSet(top, '//script[@src]')
for (node in srcNode)
{
b = xmlAttrs(node)
fname = file.path(dir, b['src'])
alternatesrc=FindSrcReplacement(fname)
if (!is.null(alternateSrc))
{
s = alternateSrc
names(s) = 'src'
newNode = xmlNode("script",attrs = s)
replaceNodes(node, newNode)
}else{
str=ReadFileForEmbedding(fname);
if (!is.null(str))
{ 
newNode = xmlNode("script", str, attrs = c(type = "text/javascript"))
replaceNodes(node, newNode)
}
}
}

# extract all <link> tags with src value
linkNode=getNodeSet(top, '//link[@href]')
for (node in linkNode)
{
b = xmlAttrs(node)
fname = file.path(dir, b['href'])
str = ReadFileForEmbedding(fname, FALSE);
if (!is.null(str))
{
newNode = xmlNode("style", str)
replaceNodes(node, newNode)
}
}

saveXML(html, file = fnameOut)
return(TRUE)
}
ReadFileForEmbedding <- function(fname, addCdata = TRUE)
{
data = ReadFullFile(fname)
if (is.null(data))
return(NULL)
str = paste(data, collapse ='\n')
if (addCdata) {
str = paste(cbind('// <![CDATA[', str,'// ]]>'), collapse ='\n')
}
return(str)
}
ReadFullFile <- function(fname)
{
if(!file.exists(fname))
return(NULL)

con = file(fname, open = "r")
data = readLines(con)
close(con)
return(data)
}
FindSrcReplacement <- function(str)
{
# finds reference to 'plotly' js and replaces with a version from CDN
# This allows the HTML to be smaller, since this script is not fully embedded in it
str <- iconv(str, to="UTF-8")
pattern = "plotlyjs-(\\w.+)/plotly-latest.min.js"
match1=regexpr(pattern, str)
attr(match1, 'useBytes') <- FALSE
strMatch=regmatches(str, match1, invert = FALSE)
if (length(strMatch) == 0) return(NULL)

pattern2 = "-(\\d.+)/"
match2 = regexpr(pattern2, strMatch[1])
attr(match2, 'useBytes') <- FALSE
strmatch = regmatches(strMatch[1], match2)
if (length(strmatch) == 0) return(NULL)

# CDN url is https://cdn.plot.ly/plotly-<Version>.js
# This matches the specific version used in the plotly package used.
verstr = substr(strmatch, 2, nchar(strmatch)-1)
str = paste('https://cdn.plot.ly/plotly-', verstr,'.min.js', sep='')
return(str)
}
#################################################