Using plotly.js within Labkey

LabKey Support Forum (Inactive)
Using plotly.js within Labkey Will Holtz  2016-01-30 08:50
Status: Closed
 
I've been trying to use plotly.js within Labkey with the hope of generating interactive plots. I thought the easiest route to achieving this would be to create an R report with Labkey that contains a knitr document and then in that knitr document, use the R plotly library to generate the interactive plots. Finally, I'd like to use Rserve to make all this run faster, but that is an aside at this point.

My reasoning for going through R, rather than directly using the plotly javascript library, was not wanting to have to transform the data coming from Labkey into the format required by plotly.js. Additionally, using the plotly R library means that I can create my plots using ggplot2 (which I am already well versed in) and then automatically convert them to plotly plots (via ggplotly). I also could have skipped the knitr step, and directly used the plotly R library within an R view, but it isn't clear to me how to get the plotly R library to output javascript/HTML that Labkey could display. I'm guessing this is possible -- it just wasn't clear to me.

So, using the Lakbey>R report>knitr>plotly approach yields a document that contains "<!–html_preserve–>" tags flanking where the plot should be. Between the html_preserve tags is a bunch of json containing the data for the plot, but I don't see the javascript for the actual plot generation. Some Googling has revealed that knitr is generating the html_preserve tags and it has something to do with escaping other HTML tags. This thread https://github.com/rstudio/rmarkdown/issues/326 indicates that this HTML escaping behavior can be modified by passing options to md_extentions in knitr. I tried including the YAML header with md_extensions options in my Rmd file that generates my R report, but it appears that Labkey does not process the header as such. I spent a few minutes looking at the Labkey R report generation java code and couldn't find exactly how knitr is getting called.

Has anyone else gotten interactive plots with plotly.js working within Labkey? It would be a big win for the Labkey community to be able to easily leverage all the power of plotly. Any thoughts or pointers are appreciated.

thanks,
-Will
 
 
Matthew Bellew responded:  2016-01-30 09:30
It only takes a few lines of javascript to translate the selectrows format into another layout. This seems like a very round-about way of tackling the problem.

Also have a look at LABKEY.vis package which is loosely modeled on ggplot.

   https://www.labkey.org/files/home/Documentation/%40files/javascript-api/symbols/LABKEY.vis.Plot.html
 
Will Holtz responded:  2016-01-30 09:56
Hi Matthew,

I agree my approach is rather round-about. I will likely investigate performing the data format transformation and using plotly.js directly as a short term solution. However, my approach is advantageous for me, because I have users with experience in R who would generate plotly.R reports via Rmarkdown, but are unlikely to consider writing javascript code. I have used the LABKEY.vis package and found it a bit limited for my needs.

thanks,
-Will
 
Will Holtz responded:  2016-02-01 16:24
Today I wrote javascript code to do the data format transformations and was able to use plotly.js directly. While it did not take a lot of code, it took a while for me to get it right. Transforming the data for a simple scatter plot is easy, but plots involving grouping are more involved. The d3 library, and in particular d3.nest(), turned out to be very useful but also has a bit of a learning curve. I might post some code once I do some refactoring.

I still think the Lakbey>R report>knitr>plotly approach will be useful for opening up plotly to more users, and I plan to spend some more time looking into the knitr HTML escaping issue.

-Will
 
dcivello responded:  2016-04-29 15:10
I'm in exactly the same boat here, and agree that a Lakbey>R report>knitr>plotly workflow would be fantastic. Have you made any progress on the HTML issues? We do a ton of analysis delivered as markdown (on NGS data), and would love to have this functionality right within labkey where all the data lives. I've made the decision to stick with implementation in R vs. translating to using labkey's internal visualization tools because we're often plotting thousands of points, with complex grouping (also mentioned above) -- plot.ly/ggplot seems to be the trick, but can't get it working within the knitr R view. Additionally, the htmlwidgets offer some great interactivity as well, seems like there are just some hurdles to get the JS to place nicely with labkey.

I do have a way around this however, and managed to configure the wiki view to point to a shiny application vis some JS in the source tab. As long as the shiny server can see the same data as your labkey study data, they don't even need to be on the same server, and the user doesn't know the difference -- they just see the shiny application running from within labkey. That said, because the markdown documents are so portable, would be great to get some interactive functionality within the markdown document itself.

-daniel
 
Will Holtz responded:  2016-04-29 15:41
Hi Daniel,

I have not yet had time to do further investigations into the Lakbey>R report>knitr>plotly workflow. It is still on my todo list, but I forsee it being a rather low priority for me for at least several months.

I should take another look at using shiny. My recollection was that the open source version wasn't going to meet some data security requirements I have and the paid version didn't justify the price tag for me.

-Will