huxtable (version 4.5.0)

rowspecs: Different ways to select rows and columns

Description

This help page describes how to use the row and col arguments in set_* functions.

Arguments

The basics

The set_* functions for cell properties all have arguments like this: set_property(ht, row, col, value, byrow = FALSE).

You can treat row and col arguments like arguments for data frame subsetting. For example, you can use row = 1:3 to get the first three rows, col = "salary" to specify the column named "salary", or row = ht$salary >= 50000 to specify rows where a condition is true.

There are also a few extra tricks you can use:

  • Write set_property(ht, x), omitting row and col, to set the property to x for all cells.

  • Use everywhere to refer to all rows or all columns.

  • Use final(n) to refer to the last n rows or columns.

  • Use evens to get only even rows/columns and odds for only odd ones.

  • Use every(n, from = m) to get every nth row/column starting at row/column m.

  • Use dplyr functions like starts_with, contains and matches to specify columns (but not rows). See select_helpers for a full list.

  • Set byrow = TRUE to set properties by row rather than by column.

The gory details

How the row and col arguments are parsed depends on the number of arguments passed to the set_* function.

  • If there are two arguments (excluding byrow) then the second argument is taken as the value and is set for all rows and columns.

  • If there are four arguments:

    • If row or col is numeric, character or logical, it is evaluated just as in standard subsetting. col will be evaluated in a special context provided by tidyselect::with_vars() to allow the use of dplyr functions.

    • If row or col is a function,it is called with two arguments: the huxtable, and the dimension number being evaluated, i.e. 1 for rows, 2 for columns. It must return a vector of column indices. evens(), odds(), every() and final() return functions for this purpose.

Examples

Run this code
# NOT RUN {
set_bold(jams, 2:4, 1:2, TRUE)
set_background_color(jams, evens, everywhere,
      "grey95")
set_bold(jams, everywhere,
      tidyselect::matches("yp"), TRUE)

set_text_color(jams, 2:4, 1:2,
      c("red", "violetred", "purple"))
# }

Run the code above in your browser using DataCamp Workspace