Slide 1: Slide 2: It all began with a phone conversation with Hadley Wickham. We mostly discussed limitations of the current R graphics system, but Hadley then asked what would the ideal graphics system look like to me. I was stumped; it has been so long since I considered starting over from scratch ... Slide 3: Also, I have trouble thinking on my feet. So it was only much later that I thought properly about what I would be interested in building. And it occurred to me that the combination of HTML + SVG + CSS is something that I really like. Slide 4: I have done plenty of generating HTML + SVG + CSS, either manually or programmatically with R. I have also done a bit of manipulating HTML + SVG + CSS with javascript. But what I would like to do is generate AND manipulate HTML + SVG + CSS with R. However, I do NOT want to have to be responsible for the rendering (at least not for initial experimentation). Can I get a browser to do the hard work for me ... ? Slide 5: The first step is to load the 'DOM' package. The htmlPage() function opens a new page in a web browser and returns a websocket connection which we can use to send requests from R to the browser. Slide 6: The 'DOM' package implements some of the standard DOM interface to the content of an HTML document. For example, the appendChild() function allows us to append new content to the end of a web page. Slide 7: These are some other examples of DOM actions: we can append content to a specific location, we can replace or remove content, and we can query the document for its content. Slide 8: Because we are in R, there are useful tools for programmatically generating HTML content. This example shows the 'xtable' package being used to generate an HTML table, which is then appended to the web page. Slide 9: The websocket connection is two-way, so the browser can also send requests to R. This code sets up the web page to call an R function whenever there is a mouse click on the first paragraph of text. Calls from the browser are limited to a call of the form ... RDOM.Rcall(name-of-R-function, pointer-to-DOM-element(s), format(s)-of-DOM-element(s)-passed-to-R-function, callback) This is another advantage to using the browser to render the web page content: the browser can respond to user events on the content of the web page document. The 'DOM' package allows R to respond to these user events in the browser. This also demonstrates that communication between R and the browser is asynchronous. R is not blocked waiting for the browser to callback(). The callback() function runs when the browser sends a request on the web socket. Most 'DOM' functions block by default so that we can run R code one step at a time as usual, but they can also be called asynchronously. Slide 10: We can also embed an SVG image within a web page, though namespace information must be provided. Slide 11: As with HTML content, we can use R packages to generate the SVG content that we want to add to the web page. This code uses 'lattice' to draw a plot and 'gridSVG' to generate an SVG version of the plot, which is then appended to the page. Slide 12: The browser can respond to user events on the SVG content of the web page document, and we can make that response a request to R, which can in turn modify the web page. This code creates a simple interactive graphic where a mouse click on a data point displays more information about the data point. The code above: identifies all 'use' elements in the SVG; adds an 'onclick' attribute to them that will call the R function info(); starts a new web page containing a paragraph of instructions; appends the SVG plot; appends an empty 'pre' element; defines an R function info(). After running the above code, a click on one of the data symbols in the plot in the browser will fill the 'pre' element on the web page with information about the data point. Slide 13: This code also shows a two-way interaction between the browser and R. The difference is that we are using the filePage() function to open an web page that someone else has previously created. Clicking on a paragraph calls back to R and replaces the paragraph with a table of counts of the words in the paragraph. This example demonstrates the filePage() function, which allows us to make a websocket connection to an existing web page (on the local file system; there is also a urlPage()). For this to work, the web browser should be Firefox, the greasemonkey plugin must be installed, and the RDOM.user.js user script must be installed in that. This example also demonstrates that I am interested in working with both HTML and SVG content from R. Slide 14: The 'DOM' package: is lower-level than 'Shiny' (more flexible and more dangerous); also, 'DOM' responds to user events, rather than just driving the browser (like 'RSelenium'); 'DOM' renders web content and responds to user events, rather than just allowing querying of web content (like 'rdom'); and 'DOM' is more general-purpose than just allowing chunks of code in a web page to be run (like "R Notebooks"). 'DOM' differs from 'rvest' because the latter interacts with the browser via HTTP (rather than a web socket), so is "limited" to HTTP actions (GET, PUT, ...). Slide 15: Slide 16: