【问题标题】:R - Automatically find string matches in multiple R scriptsR - 在多个 R 脚本中自动查找字符串匹配
【发布时间】:2021-03-04 18:52:03
【问题描述】:

这是一个有点奇怪的问题。而且我不知道如何最好地表达它,请与我裸露。

背景:
我们有一个闪亮的应用程序,它使用shiny.i18n 包将应用程序翻译成多种语言。我们有多个开发人员在开发这个应用程序,有时他们没有输入应该翻译成translate.json 文件的文本,这意味着有人必须检查整个应用程序脚本来检查所有内容是否都在 json 文件中。我们的应用程序目前总共包含 384 个 R 脚本,并且通过它们都需要几天时间。这真是一个庞大的应用程序..

问题
我希望以某种方式自动化这项任务。理想情况下,我想阅读所有 R 脚本的列表,例如使用list.files(...):

r_scripts <- list.files(
    path = "/path/to/scripts",
    pattern = ".R",
    recursive = TRUE,
    full.names = TRUE
)

然后获取这个 R 脚本列表,读取每个脚本并将其添加到向量中。例如

code_vctr <- as.character()

for(i in 1:length(r_scripts)){

    code_vctr <- cat(
      code_vctr,
      readLines(
        r_scripts[i]
      )
   )
}

然后,在我以某种方式将脚本连接到一个巨大的向量中之后,我需要一些方法来搜索与translate()$t(...) 相关的文本。例如,shiny.i18n 使用函数translate()$t() 将括号之间的内容翻译成用户选择的语言。因此,如果在代码中显示:`translate()$t("This should be translate"),那么文本字符串会在 translate.json 文件中查找匹配字符串:“This should be translate”,然后对其进行更改无论其他语言的字符串是什么,例如法语:“Cela devrait être traduit”。

然后,我如何搜索位于translate()$t(...) 括号之间的文本字符串?此类代码的一个示例是:

    infoBox(
      translate()$t(
        "Error"
      ),
      subtitle = translate()$t(
        "Failed to get this code to work"
      ),
      icon = icon(
        "thumbs-down",
        lib = "glyphicon"
      ),
      fill = TRUE,
      color = "red"
    )
  )

但也可以包含一个paste0 函数来处理更长的字符串。但是,无论是否包含 paste0,只要能够获取括号之间的文本就会非常有帮助。

    infoBox(
      translate()$t(
        "Warning"
      ),
      subtitle = translate()$t(
        paste0(
          "This is a very very long text string",
          "it continues on, but already just being ",
          "able to the text inbetween the translate ",
          "brackets, regardless of whether it contains ",
          "paste0 or not, would still be super helpful."
        ),
        icon = icon(
          "thumbs-down",
          lib = "glyphicon"
        ),
        fill = TRUE,
        color = "red"
      )
    )

理想情况下,我想获得一个包含所有文本的数据框,我可以使用这些文本在 translate.json 文件中搜索匹配项以查看缺少哪些匹配项..

请注意,我上面的代码示例并不能很好地工作。我似乎找不到一个很好的工作示例...

任何建议将不胜感激!提前谢谢你。

【问题讨论】:

    标签: r regex shiny internationalization


    【解决方案1】:

    我相信你想做的事可以通过4个步骤来实现:

    1. 复制文件
    r_scripts <- list.files(
        path = "/path/to/scripts",
        pattern = ".R",
        recursive = TRUE,
        full.names = TRUE
    )
    #duplicate files in a new folder (first create the new folder)
    
    new.folder <- 'H:(insert location here)'
    
    file.copy(r_scripts, new.folder)
    
    1. 将这些副本转换为文本文件,以便在 R 中轻松读取。
    #Make new file names
    new_r_scripts <- sub(pattern="\\.R$", replacement=".txt", x=r_scripts)
    
    #before renaming files, you'll probably have to paste that file location onto the names (use paste0)
    
    # rename files
    file.rename(from = r_scripts, to = new_r_scripts)
    
    1. 将所有文本文件读入 R.
    scripts <- lapply(new_r_scripts, function(x)readChar(x, file.info(x)$size))
    
    #turn the list given from the above function into a vector
    script_vector <- unlist(scripts)
    
    1. 使用前瞻后瞻的正则表达式查找您要查找的字符串。这使用包stringr
    stringr::str_extract_all(script_vector, '(?<=(translate\\(\\)\\$t\\()[[:alpha:]]+(?=\\)')
    

    请注意转义字符的使用,因为您的字符串中的某些字符是正则表达式的元字符。

    这实际上只是答案的近似值。我敢打赌我在这里某个地方犯了一个错误,但我发现这是一个有趣的问题。祝你好运,如果您遇到问题,请随时询问,我会看看我能做些什么。

    【讨论】:

    • 这绝对是在正确的轨道上!多么传奇!现在只是想找出正确的正则表达式模式:)
    • @rhyncogale 哎呀。我看到我搞砸了什么。 '(?
    【解决方案2】:

    R studio 在 RStudio IDE 中具有内置的代码导航功能,可在多个文件中搜索“关键字”并替换它们。可能这会帮助您减少一些手动更改

    R Studio 博客中提供了详细说明

    来源:https://support.rstudio.com/hc/en-us/articles/200710523-Navigating-Code

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      相关资源
      最近更新 更多