R 在没有特定格式的文本中查找代码

R 在没有特定格式的文本中查找代码,r,R,如果我想检测自由文本中的R代码,有没有可能检测到它?它存在于文本中,就像简单文本一样,没有格式 范例 这里我们说了些什么,代码就跟这篇文章一样 图书馆(dplyr) 库(stringr) dd%>%过滤器(str_计数(文本“”>=3) 您的方法是使用正则表达式(regex)。一种选择是使用rebus软件包,使其更容易: library(dplyr) library(rebus) library(stringi) # example data df <- dplyr::tibble(id

如果我想检测自由文本中的R代码,有没有可能检测到它?它存在于文本中,就像简单文本一样,没有格式

范例

这里我们说了些什么,代码就跟这篇文章一样

图书馆(dplyr)
库(stringr)
dd%>%过滤器(str_计数(文本“”>=3)


您的方法是使用正则表达式(regex)。一种选择是使用rebus软件包,使其更容易:

library(dplyr)
library(rebus)
library(stringi)

# example data
df <- dplyr::tibble(id <- c(1, 2),
                    text <- c('here we say something and the code is just as this text  library(dplyr) library(stringr) dd %>% filter(str_count(text, " ") >= 3', 
                              'here we say something and the code is just as this text'))

# pattern to detect: 'libaray(asdfasdf)' or '%>%' )
pattern <- or("library(" %R% one_or_more(ALPHA) %R% ")", "%>%")

# calculations
df %>% 
  dplyr::mutate(DETECTED = stringi::stri_detect_regex(text, pattern))
库(dplyr)
图书馆(REBS)
图书馆(stringi)
#示例数据
df%
dplyr::mutate(DETECTED=stringi::stri\u detect\u regex(文本,模式))
这将创建一个新列“DETECTED”,如果在列“text”中检测到库(asfas)或%>%,则该列为真。可能您希望在模式中包含更多选项,因为一些用户可能不会进行库调用或使用管道操作符。您可能包括赋值运算符“