使用带有闪亮的stringi来搜索数据帧会产生警告

问题描述 投票:1回答:1

我制作了一个闪亮的应用程序来搜索一个大数据帧,我想到了使用stringi。但是,当我运行应用程序时,我收到警告,不支持空搜索模式。有了这个示例应用程序,我可以很好地忽略这个警告(虽然它不断发送垃圾邮件),但是我的大数据框应用程序减慢了一切,我可以停止应用程序的唯一方法是终止R会话。

## app.R ##
require(shiny)
require(stringi)
require(dplyr)
require(DT)

ui <- fluidPage(textInput("searchall", label =  "Search"),
            dataTableOutput("tableSearch"))

server <- function(input, output, session) {
  data(GNI2014)
  output$tableSearch <- DT::renderDataTable(datatable(
  GNI2014 %>% filter(
      if (!is.null(input$searchall))
        stri_detect_fixed(str = country , pattern = input$searchall)
    ),
    options = list(sDom  = '<"top">lrt<"bottom">ip')
  ))
}

shinyApp(ui, server)

当我运行这个应用程序时,我被淹没了以下警告:

stri_detect_fixed中的警告(str = country,pattern = input $ searchall):不支持空搜索模式

什么是绕过此警告和减速带来的最佳方法。

r shiny dplyr stringi
1个回答
1
投票

你不需要stringi()。查询数据的最快方法是使用data.table()country上的密钥,并使用grepl()对数据进行子集化。

使用treemap包中的GNI2014数据的示例。

library(treemap)
library(data.table)
data(GNI2014)
gni2014table <- data.table(GNI2014)
setkey(gni2014table,"country")
searchText <- "berm"
gni2014table[grepl(searchText,gni2014table$country,ignore.case=TRUE),]

searchText <- "United"
gni2014table[grepl(searchText,gni2014table$country,ignore.case=TRUE),]

......和输出。

> library(treemap)
> library(data.table)
> data(GNI2014)
> gni2014table <- data.table(GNI2014)
> setkey(gni2014table,"country")
> searchText <- "berm"
> gni2014table[grepl(searchText,gni2014table$country,ignore.case=TRUE),]
   iso3 country     continent population    GNI
1:  BMU Bermuda North America      67837 106140
> 
> searchText <- "United"
> gni2014table[grepl(searchText,gni2014table$country,ignore.case=TRUE),]
   iso3              country     continent population   GNI
1:  ARE United Arab Emirates          Asia    4798491 44600
2:  GBR       United Kingdom        Europe   62262000 43430
3:  USA        United States North America  313973000 55200
>

仅返回要在UI上填充字段的列,如下所示。

searchText <- "United Arab"
gni2014table[grepl(searchText,gni2014table$country,ignore.case=TRUE),country]

更新2017年12月20日:添加代码以运行微基准测试,显示在第一个测试用例中lgrepl()stringi_detect_fixed()运行快20 ms,而在第二种情况下,stringi_detect_fixed()lgrepl()快100 ms,对请求进行100次迭代。

library(treemap)
library(data.table)
library(microbenchmark)
data(GNI2014)
gni2014table <- data.table(GNI2014)
setkey(gni2014table,"country")
searchText <- "berm"

microbenchmark(gni2014table[grepl(searchText,gni2014table$country,
                                  ignore.case=TRUE),])

searchText <- "United Arab"
microbenchmark(gni2014table[grepl(searchText,gni2014table$country,
                                  ignore.case=TRUE),country])

library(stringi)
searchText <- "berm"

microbenchmark(gni2014table[stri_detect_fixed(searchText,
                              gni2014table$country,
                              case_insensitive=TRUE),])

searchText <- "United Arab"

microbenchmark(gni2014table[stri_detect_fixed(searchText,
                            gni2014table$country,case_insensitive=TRUE),])

您必须自己运行代码才能重现基准,因为microbenchmark()的输出不容易在SO上显示。

也就是说,时间的总结版本是:

searchText      Function             Mean (in Microseconds)
-------------   -------------------- -----------------------
berm            grepl                526.2545
United Arab     grepl                583.1789
berm            stringi_detect_fixed 545.8772
United Arab     stringi_detect_fixed 524.1132
© www.soinside.com 2019 - 2024. All rights reserved.