• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Incorporating ggplotly into power BI

john.anderson

New Member
Hi everyone, I can't get my custom visual to work in power BI? I have followed the excellent step by step guide from https://stackoverflow.com/questions/39368729/is-it-possible-to-use-r-plotly-library-in-r-script-visual-of-power-bi
and I was able to get the 'Iris' dataset into my power bi with all the full functionailty of ggplotly working so was stoked...
but when I customise my r.script file to my trial dataset, recreate the pbiviz custom file, import that file and my trial dataset into power bi, then when I drag
the fields into the custom visual template, no visual appears, only an error message, "Error in data.frame(closed, opened, weeks_ago): object 'closed' not found Execution halted"!! The weird thing is, if I just click on R in the visualisation pane, then tick the fields, closed, opened, weeks_ago, R automatically creates
the dataframe, dataset <- data.frame(closed, opened, weeks_ago) and when I add the remaining code below substituting trial with dataset and use the remaining code below (excluding the very last line), I get the visual appearing which is of course non interactive. In summary, why am I getting that error message when trying to create my visual with the ggplotly functionality from my trial dataset?? I will attach my excel trial dataset (use the trial tab) to see if someone can have a go from the link above and succeed. Note, I'm new to power bi and R and I consider this a new post because the topic is about incorporating ggplotly custom visuals into power bi. Look forward to your reply.
My r script is:


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

############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly")
libraryRequireInstall("reshape2")
###################Actual code#####################
trial <- data.frame(closed, opened, weeks_ago)
trial <- unique(trial)

names(trial) <- gsub(" ","_",names(trial))
m1 <- min(trial$weeks_ago)
m2 <- max(trial$weeks_ago)
###################################################
data.m <- melt(trial, id.vars='weeks_ago');
####################################################
q <- ggplot(data.m, aes(weeks_ago, value)) + geom_bar(aes(fill = variable),
                                                      width = 0.4, position = position_dodge(width=0.5), stat="identity") + 
  scale_x_reverse(breaks=seq(m1,m2,by=1)) + theme_classic()
############# Create and save widget ###############
p = ggplotly(g);
internalSaveWidget(p, 'out.html');
####################################################
 

Attachments

Last edited by a moderator:

Chihiro

Excel Ninja
One mistakes/old info, in the stackoverflow link.

Code:
pbiviz new pbiDensity -t html
Should be...
Code:
pbiviz new pbiDensity -t rhtml
Looking at your code, it shouldn't work as is... You are converting 'g' to plotly, but 'g' isn't defined in your code.

Rather than uploading Excel file. Can you upload zipped PBIX file that contains R-visual that's working (not the custom visual that you imported)? It will make it much easier for me to help you from there.
 

john.anderson

New Member
Hi and thank you for responding to my post. Yes, I corrected g to q in my code, still no luck, still getting that error message in pbi not finding my dataframe objects. Yes, I changed html to rhtml in the stack overflow instructions (nice spot) and followed it perfectly and succeeded in producing the density plotly visual so I am stoked with that. I have attached a zip file containing both pbi files which work, Iris and Density. If you are able to shed light on my code in terms of getting pbi to recognse my objects that would be great and thank you for taking the time to investigate for me. Look forward to your reply.
 

Attachments

john.anderson

New Member
Hi everyone, I have solved the problem of why my visual didn't work. Reason is pbi didn't recognise my objects so below is the working code in R. Notice how I set up my data frame with my 3 variables, 'closed', 'opened' and 'weeks_ago'. This successfully produces a clustered bar chart plot of opened and closed responses by weeks ago, whereby opened and closed bars are beside each other and weeks ago is the x-axis with most recent week '1' is at the right of the plot. This produces a fully interactive ploty visual in power bi.

Code:
source('./r_files/flatten_HTML.r')
############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly")
libraryRequireInstall("reshape2")
####################################################
df <- data.frame(closed=Values$closed, opened=Values$opened,weeks_ago=Values$weeks_ago)
df <- unique(df)
names(df) <- gsub(" ","_",names(df))
m1 <- min(df$weeks_ago)
m2 <- max(df$weeks_ago)
################### Actual code ####################
data.m <- melt(df, id.vars='weeks_ago');
####################################################
q <- ggplot(data.m, aes(x = weeks_ago, value)) + geom_bar(aes(fill = variable),
                                                      width = 0.4, position = position_dodge(width=0.5), stat="identity") + 
  scale_x_reverse(breaks=seq(m1,m2,by=1)) + theme_classic()
############# Create and save widget ###############
p = ggplotly(q);
internalSaveWidget(p, 'out.html');
 
Last edited by a moderator:

Chihiro

Excel Ninja
Glad you got it solved, and thanks for sharing your solution. Sorry wasn't able to respond, was off sick yesterday.
 
Top