Skip to content

Instantly share code, notes, and snippets.

@malcook
Last active August 1, 2022 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malcook/88c8fab12fdcaa2631a6e5cb6e8d768f to your computer and use it in GitHub Desktop.
Save malcook/88c8fab12fdcaa2631a6e5cb6e8d768f to your computer and use it in GitHub Desktop.
This emacs org-mode document demonstrates an approach to embedding R's htmlwidgets in the exported html resulting from org-html-export-html.

1 Overview

This emacs org-mode document demonstrates an approach to embedding htmlwidgets in the exported html resulting from org-html-export-html. The approach:

  • uses (coopts?) org’s :prologue/:epilogue header args.
  • allows for an htmlwidget to be developed interactively “normally” within a code blocks, isolating the machinery for their inclusion in an exported html document to the time of html-export.
  • suffers from the possibility that the javascript in two widget may conflict. For example uncommenting the results of the plotly::ggplotly demonstration below and re-exporting to html will cause the DT::datatable to break (unless the DT:datatable is included after the ggploty). They may both define a handler for the document’s onLoad event, only one of which will run, thereby disaling the other. TODO: understand fully and address.

2 Method

* Demonstrations

Following are examples that use various kinds of htmlwidget.

#+caption: Include librarys required for demonstrations
library(DT)
library(RColorBrewer)
library(fission)
library(ggplot2)
library(gplots)
library(metricsgraphics)
library(networkD3)
library(plotly)

2.1 Network layout using forceNetwork

data(MisLinks, MisNodes)
forceNetwork(Links = MisLinks, Nodes = MisNodes, 
  Source =  "source", Target = "target", Value = "value", 
  NodeID = "name", Group = "group", opacity = 0.4)

2.2 Word Clouds using rWordCloud

library(rWordCloud)
text <- c('d3','wordcloud','impressive','experiment','htmlwidgets','myfirstwidget')
size <- c(25,20,13,9,6,5)
df <- data.frame(text,size)
d3Cloud(text = text, size = size)

2.3 ScatterPlots using metricsgraphics

2.3.1 An MA plot with 7039 genes

## load a example expression data as a SummarizedExperiment, and
## format two experiments as data.frame
data("fission")
cnt<-assay(fission)
names(dimnames(cnt))<-c('gene','sample')
dat<-as.data.frame(cnt[,1:2]) ##"GSM1368273" "GSM1368274"
dat$gene<-rownames(dat) ## needed as column to pass to javascript widget

# compute M, A, and encode DE as down, up, no change.
dat$M<-log2(sqrt(dat[,1] * dat[,2]))
dat$A<-log2(sqrt(dat[,1] / dat[,2]))
dat$DE <- c('DN','NC','UP')[(sign(dat$A) * as.integer(abs(dat$A)>1)) +  2]

# create the widget
dat %>%
mjs_plot(x=M
        ,y=A
        ,width=600, height=500
         ) %>%
mjs_point(color_accessor=DE
         ,color_type='category'
         ,color_range=c('green','blue','red')
          ,least_squares=TRUE
          ) %>%
mjs_labs(x="MM", y="AA") %>%
mjs_add_mouseover("function(d, i) {
                $('{{ID}} svg .mg-active-datapoint')
                     .text([d.point.gene, d.point.DE, 'M:', d.point.M.toFixed(2), 'A:', d.point.A.toFixed(2)].join(' '));
               }")

2.3.2 The cars example from http://rpubs.com/hrbrmstr/53741

mtcars %>%
  mjs_plot(x=wt, y=mpg, width=600, height=500) %>%
  mjs_point(color_accessor=carb, size_accessor=carb) %>%
  mjs_labs(x="Weight of Car", y="Miles per Gallon")

2.4 DataTables using DT

iris
iris

In either case, you can cause `org-html-export-to-html` to inline the exported html file with `#+include iris.html export html`, resulting in:

iris
iris
iris

2.5 ggplot using plotly

ggplot(data = diamonds, aes(x = cut, fill = clarity)) +
            geom_bar(position = "dodge")

These results are intentionally commented out since thier export conflict with those of previous DT::datatable.

3 Source Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment