0

So I have a situation where I would like to use an object in a tag$button(). This is to work around the fact that the columns may change.

It works on the first click and then stops working and the whole table disappears on subsequent clicks.

library(htmltools)

data <- MASS::Cars93[1:5, c("Manufacturer", "Model", "Type", "Price",
                            "Passengers", "DriveTrain", "Cylinders", "EngineSize")]

hidden_cols <- c('Passengers', 'DriveTrain', 'Cylinders', 'EngineSize')

htmltools::browsable(
  tagList(
    tags$button(
      "Show/hide more columns",
      onclick = "Reactable.setHiddenColumns('cars-vis-table', prevColumns => {
        return prevColumns.length === 0 ? [hidden_cols] : []
      })"
    ),
    reactable(
      data,
      columns = list(
        Passengers = colDef(show = FALSE),
        DriveTrain = colDef(show = FALSE),
        Cylinders = colDef(show = FALSE),
        EngineSize = colDef(show = FALSE)
      ),
      elementId = "cars-vis-table"
    )
  )
)

1 Answer 1

0

figured it out!

Just needed to use paste0() and paste() function and collapse. Basically just using paste to replicate a printed list:

onclick = paste0("Reactable.setHiddenColumns(
  'cars-vis-table', prevColumns => {
    return prevColumns.length === 0 ? ['",paste(hidden_cols, collapse = "','"),"'] : [] 
  })" 
)

Not the answer you're looking for? Browse other questions tagged or ask your own question.