Home > OS >  How to set table filter in R?
How to set table filter in R?

Time:06-18

I knit my rmd file into HTML and in order to put my table in HTML file, I use the kableExtra function. Now, I wonder if it's possible to add a filter to this table (like in Excel) to make it more interactive and let user filter some parameters itself?

df <- data.frame (origin = c("A","B","C","D","E","F","G","H","I","J"),
              Percentage = c(23,16,32,71,3,60,15,21,44,60),
              rate = c(10,12,20,200,-25,12,13,90,-105,23),
              change = c(10,12,-5,12,6,8,0.5,-2,5,-2))

library(kableExtra)
df %>%
  kbl() %>%
  kable_material(c("striped", "hover"))

CodePudding user response:

You may have better luck with filtering and other interaction using DT::datatable()

df %>% DT::datatable(rownames=F, filter="top")

There are lots of options; for example, setting font size:

df %>% DT::datatable(rownames=F, filter="top") %>% 
  DT::formatStyle(columns = colnames(df), fontSize = '150%', color = 'red')

For full treatment of options, see rstudio.github.io/DT/options.html and other documentation sources

  • Related