Learn R Programming

basictabler (version 0.3.1)

BasicTable: A class that defines a basic table.

Description

The BasicTable class represents a table with styling and formatting that can be rendered to multiple output formats.

Arguments

Value

Object of R6Class with properties and methods that define a basic table.

Format

R6Class object.

Fields

argumentCheckMode

A number (0-4 meaning none, minimal, basic, balanced, full) indicating the argument checking level.

traceEnabled

A logical value indicating whether actions are logged to a trace file.

rowCount

The number of rows in the table.

columnCount

The number of columns in the table.

cells

A TableCells object containing all of the cells in the body of the table.

theme

The name of the theme currently applied to the table.

styles

A TableStyles object containing the styles used to theme the table.

allowExternalStyles

Enable support for external styles, when producing content for external systems.

allTimings

The time taken for various activities related to constructing the table.

significantTimings

The time taken for various activities related to constructing the table, where the elapsed time > 0.1 seconds.

Methods

Documentation

For more complete explanations and examples please see the extensive vignettes supplied with this package.

new(argumentCheckMode="auto", theme=NULL, replaceExistingStyles=FALSE, tableStyle=NULL, headingStyle=NULL, cellStyle=NULL, totalStyle=NULL, compatibility=NULL, traceEnabled=FALSE, traceFile=NULL)

Create a new table, optionally specifying the name of a built in theme or CSS style declarations for the different cells within the table.

addData(dataFrame=NULL, columnNamesAsColumnHeaders=TRUE, explicitColumnHeaders=NULL, rowNamesAsRowHeaders=FALSE, firstColumnAsRowHeaders=FALSE, explicitRowHeaders=NULL, columnFormats=NULL, baseStyleNames=NULL)

Generate the table from a data frame, specifying headers and value formatting.

addMatrix(matrix=NULL, columnNamesAsColumnHeaders=TRUE, explicitColumnHeaders=NULL, rowNamesAsRowHeaders=FALSE, explicitRowHeaders=NULL, columnFormats=NULL, baseStyleNames=NULL)

Generate the table from a matrix, specifying headers and value formatting.l

mergeCells(rFrom, cFrom, rSpan=NULL, cSpan=NULL, rTo=NULL, cTo=NULL)

Merge cells in the table. This does not delete the other cells covered by the merged cell. When the table is output, the top-left most cell in the merged cell range is rendered over the other cells (which are effectively hidden).

unmergeCells(r, c, errorIfNotFound=TRUE)

Delete a merged cell range by specifying any of the cells covered by the merged cell.

applyCellMerges()

Updates the isMerged, isMergeRoot and mergeIndex properties of the cells.

formatValue(value=NULL, format=NULL)

Format a value for display, using either sprintf(), format() or a custom formatting function.

addStyle(styleName, declarations)

Define a new TableStyle and add it to the TableStyles collection.

createInlineStyle(baseStyleName, declarations)

Create a TableStyle object that can be used to style individual cells in the table.

setStyling(rFrom=NULL, cFrom=NULL, rTo=NULL, cTo=NULL, cells=NULL, baseStyleName=NULL, style=NULL, declarations=NULL)

Set the style settings across a range of cells.

resetCells()

Clear the cells of the table.

getCells(specifyCellsAsList=TRUE, rowNumbers=NULL, columnNumbers=NULL, cellCoordinates=NULL)

Retrieve cells by a combination of row and/or column numbers.

findCells(rowNumbers=NULL, columnNumbers=NULL, minValue=NULL, maxValue=NULL, exactValues=NULL, includeNull=TRUE, includeNA=TRUE)

Find cells in the body of the table matching the specified criteria.

print(asCharacter=FALSE)

Either print the table to the console or retrieve it as a character value.

asMatrix(firstRowAsColumnNames=FALSE, firstColumnAsRowNames=FALSE, rawValue=FALSE)

Gets the table as a matrix, with or without headings.

asDataFrame(firstRowAsColumnNames=FALSE, firstColumnAsRowNames=FALSE, rawValue=FALSE, stringsAsFactors)

Gets the table as a data frame, with or without headings.

getCss(styleNamePrefix)

Get the CSS declarations for the entire table.

getHtml(styleNamePrefix)

Get the HTML representation of the table, specifying the CSS style name prefix to use.

saveHtml(filePath, fullPageHTML=TRUE, styleNamePrefix)

Save the HTML representation of the table to a file.

renderTable(width, height, styleNamePrefix)

Render the table as a htmlwidget.

writeToExcelWorksheet(wb=NULL, wsName=NULL, topRowNumber=NULL, leftMostColumnNumber=NULL, mapStylesFromCSS=TRUE)

Output the table into the specified workbook and worksheet at the specified row-column location.

asList()

Get a list representation of the table.

asJSON()

Get a JSON representation of the table.

viewJSON()

View the JSON representation of the table.

Examples

Run this code
# NOT RUN {
# The package vignettes have many more examples of working with the
# BasicTable class.
# Quickly rendering a table as an htmlwidget:
library(basictabler)
qhtbl(data.frame(a=1:2, b=3:4))
# Rendering a larger table as an htmlwidget:
library(basictabler)
library(dplyr)
tocsummary <- bhmsummary %>%
  group_by(TOC) %>%
  summarise(OnTimeArrivals=sum(OnTimeArrivals),
            OnTimeDepartures=sum(OnTimeDepartures),
            TotalTrains=sum(TrainCount)) %>%
  ungroup() %>%
  mutate(OnTimeArrivalPercent=OnTimeArrivals/TotalTrains*100,
         OnTimeDeparturePercent=OnTimeDepartures/TotalTrains*100) %>%
  arrange(TOC)

tbl <- BasicTable$new()
columnHeaders <- c("TOC", "On-Time Arrivals", "On-Time Departures",
  "Total Trains", "On-Time Arrival %", "On-Time Departure %")
columnFormats=list()
columnFormats[[2]] <- list(big.mark=",")
columnFormats[[3]] <- list(big.mark=",")
columnFormats[[4]] <- list(big.mark=",")
columnFormats[[5]] <- "%.1f"
columnFormats[[6]] <- "%.1f"
tbl$addData(tocsummary, columnNamesAsColumnHeaders=FALSE,
  firstColumnAsRowHeaders=TRUE,
  explicitColumnHeaders=columnHeaders, columnFormats=columnFormats)
tbl$renderTable()
# }

Run the code above in your browser using DataLab