Skip to content

Using DataTables in RCAP

Shane Porter edited this page Aug 24, 2016 · 1 revision

This document explains how options can be set in RCAP for DataTable controls, including sparklines.

No options/ default options

If the user does not wish to set any options, or use the default options, then the control function simply has to return a data.frame object.

This way, any old notebooks from before the DataTables revamp in RCAP will still continue to work as normal. The defaults may have changed a little though.

Setting options

Data Table options can be set by the control function, by returning a list of two elements, named "data" and "options". The data element is the data.frame that is to be displayed by the DataTable. The options element is itself a list, and the inner elements are described next.

Sparklines

First, note the shape of the data- to be able to make a plot of some data, we must have multiple values, not just one value in each cell of the table. This is supported by R:

mtcars$series <- lapply(seq_along(mtcars$mpg), 
						function(x) rnorm(rpois(1, lambda = 10)))
View(mtcars)

Such columns will be automatically detected, and the default plot will be a histogram. However, other types of plots (line graph, boxplot) can be specified in sparkOptions, a list object with elements called "line" or "box" which are vectors of column names.

sparklinesExample <<- function() {
	testData <- mtcars
	mtcars$series <- lapply(seq_along(mtcars$mpg), 
						function(x) rnorm(rpois(1, lambda = 10)))
	res <- list(data = mtcars)
	res$options <- list(sparkOptions = list(line = "series"))
	res
}

Column widths

In DataTable, column widths must be specified as percentages.

columnWidthsExample <<- function {
  list(
    data = mtcars,
    options = list(
      columnWidths = c("50%", rep("5%", 10))
    )
  )
}

Font size

Both the font sizes for table header and table body can be specified.

fontSizeExample <<- function() {
  list(
    data = mtcars,
    options = list(
      thSize = "12px",
      tdSize = "11px"
    )
  )
}

Right-align

rightAlignExample <<- function() {
  list(
    data = mtcars,
    options = list(
      rightAlign = c("mpg", "wt")
    )
  )
}

Format numeric columns

Fixed number of decimal places

numericFormatExample <<- function() {
    list(
        data = data.frame(big = rnorm(100)*1000,
            little = rnorm(100)),
        options = list(
            decimalPlaces = 3))
}

Add column colors

Both background and font colors can be added to columns in the data. Note you can pass hex colors as well as named colors. Also note the American English spelling of color, the British English spelling is not currently supported.

Background colors

changeColumnColorsExample <<- function() {
  list(
    data = mtcars,
    options = list(
      columnColor = c("mpg" = "red", "cyl" = "blue")
    )
  )
}

Text colors

changeColumnFontColorExample <<- function() {
  list(
    data = mtcars,
    options = list(
      textColor = c("hp" = "#1940C8", "drat" = "green")
    )
  )
}

Conditional Formatting

We can also edit cell text and background colors using conditional formatting. Note: cell colors take precedence over column background and text colors.

changeCellColorExample <<- function() {
  list(
    data = mtcars,
    options = list(
      cellColor = list("hp" = ifelse(mtcars$hp > 95, "red", 
                                     ifelse(mtcars$hp < 90, "blue", "green"))),
      cellBgColor = list("am" = ifelse(mtcars$am == 1, "green", "blue"))
    )
  )
}

JS should not over ride options specified by the R code in notebooks

Precedence: R notebook code > GUI setting > JS defaults