如何修改R Shiny中RenderDT的内置搜索栏,以允许多个用逗号分隔的条目?

huangapple go评论69阅读模式
英文:

How can I modify the inbuilt search bar of RenderDT in R Shiny to allow multiple entries separated by commas?

问题

我一直在尝试在shiny的renderDT表格函数的搜索栏中添加多个条目。
例如,在下面的代码中,我想修改内置在renderDT中的搜索栏,使其能够接受多个条目,以逗号分隔;例如,setosa,virginica应该返回包含setosa和virginica的行。我找到了添加新搜索栏的解决方案,但我想知道是否可以相应地修改这个。对于此方面的任何帮助将不胜感激。

```R
if (interactive()) {
  library(shiny)
  library(DT)
  shinyApp(
    ui = fluidPage(fluidRow(column(12, DTOutput('tbl')))),
    server = function(input, output) {
      output$tbl = renderDT(
        iris, options = list(lengthChange = FALSE)
      )
    }
  )
}

我尝试了类似于这样的东西,但这会添加另一个搜索栏选项,这是不必要的。

if (interactive()) {
  library(shiny)
  library(DT)
  
  shinyApp(
    ui = fluidPage(
      fluidRow(DTOutput('tbl'))
    ),
    server = function(input, output) {
      output$tbl = renderDT({
        data <- iris
        
        searchItems <- unlist(strsplit(input$search, ",")) # 通过逗号拆分输入字符串
        searchItems <- trimws(searchItems) # 移除首尾空格
        
        filteredData <- data[data$Species %in% searchItems, ]
        
        datatable(filteredData, options = list(lengthChange = FALSE))
      })
    }
  )
}

<details>
<summary>英文:</summary>

I have been trying to add multiple entries on the search bar of the renderdt table function on shiny.
for example on the following code, instead of having a new search bar, i want to modify the one which is inbuilt in renderDT and allow it to take multiple entries, comma separated; for example setosa,virginica should bring rows with both setosa and virginica. I found solutions to add a new search bar but i wanted to know if i can modify this one accordingly. Any help regarding this would be highly appreciated.

if (interactive()) {
library(shiny)
library(DT)
shinyApp(
ui = fluidPage(fluidRow(column(12, DTOutput('tbl')))),
server = function(input, output) {
output$tbl = renderDT(
iris, options = list(lengthChange = FALSE)
)
}
)
}



i tried something like this, but this adds another search bar option and that is unnecessary

if (interactive()) {
library(shiny)
library(DT)

shinyApp(
ui = fluidPage(
fluidRow(DTOutput('tbl'))
),
server = function(input, output) {
output$tbl = renderDT({
data <- iris

    searchItems &lt;- unlist(strsplit(input$search, &quot;,&quot;)) # Split input string by commas
    searchItems &lt;- trimws(searchItems) # Remove leading/trailing whitespace
    
    filteredData &lt;- data[data$Species %in% searchItems, ]
    
    datatable(filteredData, options = list(lengthChange = FALSE))
  })
}

)
}



</details>


# 答案1
**得分**: 1

你可以使用这段代码:

```r
library(shiny)
library(DT)

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

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)

个人而言,我更喜欢使用搜索构建器

datatable(
  iris[c(1, 2, 51, 52, 101, 102),],
  extensions = "SearchBuilder",
  options = list(
    dom = "Qlfrtip", 
    searchbuilder = TRUE
  )
)
英文:

You can use this code:

library(shiny)
library(DT)

callback &lt;- function(sep) {
  sprintf(&#39;
$(&quot;div.search&quot;).append($(&quot;#mySearch&quot;));
$(&quot;#mySearch&quot;).on(&quot;keyup redraw&quot;, function(){
  var splits = $(&quot;#mySearch&quot;).val().split(&quot;%s&quot;).filter(function(x){return x !==&quot;&quot;;})
  var searchString = &quot;(&quot; + splits.join(&quot;|&quot;) + &quot;)&quot;;
  table.search(searchString, true).draw(true);
});
&#39;, sep)
}

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

server &lt;- function(input, output){
  
  output[[&quot;dtable&quot;]] &lt;- renderDT({
    datatable(
      iris[c(1, 2, 51, 52, 101, 102),],
      options = list(
        dom = &quot;l&lt;&#39;search&#39;&gt;rtip&quot;
      ),
      callback = JS(callback(&quot;,&quot;))
    )
  }, server = FALSE)
  
}

shinyApp(ui, server)

如何修改R Shiny中RenderDT的内置搜索栏,以允许多个用逗号分隔的条目?

Personally I prefer the search builder:

datatable(
  iris[c(1, 2, 51, 52, 101, 102),],
  extensions = &quot;SearchBuilder&quot;,
  options = list(
    dom = &quot;Qlfrtip&quot;, 
    searchbuilder = TRUE
  )
)

如何修改R Shiny中RenderDT的内置搜索栏,以允许多个用逗号分隔的条目?

huangapple
  • 本文由 发表于 2023年6月2日 01:49:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定