Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a colvis button #246

Open
trangdata opened this issue Jun 22, 2022 · 7 comments
Open

Adding a colvis button #246

trangdata opened this issue Jun 22, 2022 · 7 comments
Labels
enhancement New feature or request
Milestone

Comments

@trangdata
Copy link

Hi there, awesome package 💯 🚀 .

I know the CSV download button has just recently been implemented (#11), and I was wondering if reactable could also provide a colvis button similar to {DT} as well (which allows users to select the columns they want to be "visible"):

https://rstudio.github.io/DT/extensions.html#colvis-retired

@glin
Copy link
Owner

glin commented Jun 25, 2022

Hi, and yes, that's absolutely possible. There's actually an undocumented JavaScript API to do this today, which I didn't publicize earlier since I wasn't sure if it would be useful. So making that officially supported would be simple enough, and most of the effort would be coming up with useful examples.

Any thoughts on how you'd like users to select visible columns? Besides the ColVis style multi-select button popover thing, you could also use a checkbox group, or a single checkbox to toggle a fixed set of columns, or a button to toggle a fixed set of columns, etc.

There are so many different ways to do a column visibility UI, so like the CSV download button, reactable probably won't provide a ColVis button out of the box. But just provide a JavaScript API that makes it easy enough to make your own.

@glin glin added the enhancement New feature or request label Jun 25, 2022
@trangdata
Copy link
Author

Hi @glin thank you so much for the prompt response! I was thinking the ColVis style multi-select button popover may be the best option for allowing the user to select the columns they want to keep, but I'm not sure what a "checkbox group" would look like.

@mubashirqasim
Copy link

Hi Glin,

First of all, it's a great library. Thanks for all your good work to put this together!
I would also like to have the capability to control Column visibility.

Here is a good example from https://coinmarketcap.com/ to control column visibility.

image

Cheers,

@mubashirqasim
Copy link

Here is another excellent example from https://www.tipranks.com/smart-portfolio/holdings using the Custom View button.

image

@ulyngs
Copy link

ulyngs commented Oct 20, 2022

+1!

Having an easy way to let the user choose which columns they want to see, and reorder them (#270), are the only missing pieces that's causing me holding me back in reactable!

A simple colvis button similar to DT would suffice, alongside some sort of documented way to use crosstalk filters to do the same.

Can you point to the undocumented JavaScript API you mentioned?

@glin
Copy link
Owner

glin commented Nov 27, 2022

The JavaScript API methods to control column visibility are now in the development version:

There's a short demo of using this to create a column visibility toggle button at Column visibility toggle button example.

I didn't make an example of a ColVis-style multi-select button popover because it's much more complicated and would require custom JavaScript/CSS or another 3rd-party library, so left it out for now.

With native HTML inputs, you can create a group of checkbox inputs for individual column toggles, but opted for the simpler toggle button for the column visibility example. Here's the example I came up if you want to try checkboxes. I think it works ok but there's definitely room for improvement.

library(shiny)
library(htmltools)
library(reactable)

additionalColumns <- c("Passengers", "DriveTrain", "Cylinders", "EngineSize")

data <- MASS::Cars93[1:5, c("Manufacturer", "Model", "Type", "Price", additionalColumns)]

browsable(shiny::fluidRow(
  shiny::column(
    3,
    tags$fieldset(
      tags$legend("Show columns"),
      lapply(additionalColumns, function(name) {
        div(
          tags$label(
            tags$input(
              type = "checkbox",
              onclick = sprintf("Reactable.toggleHideColumn('cars-vis-table', '%s', !event.target.checked)", name)
            ),
            name
          )
        )
      })
    )
  ),
  shiny::column(
    9,
    reactable(
      data,
      columns = list(
        Passengers = colDef(show = FALSE),
        DriveTrain = colDef(show = FALSE),
        Cylinders = colDef(show = FALSE),
        EngineSize = colDef(show = FALSE)
      ),
      elementId = "cars-vis-table"
    )
  )
))

@glin glin added this to the v0.4.0 milestone Nov 27, 2022
@hannahkunde
Copy link

Thanks @glin for the two options. That already helped a lot.

I am now wondering if there is an easy way to have two (or more) buttons with which I can show/hide several columns at the same time. I already tried around with the Column visibility toggle button example, but cannot come to a solution, which satisfies me.

Do you have any recommendations for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants