rdt

Rename columns within DT datatable function


It seems the API allows for renaming columns within options > columnDefs (see here). However, passing name as an argument doesn't seem to take. Does DT not support this or am I doing something wrong? I don't want to rename the columns upstream; I just want to know if I'm missing something in the documentation.

Here the searchable argument is used (the mpg column search is greyed out) but the name argument is not applied:

mtcars |>
  DT::datatable(
    filter = "top",
    options = list(
      columnDefs = list(
        list(targets = 1, name = "x", searchable = FALSE)
      )
    )
  )

enter image description here


Solution

  • Change name to title:

    mtcars |>
      DT::datatable(
        filter = "top",
        options = list(
          columnDefs = list(
            list(targets = 1, title = "x", searchable = FALSE)
          )
        )
      )
    

    The name option basically means "you can refer to this column as x" when using the DataTables API.

    The title option controls the display value shown in the column heading:

    The title of a column is shown in the header cell for that column. It may also be used for other interactions in extensions such as column visibility control, filtering and more.