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

How to manage dynamically-appearing columns within the columns list? #65

Closed
JoshADHD opened this issue Jul 31, 2020 · 2 comments
Closed

Comments

@JoshADHD
Copy link

Currently, I have a table that I pivot_wider to reveal the same statistic, but at different points in time. The number of time-points changes dynamically depending on the statistic. I'd like to be able to manage the list of colDef() calls programatically to handle these "new" columns as they appear. Many of the columns are static, but the time-points in particular can be a single point, or upwards of five/six points.

My attempt at this now is:

round_cols <- grep("rd_", colnames(temp), value = TRUE)

round_colDefs <- round_cols %>% seq_along() %>% map(~ {
    colDef(name = paste("round", .x), ...)
})

names(round_colDefs) <- round_cols

round_colDefs <- unlist(round_colDefs, recursive = FALSE)

Then, in the columns argument of reactable()

columns = list(
    name = colDef(name = "player", ...),
    rank = colDef(),
    round_colDefs,
    country = colDef(name = "home", ...)
)

This is returning an error that I'm not providing a named list of colDef() functions.

Any insight into how I might tackle this?

@JoshADHD JoshADHD changed the title How do manage dynamically-appearing columns within the columns list? How to manage dynamically-appearing columns within the columns list? Jul 31, 2020
@glin
Copy link
Owner

glin commented Aug 1, 2020

Hi,

Is round_colDefs a named list? (could you provide a runnable example?)

If so, you can programmatically create named lists by combining them with c(). For example:

library(reactable)

# Separate named list of colDefs to add. Could be an empty list as well.
extra_columns <- list(
  cyl = colDef(name = "CYL"),
  disp = colDef(name = "DISP")
)

# Method 1: this won't work due to "`columns` must be a named list of column definitions"
# because extra_columns is added as a nested list
reactable(
  mtcars,
  columns = list(
    mpg = colDef(name = "MPG"),
    extra_columns,
    hp = colDef(name = "HP")
  )
)

# This is equivalent to:
reactable(
  mtcars,
  columns = list(
    mpg = colDef(name = "MPG"),
    list(
      cyl = colDef(name = "CYL"),
      disp = colDef(name = "DISP")
    ),
    hp = colDef(name = "HP")
  )
)

#####

# Method 2: this works because c() combines and flattens the named lists
reactable(
  mtcars,
  columns = c(
    list(mpg = colDef(name = "MPG")),
    extra_columns,
    list(hp = colDef(name = "HP"))
  )
)

# This is equivalent to:
reactable(
  mtcars,
  columns = list(
    mpg = colDef(name = "MPG"),
    cyl = colDef(name = "CYL"),
    disp = colDef(name = "DISP"),
    hp = colDef(name = "HP")
  )
)

@JoshADHD
Copy link
Author

Thank you, this is exactly the solution I needed!

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

No branches or pull requests

2 participants