Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

R - post select specific columns in an interactive datatable

$
0
0

I have a dataset that let's say has two columns. One column is for score and the other is for the actual value. I would like to colour the actual value column based on the score column, then remove the score column. I already did this but I can not remove the score column using datatable options. Remember, by datatable I mean DT::datatable() the interactive interface one.

## Sample df  ##

df = data.frame(
    actual = sample(1:10),
    score = runif(10)
)

----------

## Coloring breaks ##

brks <- quantile(df, probs = seq(.05, .95, .05), na.rm = TRUE)
clrs <- round(seq(255, 40, length.out = length(brks) + 1), 0) %>%
  {paste0("rgb(255,", ., ",", ., ")")}

----------

## Generating DT ##

datatable(df) %>% formatStyle(
  'actual', 'score',
  backgroundColor = styleInterval(brks, clrs))

More information here about styling:

DT Styling

Not applicable workaround

You can select columns first prior generating a datatable. i.e df %>% select(actual) %>% datatable(.) but that does not work in my case because I have to do the styling first.

Expected workaround

I'd like an option to specify which columns to select using DT::datatable() (the value column only here in this case), multiple columns in my real case.

datatable(df) 

There is an options called selected but it says it works for shiny. I have read about options and there is a select.item that might work, but I might be missing the correct syntax.


Viewing all articles
Browse latest Browse all 201867

Trending Articles