cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Frequent Visitor

[SOLVED] R plotly custom visualization margin padding

Good morning all, 

 

I am creating a custom visualization with R with the plotly package.

 

The issue I am having is with the margin padding the custom visualization is adding to the output. I have set up the plotly chart to use 0 margin and padding, but the PowerBI visualization is adding padding I am trying to get rid off. Be aware in RStudio the plotly chart is with 0 margin, so I am positive the chart itself is produced correctly.

 

I have gone through the json files for the visualization trying to figure out an option I could find in the files, but I haven't been successful.

 

Is it possible to remove the padding the custom visuals are adding to the plotly chart?

 

Thank you

 

Kind regards,

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Frequent Visitor

[SOLVED] Re: R plotly custom visualization margin padding

Good morning, 

 

I have finally managed to get rid of the extra padding introduced in the visualization. I appears the bug is with htmlWidgets, that introduces padding into the html file.

 

A simple R function can get rid of the padding, such as the one below:

ReadFullFileReplaceString <- function(fnameIn, fnameOut, sourceString,targetString) {
  if(!file.exists(fnameIn))
    return(NULL)
  tx  <- readLines(fnameIn,encoding = "UTF-8")
  tx2  <- gsub(pattern = sourceString, replace = targetString, x = tx)
  writeLines(tx2, con = fnameOut)
}

The calling the function like so:

ReadFullFileReplaceString('out.html', 'out.html', ',"padding":[0-9]*,', ',"padding":0,')

Credit goes to MS, as the solution was found in one of their R visualizations, PowerBI-visuals-forcasting-exp

in their script.r file: https://github.com/microsoft/PowerBI-visuals-forcasting-exp/blob/master/script.r

 

 

View solution in original post

6 REPLIES 6
Highlighted
Solution Sage
Solution Sage

Re: R plotly custom visualization margin padding

Hi @JVidal ,

In this scenario, I would suggest you opening an idea in the idea forum at https://ideas.powerbi.com/forums/265200-power-bi-ideas

Best Regards,

Teige

Highlighted
Frequent Visitor

[SOLVED] Re: R plotly custom visualization margin padding

Good morning, 

 

I have finally managed to get rid of the extra padding introduced in the visualization. I appears the bug is with htmlWidgets, that introduces padding into the html file.

 

A simple R function can get rid of the padding, such as the one below:

ReadFullFileReplaceString <- function(fnameIn, fnameOut, sourceString,targetString) {
  if(!file.exists(fnameIn))
    return(NULL)
  tx  <- readLines(fnameIn,encoding = "UTF-8")
  tx2  <- gsub(pattern = sourceString, replace = targetString, x = tx)
  writeLines(tx2, con = fnameOut)
}

The calling the function like so:

ReadFullFileReplaceString('out.html', 'out.html', ',"padding":[0-9]*,', ',"padding":0,')

Credit goes to MS, as the solution was found in one of their R visualizations, PowerBI-visuals-forcasting-exp

in their script.r file: https://github.com/microsoft/PowerBI-visuals-forcasting-exp/blob/master/script.r

 

 

View solution in original post

Highlighted
Helper IV
Helper IV

Re: [SOLVED] Re: R plotly custom visualization margin padding

@JVidal ,

I'm new to R script, where would I add your code in my script to remove the padding?

Below is my R script example;

 

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

libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly")

df <- data.frame(RunID = Values$RunID, CLWT = Values$xCLw, PCLF = Values$xPCLFw) 

g = ggplot(df, aes(x=PCLF, y=CLWT, color= as.factor(RunID)))+
  geom_point(size=2)+
  labs(title="Pan Polar", x = "% CLF",y = "CL")+
  theme(text=element_text(size=8),
        plot.margin = unit(c(0.1,1,0.1,0.1),"cm"))

p = ggplotly(g) %>% config(displaylogo = FALSE, 
                           modeBarButtonsToRemove = list
                           ('toImage',
                             'zoomIn2d',
                             'zoomOut2d',
                             'lasso2d',
                             'hoverCompareCartesian',
                             'select2d'));
internalSaveWidget(p, 'out.html');
Highlighted
Helper I
Helper I

Re: [SOLVED] Re: R plotly custom visualization margin padding

Hi,

You need to paste the @JVidal's script after 

internalSaveWidget(p, 'out.html')

That should do it.

Many thanks to @JVidal and the script works well.

 

Highlighted
Frequent Visitor

Re: [SOLVED] Re: R plotly custom visualization margin padding

Hi @JVidal ,

 

I am also trying to get rid off the extra padding of the custom visual. The R function you provided before, should this be included in script.r file? If so, does it comes before the "g = ggplotly(p); internalSaveWidget(g, 'out.html'); " part or after it.

 

Thanks in advance.

Ragards

Highlighted
Helper I
Helper I

Re: [SOLVED] Re: R plotly custom visualization margin padding

Hi JNahar,

 

Yes, you need to paste/run the above code in the script.r file and for second part of your question, please see the previous discussions in the post

 

Cheers

Helpful resources

Announcements
June 2020 Community Highlights

June 2020 Community Highlights

Featured community members, changes to the Community, and more! Read up on recent Power BI community news.

Using the Community

Using the Community

Need help with the Power BI Community? Our 'Using the Community' support articles are a great place to start.

Community Summit North America

Community Summit North America

Innovate, Collaborate, Grow. The top training and networking event across the globe for Microsoft Business Applications

Power Platform 2020 release wave 2 plan

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors