Skip to contents

[Experimental] Built on top of the tip engine, and experimental: the API may change in upcoming releases. Gives the rows of a table made by DT::datatable() or reactable::reactable() a tip drawn from the row's own values, and optionally a tip of their own to chosen cells and header cells. The tips follow the data row, so sorting, filtering and paging need no code of yours: each row is its own record, whatever place it has on the screen.

Usage

tip_table(
  widget,
  ...,
  rows = NULL,
  cells = NULL,
  headers = NULL,
  style = NULL,
  placement = NULL,
  marker = NULL,
  id = NULL,
  max_pins = NULL
)

Arguments

widget

A table made by DT::datatable() or by reactable::reactable().

...

Must be empty. It makes a misspelt argument an error.

rows

One string, tip_template() or tip_spec() for every row, or NULL. NA and "" are no tip. A tip_spec() here may also set pin and events. A tip_template() is given as an object: the name of a template made with tip_define() stops with an error that says so.

cells

A named vector or list, one entry per column (by the column's name; a name given twice is an error), for the cells of that column. Each entry is a string, a tip_template() or a tip_spec(); NA and "" give that column no tip. Use a list to mix them: list(status = tip_spec("{{ note }}", placement = "right"), wins = "{{ wins }} wins").

headers

The same for the header cells; a header has no row to read from, so a tip_spec() here holds words or a tip_template() with no placeholders.

style

How every tip in the table looks, or NULL (the default) to take the page's: whatever style takes in tip_on() (a preset name, a tip_style(), a tip_theme() or a bslib::bs_theme()). A tip_spec() with its own style beats it for that target. A row's own style also reaches the cells in that row that have none of their own, because the tip engine reads the nearest tagged element. A list of CSS variables (tip_style(theme = list("--x" = ...))) is for the whole page, so a table refuses it with an error: give it to use_hovercraft(), or use the roles of tip_theme().

placement

Where the tips show: "auto", "top", "bottom", "left" or "right", or NULL for the page's choice. A tip_spec() with its own placement beats it. A cell's tip follows this, not its row's.

marker

The visible cue on every cell and header cell that has a tip, so a reader can see where the tips are: "corner" (a small triangle in the top corner, as a spreadsheet shows a note), or NULL or "none" for no marker (the default). A tip_spec() with its own marker beats it. A row takes no marker, and a table draws only the corner for now: "underline", "icon" and an icon of your own stop with an error. A cell with a marker is highlighted while its tip is open. See tip_marker().

id

The id this table's Shiny inputs hang on (input$<id>_pin), and the base of its tips' ids: one non-empty string with no whitespace and no :. It is the table's id, not one tip's. Inside renderDT() or renderReactable() in a module's server function, the plain id is enough: tip_table() adds the module's prefix from the session the table is drawn in, as tip_show() does, so id = "scores" in module "mod1" is input[["mod1-scores_pin"]]. In a module's UI function, or anywhere else with no session (a static page), nothing adds the prefix: give ns("scores") there. An id that already has the prefix (session$ns("scores")) is left alone. Give it a different id from every other tip source on the page: two tables with one id make the page warn in the browser console. With an id, every marked row, cell and header cell gets its own tip id derived from it, which follows the data row through sorting, filtering and paging; how that id is built is not part of the API: tip_show(), tip_hide() and tip_update() take the table's id with row and column instead. NULL gives the tips no id. A pin or events given to rows (see tip_spec()) need an id.

max_pins

[Experimental] How many of this table's rows can be pinned at once, a whole number from 1 to 10, or NULL (the default). NULL counts the table's pins in the app's limit (use_hovercraft(max_pins =)): a row pin can close a pin elsewhere, and the other way round. A number gives the table its own count, outside the app's: a row pinned past it closes the table's oldest pin and no other, and a pin elsewhere never closes one of the table's rows. It needs a rows spec that pins (tip_spec() with pin = TRUE) and the table's id. All the pins open together, the app's and every table's, cannot pass 10, the engine's cap. On a viewport narrower than 600 pixels the engine keeps one pin at a time, whatever the counts say.

Value

The widget, with the tips added.

Details

Known limits: row is the position in the data the table has now, so a pin follows the row number after replaceData() or a redraw; a DT table in server mode has only the rows of the current page on the screen; and the table's script does some work for each row on the screen on every change of the data, so page a long table. tip_table() reads the table's data once in R (for a reactable table it parses all the rows) to check the columns and values the tips name, so its R time grows with the rows.

A cell's tip wins over its row's, and a row's tip opens at the pointer. The tip of a cell or a header cell points at its text, not at the whole cell, so a right-aligned number in a wide column, or a right-aligned header, gets a tip beside the text and not far to its left; there is no argument for it. A target that needs its own settings (style, placement, marker or more) takes a tip_spec() in place of words. A tip is made of words (markdown) with {{ column }} placeholders, or of a tip_template(); every placeholder is checked against the table's columns, and a miss lists them. Placeholders that start with a dot are kept for hovercraft. A header tip has no row to read from, so it takes words, or a tip_template() with no placeholders.

Tips also show for the keyboard. A row, a cell or a header cell cannot take focus by itself, so tip_table() gives the table one tab stop, and the arrow keys move between the rows, cells and header cells that have a tip: left and right inside a row (the row first, then its cells), up and down between rows to the same column, Home and End to the ends of the row, and Ctrl with Home or End to the ends of the table. The tip shows for what has focus. A header that its widget already makes focusable (the sortable headers of DT) keeps its own tab stop. Enter and Space do not select a row. A click on a row selects it in both widgets, so a row's tip does not pin on click.

Pins: with tip_spec(pin = TRUE) as rows, a reader can keep a row's tip open. The tip that shows on hover or focus gets a small pin handle in its corner, the same as tip_on()'s (pin = TRUE): a click or tap on it pins the row's tip, and the tip's own close button, Escape or a click away closes it (a click away does not close a panel that opens in place, see below). The pin key P pins the tip of the row that has focus or that the pointer rests on, or of the row a focused cell belongs to; a cell or a header cell has no pin of its own ("only rows pin for now"), so P while the pointer rests on a cell that has a tip of its own pins nothing. A long form (more) opens with its "More" button, or with the M key when the focus is on the row, a cell or a header cell, or the pointer rests on it. A tap on a row shows its tip and a tap on the handle pins it. The handle is not a column and not a tab stop, so the table keeps its one tab stop and its columns; keyboard readers use the key, which each row that can pin announces as aria-keyshortcuts. A pin follows its row. It stays on a row when DT sorts (client and server mode), and on the same row number when DT draws the table again or DT::replaceData() or reactable::updateReactable(data = ) give it new data, with the new values in its tip. It closes when its row leaves the page (a filter or another page in DT, another page in reactable), when reactable shows another row in the row's place, when the table goes, and when a click lands elsewhere. The input then says why: DT closes a pin of a row that left the page itself (reason "gone", by "data"); reactable keeps its row nodes and gives them other rows, so the layer closes the pin through the tip engine's host command (reason "host", by "host", via "command"). A server that acts on by == "data" sees the two widgets differently.

Shiny inputs

tip_spec(events = ) on rows turns on inputs that belong to the table, one per event and not one per row, named after the table's id: input$<id>_hint, input$<id>_pin and input$<id>_copy. Each is NULL until its first event. Its value is the event that just happened, with the fields of tip_on()'s (state, text, by, via and reason), and says which tip it was:

  • target: "row" ("cell" and "header" are kept for later; only rows send events for now);

  • row: the row's place in the data the widget has now, counted from 1, in client mode and in server mode alike, and not the row's place on the screen: after a sort, the third data row is still 3. It is NULL for a header. After DT::replaceData(), reactable::updateReactable(data = ) or a re-render with other data, row 3 is whatever is third now, so do not keep it as a record's id;

  • column: the data column's name (the name in the data frame, not the header's text), or NULL for a row;

  • id: the tip's id. How it is written is not part of the API.

