由于R版本问题,我需要在qdap::mgsub()
和textclean::mgsub()
之间切换。除了参数的顺序外,功能几乎相同:
qdap::mgsub(pattern,replacement,x)
textclean::mgsub(x,pattern,replacement)
我有很多使用qdap::mgsub()
的代码。毫无疑问,在将参数传递给函数时,我没有正确命名参数。因此,为了能够使用textclean :: mgsub(),我需要对它们全部重新排序。
(编程)是否有一种优雅的方式来在这两个函数之间切换而无需更改参数的顺序?
答案 0 :(得分:2)
考虑@duckmayr的答案之后,我想出了另一个解决方案:
首先运行此功能:
reorder_mgsub <- function(pattern,replacement,x){
output <- textclean::mgsub(x,pattern,replacement)
return(output)
}
第二:找到并用qdap::mgsub
替换reorder_mgsub
此解决方案可能不太优雅,因为我必须手动执行第2步,但对我来说效果很好。
答案 1 :(得分:1)
您可以使用正则表达式来替换您在其中调用旧函数的每个文件的文本中出现的内容,并使用如下函数:
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)
由reprex package(v0.2.1)于2018-10-10创建
答案 2 :(得分:1)
好吧,您还可以在程序包中重新分配原始功能以适合您的代码。
即,使用mgsub的源代码
reorder_mgsub <- function(pattern,replacement,x, leadspace = FALSE, trailspace = FALSE,
fixed = TRUE, trim = FALSE, order.pattern = fixed, safe = FALSE,
...){
if (!is.null(list(...)$ignore.case) & fixed) {
warning(paste0("`ignore.case = TRUE` can't be used with `fixed = TRUE`.\n",
"Do you want to set `fixed = FALSE`?"), call. = FALSE)
}
if (safe) {
return(mgsub_regex_safe(x = x, pattern = pattern, replacement = replacement,
...))
}
if (leadspace | trailspace) {
replacement <- spaste(replacement, trailing = trailspace,
leading = leadspace)
}
if (fixed && order.pattern) {
ord <- rev(order(nchar(pattern)))
pattern <- pattern[ord]
if (length(replacement) != 1)
replacement <- replacement[ord]
}
if (length(replacement) == 1) {
replacement <- rep(replacement, length(pattern))
}
if (any(!nzchar(pattern))) {
good_apples <- which(nzchar(pattern))
pattern <- pattern[good_apples]
replacement <- replacement[good_apples]
warning(paste0("Empty pattern found (i.e., `pattern = \"\"`).\n",
"This pattern and replacement have been removed."),
call. = FALSE)
}
for (i in seq_along(pattern)) {
x <- gsub(pattern[i], replacement[i], x, fixed = fixed,
...)
}
if (trim) {
x <- gsub("\\s+", " ", gsub("^\\s+|\\s+$", "", x, perl = TRUE),
perl = TRUE)
}
x
}
其次是
assignInNamespace('mgsub', reorder_mgsub, 'textclean')
应该将更新后的功能分配给textclean
包的名称空间,并且使用textclean::mgsub
的任何代码现在都将使用更新后的功能。这样就无需更改所有代码。