【问题标题】:How to Implement DataTables Option in Shiny R syntax?如何在 Shiny R 语法中实现 DataTables 选项?
【发布时间】:2021-02-22 16:01:01
【问题描述】:

我正在尝试使用 DataTables 中的一些扩展选项在 Shiny 中为 DataTable 添加一个选项。

我想实现选项 SearchBuilder.columns 以便搜索框只能在“id”列中搜索 https://datatables.net/reference/option/searchBuilder.columns

如何在 R Shiny 中实现这个选项?语法是什么?

下面的代码不起作用。

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

这里是完整的代码:

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)

【问题讨论】:

    标签: r shiny datatables dt datatables-1.10


    【解决方案1】:

    很棒的扩展!

    它在“DT”包中不可用。以下是您可以使用它的方法。

    首先,下载the JavaScript file and the CSS file

    那么,这里是R代码:

    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
    

    【讨论】:

    • 这真的很酷。我将尝试将其实现到我闪亮的应用程序中。感谢您清理 DataTable 的选项
    猜你喜欢
    • 2021-08-31
    • 2021-11-29
    • 2015-07-30
    • 2019-10-01
    • 1970-01-01
    • 2021-05-05
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多