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.

Reply
Han_Solo
Helper I
Helper I

Custom Rscript - problem with published DiagrammeR. attr_theme doesn't refer to any available theme

Hello,

 

I have quite a bizzare error. On PowerBI desktop everything works flawlessly. But when I publish report online I receive an error  on Rsctipt Visual. attr_theme does not refer to anything while clearly it refers to working attribute.

 image_2022-01-04_182413.png

 

Code looks like this: 

 

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: 

# dataset <- data.frame(Ship From, Ship To, volume)
# dataset <- unique(dataset)

# Paste or type your script code here:


library(DiagrammeR)
uniquenodes <- unique(c(dataset$'Ship From', dataset$'Ship To'))
uniquenodes
options(scipen=99)


nodes <- create_node_df(n=length(uniquenodes),
                        label=uniquenodes,
                        color="DodgerBlue",
                        fillcolor ="GhostWhite",
                        shape ="rectangle",
                        fontcolor ="RoyalBlue4",
                        penwidth =2,
                        fontsize =16,
                        height =0.5,
                        width =0.7,
                        mindist =5)

edges <- create_edge_df(from =match(dataset$'Ship From', uniquenodes), 
                        to =match(dataset$'Ship To', uniquenodes), 
                        rel ="leads_to",
                        label =dataset$volume,
                        fontsize =14,
                        color ="SlateGray",
                        penwidth =2,
                        arrowsize =1)

g <- create_graph(nodes_df=nodes, 
                  edges_df =edges,
                  directed =TRUE,
                  attr_theme ="lr")
                  
tmp <- export_graph(g, file_name ="image.png", file_type ="png")

 

the custom Rscript visual renders in Desktop version as follows: 

Han_Solo_0-1641317213778.png

my question is. How is it possible that Rscript renders on desktop correctly and not on published web version?

Best Regards 

 

1 ACCEPTED SOLUTION

Hello,

 

@v-yanjiang-msft I was able to fix this. Apparently Power BI handles characters differently than R. 

First I assigned layout attribute not in create_graph function but in add_global_graph_attrs as "layout" and "rankdir" which eliminated the issue.

 

dataset$'Ship From' <- as.character(dataset$'Ship From')
dataset$'Ship To' <- as.character(dataset$'Ship To')

 

Then I changed uniquenodes values from integers to characters to able Power BI to match the values. with piping. 

 

g <- create_graph(nodes_df = nodes,
                  edges_df = edges,
                  directed = TRUE) %>% add_global_graph_attrs(
                    attr = c("layout", "rankdir"),
                    value = c("dot", "LR"),
                    attr_type = c("graph", "graph")
                  )

 

the whole code looks like this:

 

 

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:

# dataset <- data.frame(Ship From, Ship To, volume)
# dataset <- unique(dataset)

# Paste or type your script code here:

library(DiagrammeR)
options(scipen = 99)
dataset$'Ship From' <- as.character(dataset$'Ship From')
dataset$'Ship To' <- as.character(dataset$'Ship To')
uniquenodes <- unique(c(dataset$'Ship From', dataset$'Ship To'))

nodes <- create_node_df(
  n = length(uniquenodes),
  label = uniquenodes,
  type = uniquenodes,
  color = "DodgerBlue",
  fillcolor = "GhostWhite",
  shape = "rectangle",
  fontcolor = "RoyalBlue4",
  penwidth = 2,
  fontsize = 16,
  height = 0.5,
  width = 0.7,
  mindist = 5
)

edges <-
  create_edge_df(
    from = factor(dataset$'Ship From', levels = uniquenodes),
    to = factor(dataset$'Ship To', levels = uniquenodes),
    rel = "leading_to",
    label = dataset$volume,
    fontsize = 14,
    color = "SlateGray",
    penwidth = 2,
    arrowsize = 1
  )

g <- create_graph(nodes_df = nodes,
                  edges_df = edges,
                  directed = TRUE) %>% add_global_graph_attrs(
                    attr = c("layout", "rankdir"),
                    value = c("dot", "LR"),
                    attr_type = c("graph", "graph")
                  )

tmp <- export_graph(g, file_name = "image.png", file_type = "png")

 

 



View solution in original post

3 REPLIES 3
Han_Solo
Helper I
Helper I

Hello,

 

I was able to fix this. Apparently Power BI handles characters differently than R. 

First I assigned layout attribute not in create_graph function but in add_global_graph_attrs as "layout" and "rankdir" which eliminated the issue.

 

dataset$'Ship From' <- as.character(dataset$'Ship From')
dataset$'Ship To' <- as.character(dataset$'Ship To')

 

