您可以使用正则表达式替换您调用旧函数的每个文件的文本中的出现,使用如下函数:
replace_mgsub <- function(path) {
file_text <- readr::read_file(path)
file_text <- gsub("qdap::mgsub\\(([^, ]+) *, *([^, ]+) *, *([^\\)]) *\\)",
"textclean::mgsub\\(\\3, \\1, \\2\\)", file_text)
readr::write_file(file_text, path)
}
然后你会调用每一个相关path
的(我假设你知道你需要调用函数的文件列表;如果没有,请在下面评论,我可以在上面添加一些东西)。gsub()
下面是该部分功能的演示:
file_text <- "qdap::mgsub(pattern,replacement,x)"
cat(gsub("qdap::mgsub\\(([^, ]+) *, *([^, ]+) *, *([^\\)]) *\\)",
"textclean::mgsub\\(\\3, \\1, \\2\\)", file_text))
#> textclean::mgsub(x, pattern, replacement)
file_text <- "# I'll have in this part some irrelevant code
# to show it won't interfere with that
y = rnorm(1000)
qdap::mgsub(pattern,replacement,x)
z = rnorm(10)
# And also demonstrate multiple occurrences of the function
# as well as illustrate that it doesn't matter if you have spaces
# between comma separated arguments
qdap::mgsub(pattern, replacement, x)"
cat(gsub("qdap::mgsub\\(([^, ]+) *, *([^, ]+) *, *([^\\)]) *\\)",
"textclean::mgsub\\(\\3, \\1, \\2\\)", file_text))
#> # I'll have in this part some irrelevant code
#> # to show it won't interfere with that
#> y = rnorm(1000)
#> textclean::mgsub(x, pattern, replacement)
#> z = rnorm(10)
#> # And also demonstrate multiple occurrences of the function
#> # as well as illustrate that it doesn't matter if you have spaces
#> # between comma separated arguments
#> textclean::mgsub(x, pattern, replacement)