-
Notifications
You must be signed in to change notification settings - Fork 13
Using DataTables in RCAP
This document explains how options can be set in RCAP for DataTable controls, including sparklines.
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.
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.
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
}
In DataTable, column widths must be specified as percentages.
columnWidthsExample <<- function {
list(
data = mtcars,
options = list(
columnWidths = c("50%", rep("5%", 10))
)
)
}
Both the font sizes for table header and table body can be specified.
fontSizeExample <<- function() {
list(
data = mtcars,
options = list(
thSize = "12px",
tdSize = "11px"
)
)
}
rightAlignExample <<- function() {
list(
data = mtcars,
options = list(
rightAlign = c("mpg", "wt")
)
)
}
Fixed number of decimal places
numericFormatExample <<- function() {
list(
data = data.frame(big = rnorm(100)*1000,
little = rnorm(100)),
options = list(
decimalPlaces = 3))
}
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.
changeColumnColorsExample <<- function() {
list(
data = mtcars,
options = list(
columnColor = c("mpg" = "red", "cyl" = "blue")
)
)
}
changeColumnFontColorExample <<- function() {
list(
data = mtcars,
options = list(
textColor = c("hp" = "#1940C8", "drat" = "green")
)
)
}
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
RCAP - RCloud powered dashboards and websites