Then I changed uniquenodes values from integers to characters to able Power BI to match the values. with piping. 

 

g <- create_graph(nodes_df = nodes,
                  edges_df = edges,
                  directed = TRUE) %>% add_global_graph_attrs(
                    attr = c("layout", "rankdir"),
                    value = c("dot", "LR"),
                    attr_type = c("graph", "graph")
                  )

 

the whole code looks like this:

 

 

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:

# dataset <- data.frame(Ship From, Ship To, volume)
# dataset <- unique(dataset)

# Paste or type your script code here:

library(DiagrammeR)
options(scipen = 99)
dataset$'Ship From' <- as.character(dataset$'Ship From')
dataset$'Ship To' <- as.character(dataset$'Ship To')
uniquenodes <- unique(c(dataset$'Ship From', dataset$'Ship To'))

nodes <- create_node_df(
  n = length(uniquenodes),
  label = uniquenodes,
  type = uniquenodes,
  color = "DodgerBlue",
  fillcolor = "GhostWhite",
  shape = "rectangle",
  fontcolor = "RoyalBlue4",
  penwidth = 2,
  fontsize = 16,
  height = 0.5,
  width = 0.7,
  mindist = 5
)

edges <-
  create_edge_df(
    from = factor(dataset$'Ship From', levels = uniquenodes),
    to = factor(dataset$'Ship To', levels = uniquenodes),
    rel = "leading_to",
    label = dataset$volume,
    fontsize = 14,
    color = "SlateGray",
    penwidth = 2,
    arrowsize = 1
  )

g <- create_graph(nodes_df = nodes,
                  edges_df = edges,
                  directed = TRUE) %>% add_global_graph_attrs(
                    attr = c("layout", "rankdir"),
                    value = c("dot", "LR"),
                    attr_type = c("graph", "graph")
                  )

tmp <- export_graph(g, file_name = "image.png", file_type = "png")

 

 



v-yanjiang-msft
Community Support
Community Support

Hi @Han_Solo ,

There are some limitations when you use R script on power bi service, you can take the following links for reference:

Creating R visuals in the Power BI service

R packages in the Power BI service

As the document states that custom R packages are not supported in Power BI Service.


Known Limitations

R visuals in the Power BI service have a few limitations:


Best Regards,
Community Support Team _ kalyj

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hello,

 

@v-yanjiang-msft I was able to fix this. Apparently Power BI handles characters differently than R. 

First I assigned layout attribute not in create_graph function but in add_global_graph_attrs as "layout" and "rankdir" which eliminated the issue.

 

dataset$'Ship From' <- as.character(dataset$'Ship From')
dataset$'Ship To' <- as.character(dataset$'Ship To')

 

Then I changed uniquenodes values from integers to characters to able Power BI to match the values. with piping. 

 

g <- create_graph(nodes_df = nodes,
                  edges_df = edges,
                  directed = TRUE) %>% add_global_graph_attrs(
                    attr = c("layout", "rankdir"),
                    value = c("dot", "LR"),
                    attr_type = c("graph", "graph")
                  )

 

the whole code looks like this:

 

 

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:

# dataset <- data.frame(Ship From, Ship To, volume)
# dataset <- unique(dataset)

# Paste or type your script code here:

library(DiagrammeR)
options(scipen = 99)
dataset$'Ship From' <- as.character(dataset$'Ship From')
dataset$'Ship To' <- as.character(dataset$'Ship To')
uniquenodes <- unique(c(dataset$'Ship From', dataset$'Ship To'))

nodes <- create_node_df(
  n = length(uniquenodes),
  label = uniquenodes,
  type = uniquenodes,
  color = "DodgerBlue",
  fillcolor = "GhostWhite",
  shape = "rectangle",
  fontcolor = "RoyalBlue4",
  penwidth = 2,
  fontsize = 16,
  height = 0.5,
  width = 0.7,
  mindist = 5
)

edges <-
  create_edge_df(
    from = factor(dataset$'Ship From', levels = uniquenodes),
    to = factor(dataset$'Ship To', levels = uniquenodes),
    rel = "leading_to",
    label = dataset$volume,
    fontsize = 14,
    color = "SlateGray",
    penwidth = 2,
    arrowsize = 1
  )

g <- create_graph(nodes_df = nodes,
                  edges_df = edges,
                  directed = TRUE) %>% add_global_graph_attrs(
                    attr = c("layout", "rankdir"),
                    value = c("dot", "LR"),
                    attr_type = c("graph", "graph")
                  )

tmp <- export_graph(g, file_name = "image.png", file_type = "png")

 

 



Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors