我刚接触过光泽,并询问是否可以做些什么。 我想允许用户输入文本搜索词,并返回描述中出现用户搜索词的数据框的行。
例如,如果用户输入“环绕”,“便携式音频”,“技术”,“ mp3”,则将返回包含至少一个搜索词的所有行。每行中的类别列不必包含所有搜索词,但必须至少包含这些词之一。
我希望允许用户将搜索词作为文本字符串输入,而不是创建过滤器复选框,因为我不想将它们限制为仅特定的搜索词。一旦用户输入了搜索词,这些搜索词必须用逗号-分隔,这些词将被分成一系列值。这样,用户可以将多个单词存储为一个搜索词。例如,“便携式音频”将被存储为一个搜索词,而不是红色且大小写被单独存储。
我有一个包含销售数据的大型数据集,并且在下面包括了一个样本数据集。 该数据集包括一个带有每个产品类别描述的文本列。
rm(list=ls())
ID<-c("AG138", "HF249", "KI786", "DS264", "WQ987", "HS682", "UI987",
"SA376")
Price<-c("40","60","100","85","65","120","38","250")
Condition<-c("New","Used - Excellent","New", "New","Used - Good","New",
"New","Used - Excellent")
Category<-c("Computers,Office,Computer Accessories,Computer Accessories
& Peripherals,Laptop Accessories,Laptop Chargers & Adapters,Computers &
Tablets,Electronics,Battery Chargers,Batteries,Office Technology",
"Surround Speakers,Electronics Features,Surround Sound
Systems,Frys,Electronics,All Home Theater Systems,TVs
Entertainment,Speakers,Home Audio & Theater,Home Electronics,TV Sound
Systems rxxbwxxcscxuvutetd,Home & Kitchen Features,Home Theater
Systems,Audio Speakers,TV Sound Systems,Home Audio,Home
Theater,Electrical,Audio",
"Bluetooth & Wireless Speakers,Portable Audio &
Video,Electronics,MP3 & MP4 Player Accessories,Portable
Speakers,Audio",
"Computers,Internal Hard Drives,Internal Drives,Drives, Storage
& Blank Media,Computers & Accessories,Hard Drives (HDD, SSD &
NAS),Computer Accessories & Peripherals,Internal Hard Disk
Drives,Electronics,Hard Drives & Storage,Drives
Storage,Computers/Tablets & Networking,Computer Components,Computers &
Tablets,Data Storage",
"Parts & Accessories,Car Speakers,6.5\\ Car
Speakers,Electronics,Car Electronics,Speakers,eBay Motors,Car
Audio,Consumer Electronics,Auto & Tires,All Car Speakers &
Subwoofers,Coaxial Speakers,Speakers & Speaker Systems,Car & Vehicle
Electronics,Auto Electronics,Car Speakers & Speaker Systems,Car
Speakers and Subwoofers,Vehicle Electronics & GPS,Car Electronics &
GPS",
"In-Wall & In-Ceiling Speakers,In-Ceiling Speakers,Home
Audio,Speakers,Audio",
"Straps & Hand Grips,Camera & Photo Accessories,Camera
Accessories,Cameras & Photo,Digital Camera
Accessories,Electronics,Camera Straps,Cameras &
Camcorders,Photography,Other Camera Accessories,Camera Straps
Harnesses,Photography Bags Cases,Camera Bags, Cases & Straps",
"LCD TVs,Samsung TVs,Electronics,Shop TVs by
Type,Televisions,TVs Entertainment,TV & Home Theater,TVs by Brand,All
Flat-Panel TVs,All TVs,LED & LCD TVs,TVs,TV & Video,Television &
Video,LED TVs")
sales_df<-data.frame(ID, Price, Condition, Category,
stringsAsFactors=FALSE)
library(shiny)
library(DT)
shinyApp(
ui <- fluidPage(
setBackgroundColor("ghostwhite"),
titlePanel("Sales Catalogue"),
sidebarLayout(position = "left",
sidebarPanel(
tabsetPanel(id = "tabset",
tabPanel("Search",
textInput("search_input",
"Input Search Terms", "Input Search Terms (comma delimited)"),
verbatimTextOutput("search_value"))
),
actionButton("go", "Filter Data")
),
mainPanel(
DT::dataTableOutput("sales_table")
)
)
),
server = function(input, output) {
terms <- unlist(strsplit("search_input", ","))
grepl(Category, terms)
#if True is the result at least once, return row in filtered
dataframe
output$sales_table = DT::renderDataTable({
sales_df
})
output$search_value <- renderText({input$search_input})
}
)
我已经包含了我一直在尝试的代码。我使用grepl函数来匹配产品类别描述中的单词。理想情况下,该应用将遍历整个数据框(最终数据集将具有一百万行),并返回其中产品类别描述包含至少一个用户搜索词的行。我不确定什么是最好,最省时的方法。
由于我是新来的人,我不确定描述我遇到的问题的最佳和最清晰的方法,因此我可以在需要时添加更多信息。任何建议,将不胜感激。谢谢。
答案 0 :(得分:0)
通过将选项search$regex
设置为TRUE
,可以在搜索输入中使用正则表达式:
datatable(iris[c(1,2,51,52,101,102),],
options =
list(
search = list(regex = TRUE)
)
)
另一个解决方案(点击“返回”以运行搜索)
js <- c(
"function(settings){",
" var instance = settings.oInstance;",
" var table = instance.api();",
" var input = instance.parent().find('.dataTables_filter input');",
" input.off('keyup search input').on('keypress', function(e){",
" if(e.which == 13){",
" var keywords = input.val().split(' '), filter ='';",
" for(var i=0; i<keywords.length; i++){",
" filter = (filter !== '') ? filter + '|' + keywords[i] : keywords[i];",
" }",
" table.search(filter, true, false).draw();",
" }",
" });",
"}"
)
datatable(iris[c(1,2,51,52,101,102),],
options =
list(initComplete = JS(js)
)
)