在Shiny DT数据表中使用空格(不是管道)批量搜索正则表达式 [英] Batch searching regex in Shiny DT datatables with spaces (not pipes)

查看:79
本文介绍了在Shiny DT数据表中使用空格(不是管道)批量搜索正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面来自

 > sessionInfo()
R版本3.6.3(2020-02-29)
平台:x86_64-apple-darwin15.6.0(64位)
运行于:macOS High Sierra 10.13.6

矩阵产品:默认
BLAS:/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK:/Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

语言环境:
[1] en_GB.UTF-8 / en_GB.UTF-8 /en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

附加基本软件包:
[1] stats4并行stats图形grDevices utils数据集方法
[9]基本

其他附加软件包:
[1] ggplot2_3.3.0 DT_0.13 rsconnect_0.8.16 Shinythemes_1.1.2
[5] dplyr_0.8.5 Shiny_1.4.0。 2 BiocParallel_1.20.1 MLInterfaces_1.66.5
[9] cluster_2.1.0 annotate_1.64.0 XML_3.99-0.3 AnnotationDbi_1.48.0
[13] IRanges_2.20.2 MSnbase_2.12.0 Pro tGenerics_1.18.0 S4Vectors_0.24.4
[17] mzR_2.20.0 Rcpp_1.0.4.6 Biobase_2.46.0 BiocGenerics_0.32.0


解决方案

这里是一种方法:

  library(shiny)
库(DT)

回调<-'
$( div.search)。append($(#mySearch));
$(#mySearch)。on( keyup redraw,function(){
var splits = $(#mySearch)。val()。split().filter( function(x){return x!==;})
var searchString =( + splits.join( |)+);
table.search(searchString,true ).draw(true);
});
'

ui<-fluidPage(
tags $ head(tags $ style(HTML(。search {float:right;})))),
br(),
标签$ input(type = text,id = mySearch,占位符= Search),
DTOutput( dtable)


服务器<-函数(输入,输出){

output [[ dtable]]<-renderDT({
datatable(
iris [ c(1,2,51,52,101,102),],
选项=列表(
dom = l<'search'> rtip
),
回调= JS (回调)

},服务器= FALSE)

}

ShinyApp(ui,服务器)


Following on from this post can anyone please tell me if it's possible to implement a way to search an interactive Shiny DT datatable where keywords are separated by spaces and not pipes? Users of my apps will have lists of genes separated by spaces and adding pipes will defeat the point of making the app user friendly.

Example code:

## example taken from https://rstudio.github.io/DT/007-search.html
library(DT)
mtcars2 = mtcars[, c(1:5, 9)]
mtcars2$am = factor(mtcars$am, c(0, 1), c('automatic', 'manual'))
dt <- datatable(
  mtcars2, colnames = c('model' = 1),
  filter = list(position = 'top', clear = FALSE),
  options = list(
    search = list(regex = TRUE, caseInsensitive = TRUE),
    pageLength = 5
  )
)


## ----simple Shiny app with datatable----
ui <- fluidPage(
  fluidRow(
    DT::dataTableOutput("table")
  )
)

server <- function(input, output) {
  output$table <- DT::renderDataTable(dt)
}

shinyApp(ui = ui, server = server)

> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats4    parallel  stats     graphics  grDevices utils     datasets  methods  
[9] base     

other attached packages:
 [1] ggplot2_3.3.0        DT_0.13              rsconnect_0.8.16     shinythemes_1.1.2   
 [5] dplyr_0.8.5          shiny_1.4.0.2        BiocParallel_1.20.1  MLInterfaces_1.66.5 
 [9] cluster_2.1.0        annotate_1.64.0      XML_3.99-0.3         AnnotationDbi_1.48.0
[13] IRanges_2.20.2       MSnbase_2.12.0       ProtGenerics_1.18.0  S4Vectors_0.24.4    
[17] mzR_2.20.0           Rcpp_1.0.4.6         Biobase_2.46.0       BiocGenerics_0.32.0 

解决方案

Here is a way:

library(shiny)
library(DT)

callback <- '
$("div.search").append($("#mySearch"));
$("#mySearch").on("keyup redraw", function(){
  var splits = $("#mySearch").val().split(" ").filter(function(x){return x !=="";})
  var searchString = "(" + splits.join("|") + ")";
  table.search(searchString, true).draw(true);
});
'

ui <- fluidPage(
  tags$head(tags$style(HTML(".search {float: right;}"))),
  br(),
  tags$input(type = "text", id = "mySearch", placeholder = "Search"),
  DTOutput("dtable")
)

server <- function(input, output){

  output[["dtable"]] <- renderDT({
    datatable(
      iris[c(1,2,51,52,101,102),],
      options = list(
        dom = "l<'search'>rtip"
      ),
      callback = JS(callback)
    )
  }, server = FALSE)

}

shinyApp(ui, server)

这篇关于在Shiny DT数据表中使用空格(不是管道)批量搜索正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