input$<id>_pinned (with events = "pin") is what is pinned now: a list of such addresses (target, row, column, id), the oldest pin first. It is NULL until the first pin event, and an empty list once nothing is pinned. It is sent in the same batch as _pin, and a value equal to the one before does not fire an observer again. Read it with length(input$scores_pinned): it holds as many addresses as the table has pins open (see max_pins). For a tag of tip_on() with events = "pin", input$<id>_pinned is TRUE or FALSE (read it with isTRUE(input$run_pinned)).

input$<id>_pin and input$<id>_hint are told apart by state, whose values may grow in a later version. A pin's state is "pinned", "closed" or "updated" (the tip is pinned and was drawn again because its words changed: an app that only tests for "closed" should not take it for a pin); a hint's is "shown" or "hidden". The values of by are "reader", "data" (the page changed under the tip) and "host" (a command of the page did it). The values of reason (the same for the inputs of tip_on()) are, for a pin, "select" (the same element again), "escape", "button" (the tip's close button), "outside" (a click away), "replaced" (another tip was pinned), "gone" (the row left the page), "below" (a layer around it closed), "host" (a command of the page closed it) and "redraw" (the tip was drawn again with other host slots, by = "data"); for a hint, "leave" (the pointer left), "blur" (the focus left), "escape", "outside", "pinned" (the hint became a pin), "replaced", "gone" and "host". reason is NULL when a tip opens.

In place

[Experimental] With tip_spec(to = "in_place", pin = TRUE) as rows (and the table's id), a row's pinned tip is drawn as a panel under its row, as wide as the table's visible part, in DT and in reactable. The hover tip still floats. The pin handle or the pin key opens it, and it holds what any pinned tip holds: its blocks, the long form with its "More" button, a tip_output(). The widget's own inputs (rows_selected, cell_clicked) are not touched by a click in it.

It follows the pin: DT moves the panel with its row when the table sorts, and puts it back after every draw, in server mode too; a filter or another page that takes the row off the page closes it (reason "gone"). DT::replaceData() keeps it on the same row number. reactable closes it when it shows other rows in the row's node (reason "host"), as for a floating pin, and keeps it when it is given new data with updateReactable(data = ). A server-mode DT drawn again by renderDT() closes it, as it does a floating pin. A panel counts in use_hovercraft(max_pins = ) and tip_table(max_pins = ) like any pin: the oldest closes (reason "replaced") and its panel goes with it.

A click away does not close a panel: it closes by its close button, Escape, the pin limit, or its row going. The pin key on a row whose panel is open does nothing. Escape on a row that has no pin of its own closes the newest pin, as for floating pins. With scrollX the panel stays at the left edge of the visible part. The FixedColumns extension of DT (it copies the rows) stops with an error; responsive and RowGroup of DT, reactable's groupBy, and screen readers are not covered. A click inside a DT panel is stopped at the panel's row, so a handler that the page delegates on document does not see it.

What works, each checked in a browser:

  • DT, in client mode and in server mode under Shiny, with scrollX, scrollY, fixedHeader, colReorder and responsive. With responsive, a column folded into the details row has no tip there; the row's tip is unchanged.

  • reactable, with sorting, filtering, paging, row selection (a click selects the row and its tip stays), groupBy (the rows inside a group have tips, the group's own row has none), details (the details panel has none) and filterable = TRUE (the tip of a header cell stays on the header cell; the filter box under it has no tip and is not described twice). DT's filter row has none either.

  • An id inside a module: give the plain id inside renderDT() or renderReactable(), and ns() in the module's UI function (see id).

  • Tables that Shiny updates with new data: DT::replaceData() (it works in server mode) with the user's own data frame, and reactable::updateReactable(data = ). The tips follow the new rows, as they do after sorting, filtering and paging.

Not supported: a DT table with its own createdRow or initComplete option; a reactable column whose headerClass is a JavaScript function; any other widget.

Large tables: tip_table() adds nothing to the data of the page, since each tip reads its values from the table's own data in the browser. A reactable row carries the values its tips read in its class, as well as in the engine's attribute, so they sit twice in the page for every row that is drawn: page the table. A table with every row drawn at once is slow in DT when cells or headers have tips (a table of 2,000 rows with a row tip and three cell tips took about a second to draw, five times as long as without tips; thousands of rows take seconds); page it, or tip its rows only.

Give tip_table() the finished widget, last in the pipe or at least after the columns are settled. DT::datatable() keeps its columns and its inputs as they are: tip_table() adds no column and changes no event (a table with its own createdRow or initComplete option is refused for now). reactable::reactable() gets its own rowClass, class and headerClass added to yours; the class of a row holds the values its tips read, so it changes with the data.

Examples

scores <- data.frame(team = c("Owls", "Foxes"), wins = c(7, 5), losses = c(3, 5))
DT::datatable(scores) |>
  tip_table(
    rows = tip_spec(
      "**{{ team }}**: {{ wins | number:0 }} wins, {{ losses | number:0 }} losses",
      pin = TRUE,
      events = "pin"
    ),
    headers = c(wins = "Games won this season"),
    id = "scores_tips"
  )