Search code examples
rreactable

Customizing Font/Styles in "Reactable" Tables


I am following this tutorial here : https://glin.github.io/reactable/articles/examples.html#language-options - I am trying to make an interactive table. I copy/pasted the following code below and it successfully made an interactive table:

  library(reactable)
    
data <- MASS::Cars93[, c("Manufacturer", "Model", "Type", "Price")]

 reactable(
    data,
    columns = list(
        Manufacturer = colDef(
            filterable = TRUE,
            # Filter by case-sensitive text match
            filterMethod = JS("function(rows, columnId, filterValue) {
        return rows.filter(function(row) {
          return row.values[columnId].indexOf(filterValue) !== -1
        })
      }")
        )
    ),
    defaultPageSize = 5
)

enter image description here

My only question is that the font/style of my output looks different than the tutorial (I just grabbed a screenshot for one of the tables from the tutorial, the font/style for all the tables is the same):

enter image description here

  • Does anyone know why my font/style does not match the ones in the tutorial and is there some default option I can specify to fix this?

Solution

  • The issue is that the examples vignette is created via RMarkdown which will add some default CSS styling rules including the font family to be used for the document.

    When running the same code from within R this not the case. i.e. you get a more or less plain HTML table with almost no CSS rules and no font family. As a result the browser or the RStudio Viewer or ... will use it's default font family to display the HTML output.

    Hence, to get the same fonts as in the examples vignette you could run your code from within an Markdown document:

    ---
    title: "Untitled"
    output: html_document
    date: "2022-09-13"
    ---
    
    ```{r}
    library(reactable)
    
    data <- MASS::Cars93[, c("Manufacturer", "Model", "Type", "Price")]
    
    reactable(
      data,
      columns = list(
        Manufacturer = colDef(
          filterable = TRUE,
          # Filter by case-sensitive text match
          filterMethod = JS("function(rows, columnId, filterValue) {
            return rows.filter(function(row) {
              return row.values[columnId].indexOf(filterValue) !== -1
            })
          }")
        )
      ),
      defaultPageSize = 5
    )
    ```
    

    enter image description here

    A second option would be to set the font family when running from an RScript via the theme argument as in the theming example:

    library(reactable)
    
    data <- MASS::Cars93[, c("Manufacturer", "Model", "Type", "Price")]
    
    reactable(
      data,
      columns = list(
        Manufacturer = colDef(
          filterable = TRUE,
          # Filter by case-sensitive text match
          filterMethod = JS("function(rows, columnId, filterValue) {
            return rows.filter(function(row) {
              return row.values[columnId].indexOf(filterValue) !== -1
            })
          }")
        )
      ),
      defaultPageSize = 5,
      theme = reactableTheme(
        style = list(fontFamily = "-system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif")
      )
    )
    

    enter image description here