R Output Types

From Q
Jump to navigation Jump to search

R output can appear in several different formats. In particular, within Q we model:

  • Textual output
  • Charts
  • Tabular output
  • Widgets (e.g. D3)
  • Help pages

Error and raw R output is in addition to these types.

Sometimes R code may yield more than one of these types of output. In our first example, the following code:

  cat("There are seven data points in this example") 
  plot(1:7, c(45, 2, 8, 9, 23, 17, 38))

yields both text and a chart as shown in the raw R output:

  > cat("There are seven data points in this example")
  There are seven data points in this example
  > plot(1:7, c(45, 2, 8, 9, 23, 17, 38))
  [[ plot ]]

In this example, Q shows both output types side-by-side and posts a notification which asks the user to choose which output they would like to have to represent this R item.

ROutputSelectionExample1.png

A block of code in R also has the notion of something which is returned. When this return object is something that Q considers to be tabular, Q will present it as a table. So, for example, the following code produces the following output:

  m = matrix(1:20, 4, 5)

ROutputSelectionExample2.png

Note that whereas the raw R output displays this object as text, Q has displayed it as a table. When this code is run, Q does not present both the textual and tabular output formats side-by-side, as often the two formats represent the same information, and so it is redundant. (However, one can still display the textual output - see 'Selecting Output Type for R Items' below.) Similarly, when the R code returns a help page, or a widget, Q assumes that this is the information which the user would like to see, and presents that information by default.

Q's treatment of R output

So, to summarise:

  • If the R code returns a help page or a widget, that will be displayed to the user.
  • If the R code returns a tabular object and produces a chart, both the table and the chart will be shown, and the user will be asked to make a choice.
  • If the R code produces textual output and a chart, both the text and the chart will be shown, and the user will be asked to make a choice.
  • If the R code returns a tabular object and produces textual output, just the tabular object will be shown to the user.
  • Otherwise, the R code will produce either textual output or a chart, in which case it will show that output.

In any case, the object returned by a block of R code, will be the object which other R items in Q can refer to. This is independent of the way that the R item is displayed.

Selecting Output Type for R Items

When Q decides that the user should choose which type of output they would like displayed, a notification is posted:

ROutputSelectionNotification.png

The user can resolve this immediately by pressing on the appropriate button, which dismisses this notification, and the choice is remembered for this R item.

However, the user may want to change their mind about the output format, or want to select a different output format from one of the ones which appears in the notification bar. Q provides access to each of the different output formats that are produced: choose from the available outputs listed in the Output type combo box in the DISPLAY section of the Size and Properties part of the Object Inspector:

ROutputType.png

Surprising aspects of R

Occasionally, R code has surprising aspects. For example, mostly charting code will return NULL, but not always. In particular, barplot(), when applied to vectors, returns a matrix.

  x = barplot(1:7, c(45, 2, 8, 9, 23, 17, 38))

So whilst a chart is typically what is expected to be seen, the value of 'x' in this example is a matrix. It is invisible, but nonetheless, that is the return value. This means that if 'x' is referenced elsewhere in Q, it will have the value of the matrix. Indeed, in this case, Q will recognise that a matrix is returned, but a chart has also been produced, so will present the user with the choice:

ROutputSelectionExample3.png