Putnik
Putnik

Reputation: 45

How to Implement DataTables Option in Shiny R syntax?

I am trying to add an option for DataTable in Shiny using some of the expanded options that are found in DataTables.

I want to implement the opetion SearchBuilder.columns so that the search box can only search in the "id" column https://datatables.net/reference/option/searchBuilder.columns

How does one implement this option into R Shiny? What is the syntax?

The code below did not work.

output$table_pred <- DT::renderDataTable(df, options = list(pageLength =5), searchBuilder.columns = df$id)

Here is the full code:

library(shinythemes)
library(shiny)
library(DT)

setwd("c:/Desktop/datasets/")

df <- read.csv("prediction_data.csv")

df2 <- read.csv("test_data.csv")



ui <- fluidPage(

  fluidRow(
    column(12,
           dataTableOutput('table_pred')
    )
  ),
  fluidRow(
    column(12,
           dataTableOutput('table_test')
    )
  )
  
)


server <- function(input, output, session) {
  #rendering the datatable for rediction data

  output$table_pred <- DT::renderDataTable(df, options = list(pageLength =5), searchBuilder.columns = df$id)
  
  output$table_test <- DT::renderDataTable(df2,options = list(pageLength =10))
  
}

shinyApp(ui, server)

enter image description here

Upvotes: 1

Views: 601

Answers (2)

passer_days
passer_days

Reputation: 11

I know this is old, but just adding since I came across this in my own research and also looked to searchBuilder as a solution. Following the solution here, I made only one column available for searching, which made it so that the global search functionality was also limited to just that one column. It does leave a search bar above that column, but for my use case this was the most direct and simplest solution I found without having to install anything new.

Upvotes: 0

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

Awesome extension!

enter image description here

It is not available in the 'DT' package. Here is how you can use it.

Firstly, download the JavaScript file and the CSS file.

Then, here is the R code:

library(DT)
library(htmltools)

dat <- data.frame(
  x = c(0, 1, 2, 3, 4),
  id = c("sub0", "sub0", "sub1", "sub1", "sub2")
)

dtable <- datatable(
  dat,
  options = list(
    dom = "Qlfrtip",
    searchBuilder = list(
      columns = list(2) # 2 is the index of the 'id' column
    )
  )
)

path_to_searchBuilder <- # path to the folder containing the two searchBuilder files
  normalizePath("~/Work/R/DT/searchBuilder/")

dep <- htmlDependency(
  name = "searchBuilder",
  version = "1.0.0", 
  src = path_to_searchBuilder,
  script = "dataTables.searchBuilder.min.js",
  stylesheet = "searchBuilder.dataTables.min.css",
  all_files = FALSE
)

dtable$dependencies <- c(dtable$dependencies, list(dep))

dtable

Upvotes: 2

Related Questions