I am using DT::datatable in a Flexdashboard to provide some monthly KPIs for about 100 different countries. Five of them are of special interest to some of the dashboard’s users, so I am searching for a solution to easily filter on those countries.
My idea was to generate a button next to the Export Buttons that will filter the data on only those five rows. Clicking it again would most perfectly show the original table again. I found that there is a possibility to specify custom buttons but still I have no clue how to tackle my problem with this.
Here is a tiny example of the table I get so far:
# Random Data Frame
df <- data.frame(Country = paste("Country", 1:100, sep = "_"),
Revenue = rnorm(n = 100, mean = 5000, sd = 2000))
# Data Table used in Dashboard
datatable(df, class = "hover", rownames = FALSE , extensions = 'Buttons', options = list(
pageLength = 5,
responsive = TRUE,
dom = 'Bftip',
buttons = c('copy', 'csv'),
columnDefs = list(list(className = 'dt-center', targets = "_all"))
)) %>% formatCurrency(columns = "Revenue")
Below is a reproducible example in Shiny as I do not think what you are trying to do is feasible in a static document. I assume you have set runtime: shiny
.
library(DT)
library(shiny)
countries <- data.frame(
cns = LETTERS,
value = runif(26, 1, 4)
)
TOP5 <- c("A", "B", "X", "Y", "Z")
ui <- fluidPage(
actionButton("filter", "Filter countries of interest"),
DTOutput("table")
)
server <- function(input, output, session) {
output$table <- renderDT({
sel <- if(input$filter %% 2 == 0) countries$cns else TOP5
countries %>%
filter(cns %in% sel) %>%
datatable()
})
}
shinyApp(ui, server)