You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Shiny App输出的特殊格式内容转为管道分隔列表用于dplyr过滤?

解决方案

方法1:手动构建匹配模式

直接将目标字符串用paste()函数拼接,指定collapse = "|"生成管道分隔的正则模式:

# 生成管道分隔的匹配规则
pattern <- paste("wrong item", "Wrong order", "completely wrong", collapse = "|")

# 用于dplyr过滤
df %>% 
  filter(str_detect(string, pattern))

方法2:从控制台输出文本自动提取

如果是直接复制的控制台输出内容,可以通过字符串处理自动提取目标项并生成模式:

# 复制的原始控制台输出
raw_output <- '[1] "wrong item"       "Wrong order"      "completely wrong"'

# 提取所有引号内的内容并拼接成管道分隔模式
library(stringr)
pattern <- str_c(str_extract_all(raw_output, '"([^"]+)"')[[1]] %>% str_remove_all('"'), collapse = "|")

# 执行过滤
df %>% 
  filter(str_detect(string, pattern))

你之前代码的问题

你尝试的l<-paste("wrong item" "Wrong order" "completely wrong")会报错,因为paste()函数的多个输入参数必须用逗号分隔,不能直接连写字符串。

内容的提问来源于stack exchange,提问作者tezzaaa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 21:10:48