View this using the R command viewHtml() having sourced the files gtkhtml.S and command.S in the examples directory. Run this from the directory above examples/.

GtkHTML & R

You should see three images!
A PNG file A JPEG fileA JPEG via a remote URL

Embedded Objects

This section illustrates a very powerful facility offered by the gtkhtml widget that allows us to embed aribtrary widgets within it using the traditional <OBJECT> tag. This allows us to develop quite sophisticated interfaces as ``applets'' within larger HTML documents. The actions/callbacks for these objects are written in R and so can easily communicate with each other as they wish.
This is a slider that is created by R code that retrieves the parameters min, max and value to get the settings for the slider.
This object is created by returning a function from the init parameter and this is called with the embedded object as an argument. From that object, we can get access to the additional parameters such as the file. This example creates a button with the value of the file parameter as the label. When this button is clicked, we call viewHtml() with that file name, creating a second window.
These examples illustrate the well-designed mechanism for extending the HTML widget with elements that take care of their own drawing/rendering and user events. The approach is simple. When the HTML document is being parsed, <OBJECT> tags result in the creation of a GtkHTMLEmbedded widget. This is given the dimensions specified by the WIDTH and HEIGHT attributes of the <OBJECT> tag, and one can think of marking out the region for this embedded object. In this way, the engine has allocated space for the embedded object and doesn't care what goes inside it.

The next step is that the rendering engine generates a signal for the object-request callback type. Any function registered for that signal is called and is given the HTML widget and one additional argument, the GtkHTMLEmbedded widget. From this, one can get access to the recognized attributes of the <OBJECT> tag and also the "options" specified via the <PARAM> tags within the <OBJECT> tag. The callback function can do whatever it wants at this point. Typically it will create other widgets and add them to the GtkHTMLEmbedded widget. In this way, they will be visible in the HTML page when it is rendered. Additionally, callbacks can be registered for the signals emitted by these new widgets and everything behaves as expected.

The function embeddedObjectHandler() in gtkhtml.S provides an example of how one can handle embedded object tags. This behaves much like a factory object that produces embedded objects on request. It examines the type attribute of the GtkHTMLEmbedded widget passed to it. It uses this to process any types that it understands. This can be done via an if-else sequence or via a lookup in a table for a handler function. Each of these sub-handlers can examine the attributes and parameters to look for non-default values. For example, the first button example of type app/x-button provides its own label in a parameter named name!

This approach alone would necessitate having registered all types ahead of time. We can make this more flexible and exploit the fact that we have an interpreted programming language underlying this interface. One can specify a type, say app/x-R-script and provide an init parameter that contains an R script. This is evaluated and can do as it chooses to create the contents of the embedded object. If it returns a widget, that object is added to the GtkHTMLEmbedded widget. (This can be prohibited by specifying a value of FALSE for a parameter named addWidget.)

In some scenarios, the actions in the script will need access to the top-level HTML widget or the GtkHTMLEmbedded widget. For example, the script may need to read additional parameter values from the PARAM tags. In such cases, the script should return a function that takes two arguments: the HTML widget and the embedded widget, in that order. Then, it has access to all the parameters, etc.

Soon, we will add the Gtk graphics device for R as an embeddable component.


This is a simple interface to an R command line that uses HTML forms rather than the rich set of Gtk widgets. It is similar to the effect that one can obtain via the SNetscape package. We can mix regular content (text, images, sound, etc.) freely with the input and output GUI controls and elements. There are certain advantages to this approach and several disadvantages.

This interface is very trivial. The user specifies a command in the text/entry box. Hitting return or clicking on the evaluate button. The output is most easily printed on the R console. There are two other obvious alternatives.

HTML Form Element
We can display the output in another HTML form element. Specifically, if we agreed with the author that there must a text component, say, with the name output. Then we could arrange for R to set the value of that element with the contents of the printed result. This is a case where using JavaScript is simpler since it can find the element and set its value at the same programming interface/level as the form contents.
Gtk element
We can create an interface in R using Gtk that has the HTML widget in one part of the GUI and an output component in another. For example, we can split the window into two have an output window in the lower part. The downside of this is that the author loses the ability to easily reorganize and reformat the visual display. The benefits include the fact that it is currently simpler and also that we can use existing output components such as another HTML component!

This approach is implemented via the function localOutput()

One can imagine filtering the S commands and results via the html filter in gtkhtml.S to an HTML editor. Then the user could annotate the contents. We add new elements, maintaining these annotations. The result is an somewhat readable and re-runnable session. One can change the initial context (i.e. the initial value of variables, search path, etc.) and then run the session to get different output. This is getting closer to the interactive text book that I am working on with Deb Nolan and Sam Buttrey.

We can embed grpahics devices and other controls

It is very easy to modify this interface since there is no ``programming'' involved. We simply reorganize the text. With some minimal additions to the underlying R code that displays this, we can embed the HTML component within a larger GUI. Additionally we can have it display R help, show the search path, etc. See some of the examples in the SNetscape package.


Form

Visible output: On
Off
Command:
Output:

Duncan Temple Lang <duncan@research.bell-labs.com>
Last modified: Thu Nov 21 10:43:21 UTC 2002