在 R 中使用模糊搜索和 Shiny:综合指南
P粉924915787
P粉924915787 2024-04-01 14:09:01
0
1
402

我正在尝试在 DT 数据表中使用此 JS 脚本(来自此网站:https://datatables.net/blog/2021-09-17):

var fsrco = $('#fuzzy-ranking').DataTable({
    fuzzySearch: {
        rankColumn: 3
    },
    sort: [[3, 'desc']]
});
 
fsrco.on('draw', function(){
    fsrco.order([3, 'desc']);
});

使用此脚本标记:

“//cdn.datatables.net/plug-ins/1.11.3/features/fuzzySearch/dataTables.fuzzySearch.js”

我想将其合并到 Shiny 应用程序中的 DT 数据表函数中,其中使用排名顺序应用模糊搜索(顶部相似度较高),但是,我不希望显示排名列。

类似这样,但不显示排名列。

一些基本的常规示例:

    library(shiny)
    library(DT)
    js <- c(
      "  var fsrco = $('#fuzzy-ranking').DataTable({",
      "    fuzzySearch: {",
      "        rankColumn: 3",
      "    },",
      "    sort: [[3, 'desc']]",
      "});",
      "fsrco.on('draw', function(){",
      "    fsrco.order([3, 'desc']);",
      "});"
)
    ui <- fluidPage(
      DTOutput("table")
    )
    server <- function(input, output, session){
      output[["table"]] <- renderDT({
        datatable(
          iris,
          selection = "none",
          editable = TRUE, 
          callback = JS(js),
          extensions = "KeyTable",
          options = list(
            keys = TRUE,
            url = "//cdn.datatables.net/plug-ins/1.11.3/features/fuzzySearch/dataTables.fuzzySearch.js"
          )
        )
      })
    }
    shinyApp(ui, server)

P粉924915787
P粉924915787

全部回复(1)
P粉310931198

这个插件是一个旧插件,它不适用于最新版本的 DataTables。

但是我们可以采用计算相似度的 JavaScript 函数,并通过 SearchBuilder 扩展在自定义搜索中使用它。

首先,复制此 JavaScript 代码并将其保存在名称 levenshtein.js 下:

/*
BSD 2-Clause License

Copyright (c) 2018, Tadeusz Łazurski
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

function levenshtein(__this, that, limit) {
  var thisLength = __this.length,
    thatLength = that.length,
    matrix = [];

  // If the limit is not defined it will be calculate from this and that args.
  limit = (limit || (thatLength > thisLength ? thatLength : thisLength)) + 1;

  for (var i = 0; i  (limit || 100)) {
    return prepare(limit || 100);
  }
  if (thisLength === 0) {
    return prepare(thatLength);
  }
  if (thatLength === 0) {
    return prepare(thisLength);
  }

  // Calculate matrix.
  var j, this_i, that_j, cost, min, t;
  for (i = 1; i  4) return prepare(thisLength);

      that_j = that[j - 1];
      cost = this_i === that_j ? 0 : 1; // Step 5
      // Calculate the minimum (much faster than Math.min(...)).
      min = matrix[i - 1][j] + 1; // Devarion.
      if ((t = matrix[i][j - 1] + 1)  1 &&
        j > 1 &&
        this_i === that[j - 2] &&
        __this[i - 2] === that_j &&
        (t = matrix[i - 2][j - 2] + cost) 

现在,这是 R 代码:

library(DT)

dtable ').on('input', function() { fn(that, this) });",
              "  if (preDefined !== null) {",
              "     $(el).val(preDefined[0]);",
              "  }",
              "  return el;",
              "}"
            ),
            inputValue = JS(
              "function (el) {",
              "  return [$(el[0]).val()];",
              "}"
            ),
            isInputValid = JS(
              "function (el, that) {",
              "  return $(el[0]).val().length !== 0;",
              "}"
            ),
            search = JS(
              "function (value, pattern) {",
              "  var fuzzy = levenshtein(value, pattern[0]);",
              "  return fuzzy.similarity > 0.25;",
              "}"
            )
          )
        )
      )
    )
  )
)

path 

必须选择相似度的阈值。这里我取的是0.25

return fuzzy.similarity > 0.25;

编辑

要在 Shiny 中使用,请使用 server=FALSE:

renderDT({
  dtable
}, server = FALSE)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!