R 如何从qdap::mgsub()平滑切换到textclean::mgsub()?

R 如何从qdap::mgsub()平滑切换到textclean::mgsub()?,r,qdap,R,Qdap,由于R版本问题,我需要在qdap::mgsub()和textclean::mgsub()之间切换。除了参数的顺序外,函数几乎相同: qdap::mgsub(pattern,replacement,x) textclean::mgsub(x,pattern,replacement) 我使用了很多代码qdap::mgsub()。不幸的是,当我将参数传递给函数时,我没有正确地命名参数。因此,为了能够使用textclean::mgsub(),我需要对它们进行重新排序 是否有(以编程方式)一种优雅的方式

由于R版本问题,我需要在
qdap::mgsub()
textclean::mgsub()
之间切换。除了参数的顺序外,函数几乎相同:

qdap::mgsub(pattern,replacement,x)
textclean::mgsub(x,pattern,replacement)
我使用了很多代码
qdap::mgsub()
。不幸的是,当我将参数传递给函数时,我没有正确地命名参数。因此,为了能够使用textclean::mgsub(),我需要对它们进行重新排序


是否有(以编程方式)一种优雅的方式在这两个函数之间切换,而不必更改参数的顺序?

您可以使用正则表达式替换调用旧函数的每个文件文本中出现的内容,使用如下函数:

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)
}

可以使用正则表达式替换调用旧函数的每个文件的文本中出现的内容,方法如下:

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)
}

仔细考虑了@duckmayr的答案,我想出了另一个解决问题的办法:

首先运行此功能:

reorder_mgsub <- function(pattern,replacement,x){
  output <- textclean::mgsub(x,pattern,replacement)
  return(output)
}

reorder_mgsub仔细思考@duckmayr的答案,我想出了另一个解决问题的方法:

首先运行此功能:

reorder_mgsub <- function(pattern,replacement,x){
  output <- textclean::mgsub(x,pattern,replacement)
  return(output)
}

reorder_mgsub好吧,您还可以重新分配包中的原始函数以适合您的代码

即使用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
}

它应该将更新后的函数分配给
textclean
包的名称空间,使用
textclean::mgsub
的任何代码现在都将使用更新后的函数。这样就不需要更改所有代码。

好的,您还可以重新分配包中的原始函数以适合您的代码

即使用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
}

它应该将更新后的函数分配给
textclean
包的名称空间,使用
textclean::mgsub
的任何代码现在都将使用更新后的函数。这样就不需要更改所有代码。

这段代码真不错!我不知道文件的清单,但我想我自己能弄清楚。我希望有一个解决方案,但它是这样做的:
newmgsub@rdatasculptor(1)是的,我有点这样认为,但我认为这实际上可能是一个更干净的解决方案,因为即使有这样的解决方案,你仍然必须将该定义放在你谈论的每个文件的开头(除非我们在这里处理一个包)以及将
library(qdap)
的所有调用替换为
library(textclean)
qdap::
替换为(nothing)。(2)所有这些代码都在您正在构建的包中,还是仅仅是您机器上的代码?(3)您使用的是什么操作系统(更改有关识别需要在其上运行函数的文件的建议)?我想你的代码是我问题的答案,所以我会把它删掉!仔细考虑你的解决方案后,我想出了另一个简单的代码。我也会把它作为答案添加进去。代码不错!我不知道文件列表,但我想我自己可以弄清楚。我希望有一个解决方案,尽管它是这样做的:
new_mgsub@rdatasculptor(1)是的,我有点这样认为,但我认为这实际上可能是一个更干净的解决方案,因为即使有这样的解决方案,您仍然必须将该定义放在所讨论的每个文件的开头(除非我们在这里处理包),并替换所有对
库(qdap)的调用
to
library(textclean)
qdap::
to(nothing)。(2)所有这些代码都在您正在构建的软件包中,还是仅仅是您计算机上的代码?(3)您使用的是什么操作系统(更改了有关识别需要在其上运行函数的文件的建议)?我认为您的代码是我问题的答案,因此我将对其进行豁免!仔细考虑您的解决方案后,我提出了另一个简单的代码。我也将添加此代码作为答案。优雅的解决方案!(我不确定为什么,但我有点不愿意更改程序包代码本身)@rdatasculptor是的,当使用旧的未维护或仍在开发中的包时,它可能是一个有用的技巧。在某种程度上,它只是原始包的一个分支,并且函数仅在当前会话中重新分配,因此不是永久性的。但是,是的,我也经常不愿意使用类似的函数重写,并且在不需要时倾向于使用它o侵入性(如您的情况)。如果您正在向函数添加额外的代码,也可以将其包装在带有非常明显错误的
tryCatch
中,例如,
error=function(e){print(“我的自定义编辑使代码崩溃!”)}
:)优雅的解决方案!(我不确定为什么,但我有点不愿意更改包代码本身)@rdatasculptor是的,当使用旧的未维护或仍在开发中的包时,它可能是一个有用的技巧。在某种程度上,它只是原始包的一个分支,并且函数仅在当前会话中重新分配,因此不是永久性的。但是,是的,我也经常不愿意使用类似的函数重写,并且在不需要时倾向于使用它o侵入性(如您的情况)。如果您正在向函数添加其他代码,也可以将其包装在带有非常明显错误的
tryCatch
中,例如,
error=function(e){print(“我的自定义编辑使代码崩溃!”)}
:)