How to Use R Plotly Library in R Script Visual of Power Bi

Power BI: include an htmlwidget other than a plotly graphic

Since I know manage, I answer my own question. Here is an example. First, install pbiviz as explained in the links given in my question.

  • create a working directory: mkdir pbivizWorkplace and go into: cd pbivizWorkplace

  • inititalize a new visual: pbiviz new Ggiraph -t rhtml

  • in the created folder Ggiraph, edit pbiviz.json; you have to write something for description (what you want), supportUrl (e.g. https://www.example.com), and name and email

  • edit script.r, e.g.:



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

############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("ggiraph")
####################################################

################### Actual code ####################
data <- Values # we will use 'mtcars' as 'Values'
data$carname <- row.names(data)
gg_point <- ggplot(data = data) +
geom_point_interactive(aes(x = wt, y = qsec, color = disp,
tooltip = carname, data_id = carname)) +
theme_minimal()
####################################################

############# Create and save widget ###############
p <- girafe(ggobj = gg_point)
internalSaveWidget(p, 'out.html');
####################################################

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

The data imported in Power BI corresponds to Values in the script.

Since we want to use mtcars as data, we save it as an Excel file, but before that, we add a column with the row names, because we will use the row names as tooltips:

mtcars$carname <- rownames(mtcars)
openxlsx::write.xlsx(mtcars, "mtcars.xlsx")

  • Go to the folder pbivizWorkplace/Ggiraph and run pbiviz package

  • Open Power BI, import mtcars.xlsx, select and load the sheet

  • In the 'Visualizations' panel, click the three dots, the 'import a visual from a file', and select the pbiviz file in pbivizWorkplace/Ggiraph/dist

  • a new icon (showing some tools) appears at the bottom in the 'Visualizations' panel, click it

  • in the 'Fields' panel, select the columns used for the plot, here wt, qsec, disp and carname

  • you get the interactive graphic in Power BI:

Sample Image

R visuals not producing graphs in power bi

Now, we can create RHTML custom visual in power BI

1) we can use Ploty - to use it you have load you "midwest" data in power BI table

2) then drag it to R script so it will available to R Script .

3) then run R script , it will work

My R code is not plotting the visual correctly

I had to edit the x and y inputs in order to reproduce the example, but your issue seems to be a missing closing parenthesis on this line

time = factor(x,

This code gets me the result I think you're after

x<-c("Dinner", "Lunch")
y<-c(14.89,17.23)

dat <- data.frame(
time = factor(x),
total_bill = y
)
p <- ggplot(data=dat, aes(x=time, y=total_bill)) +
geom_bar(stat="identity")
p

Update:
Thanks for the additional info. I'm unfamiliar with Power BI, but it looks to me like the issue might be that the columns in 'dataset' might be unnamed (or at least it's unclear what the column names are). We can set the column names which should then allow us to plot as intended:

colnames(dataset)<- c("time", "total_bill") 

p <- ggplot(data=dataset, aes(x=time, y=total_bill)) +
geom_bar(stat="identity")
p


Related Topics



Leave a reply



Submit