首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何平稳地从qdap::mgsub()切换到textlean::mgsub()?

如何平稳地从qdap::mgsub()切换到textlean::mgsub()?
EN

Stack Overflow用户
提问于 2018-10-10 10:06:43
回答 3查看 161关注 0票数 1

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

代码语言:javascript
复制
qdap::mgsub(pattern,replacement,x)
textclean::mgsub(x,pattern,replacement)

我在使用qdap::mgsub()的地方有很多代码。不巧的是,当我传递这些论点以发挥作用时,我并没有正确地命名它们。因此,我需要重新排序所有这些代码,以便能够使用to::mgsub()。

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-10-10 10:17:28

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

代码语言:javascript
复制
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()部分的演示:

代码语言:javascript
复制
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)
票数 1
EN

Stack Overflow用户

发布于 2018-10-10 11:10:37

考虑到@duckmayr的回答,我想出了另一个解决问题的方法:

首先运行此函数:

代码语言:javascript
复制
reorder_mgsub <- function(pattern,replacement,x){
  output <- textclean::mgsub(x,pattern,replacement)
  return(output)
}

第二:用reorder_mgsub查找并替换reorder_mgsub

这个解决方案可能不那么优雅,因为我必须手工完成步骤2,但对我来说,它工作得很好。

票数 2
EN

Stack Overflow用户

发布于 2018-12-14 13:26:16

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

也就是说,使用mgsub的源代码,

代码语言:javascript
复制
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
}

紧接着是

代码语言:javascript
复制
assignInNamespace('mgsub', reorder_mgsub, 'textclean')

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52737705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档