英文:
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 <- unlist(strsplit(input$search, ",")) # Split input string by commas
searchItems <- trimws(searchItems) # Remove leading/trailing whitespace
filteredData <- 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 <- function(sep) {
sprintf('
$("div.search").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)
Personally I prefer the search builder:
datatable(
iris[c(1, 2, 51, 52, 101, 102),],
extensions = "SearchBuilder",
options = list(
dom = "Qlfrtip",
searchbuilder = TRUE
)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论