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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
MARKZI
Regular Visitor

[R] Leaflet Visual works on Desktop, breaks on Service

Hello fellow developers,

 

i have developed a custom R Visual (leaflet map) in Power BI using R Studio. The visual works just as intended on Desktop Version but once i publish the dashboard to services, it seems to run different code. I'm on directQuery mode so the datasource is the same locally as on service. Yes i have matched the recommended Versions for leaflet (2.2.2) and R (4.3.3) on my local instance. I don't use any more packages or crazy functions, everything is build in R functionality. 

I basically have this dataframe:

 

df <- data.frame(
  Latitude[,1],
  Longitude[,1],
  Timestamp[,1],
  Layers
)

 



Lat and Long values + a (field) parameter that allows me to pass certain layers to the visual. These layers (columns) are booleans and if set to True the lat long value will be kept, if it is set to false the lat long value will be dropped.

 

#Get only the extra layers (not the lat longs and timestamp):

 

extra_columns <- setdiff(colnames(Layers), c("timestamp", "geo_lat", "geo_long")) 

 

# If there are NA values they will be dropped:

 

df <- df[rowSums(is.na(df) | df == "") == 0, ]

 

# For each Layer i need to convert the string "True"/"False" into a boolean True /False

 

for (col in extra_columns) {
  if (col %in% colnames(df)) {
    df[[col]] <- as.logical(ifelse(is.na(df[[col]]), FALSE, df[[col]]))
  }
}

 


Now i create one map layer per extra column with the respective True/False Filtering of that column.

 

for (col in extra_columns) {
  filtered_data <- df[df[[col]] == TRUE, ]
  
  num_points <- nrow(filtered_data)
  
  if (num_points > 0) {
    layer_color <- generate_random_color()
    
    layer_colors[[col]] <- layer_color
    layer_labels[[col]] <- col
        
    map <- map %>%
      addCircleMarkers(
        lng = filtered_data$Longitude,
        lat = filtered_data$Latitude,
        radius = 5,
        color = layer_color,
        stroke = FALSE,
        fillOpacity = 0.8,
        group = col,
        label = ~paste("colnames: ", colnames(filtered_data)) %>% lapply(htmltools::HTML)
      )
  }
}

 


This works perfectly when locally executed, however once published it seems that there is no filtering and for what reason ever i get all datapoints on every layer. I have no idea why though.

 

1 ACCEPTED SOLUTION

Coming back to this thread with the solution:

 

I noticed that the class(df[,4]) with 4 being one of my True/False Layer returned different types on local and on services. On Local i received character which i succesfully casted on logical. On Services this line of code returned integer. I played around with more robust casting and datatype differentiation and fixed it like this:

for (col in extra_columns) {
  if (col %in% colnames(df)) {
    if (!is.logical(df[[col]])) { 
      df[[col]] <- as.logical(ifelse(df[[col]] %in% c("True", "true", "1"), TRUE, FALSE))
    }
  }
}

 

 

View solution in original post

2 REPLIES 2
Greg_Deckler
Super User
Super User

 Two thoughts come to mind. One, Leaflet is a JavaScript library and the R package simply allows you to use that library to create maps. So, perhaps there is something about the JavaScript running in Desktop versus the Service that is different? 

The other is perhaps there is a difference in the sub-packages:

@MARKZI



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Coming back to this thread with the solution:

 

I noticed that the class(df[,4]) with 4 being one of my True/False Layer returned different types on local and on services. On Local i received character which i succesfully casted on logical. On Services this line of code returned integer. I played around with more robust casting and datatype differentiation and fixed it like this:

for (col in extra_columns) {
  if (col %in% colnames(df)) {
    if (!is.logical(df[[col]])) { 
      df[[col]] <- as.logical(ifelse(df[[col]] %in% c("True", "true", "1"), TRUE, FALSE))
    }
  }
}

 

 

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

Check out the April 2025 Power BI update to learn about new features.

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors