DT::datatable 正则表达式 OR 运算符问题

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

OR 运算符适用于第一种情况,但不适用于第二种情况

此代码创建 DT::datatable(),它返回与“mazda”或“datsun”匹配的所有行

library(tidyverse)
library(DT)

DT::datatable(
  mtcars, 
  filter = list(position = 'none', clear = TRUE),
  options = list(
    search = list(regex = TRUE, caseInsensitive = TRUE, search = 'mazda|datsun'),
    pageLength = 5
  )
)

所以我希望这段代码返回所有匹配“好”或“理想”的行。但它只匹配“理想”:

datatable(
  diamonds |> head(),
  filter = list(position = 'none', clear = TRUE),
  options = list(
    search = list(regex = TRUE, caseInsensitive = TRUE, search = 'ideal|good'),
    pageLength = 5
  )
)

这里可能出了什么问题?

r regex datatables dt
1个回答
0
投票

您需要禁用

smart
选项(出于某种晦涩的原因,它适用于
mtcars
,但根据 per the docs,在使用正则表达式时,您应该始终禁用
smart
):

datatable(
  diamonds |>
    group_by(cut) |>
    slice_sample(n = 2),
  filter = list(position = 'none', clear = TRUE),
  options = list(
    search = list(regex = TRUE, caseInsensitive = TRUE, search = 'ideal|good', smart = FALSE),
    pageLength = 6
  )
)

A datatable with 6 rows with a search term

© www.soinside.com 2019 - 2024. All rights reserved.