Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-在多个R脚本中自动查找字符串匹配项_R_Regex_Shiny_Internationalization - Fatal编程技术网

R-在多个R脚本中自动查找字符串匹配项

R-在多个R脚本中自动查找字符串匹配项,r,regex,shiny,internationalization,R,Regex,Shiny,Internationalization,这是一个有点奇怪的问题。我不知道怎样才能最好地表达它,请跟我说 背景: 我们有一个闪亮的应用程序,它使用shinny.i18n包将应用程序翻译成多种语言。我们有多个开发人员在此应用程序上工作,有时他们不输入应该翻译成translate.json文件的文本,这意味着必须有人检查整个应用程序脚本,以检查json文件中是否有所有内容。。我们的应用程序目前总共包含384个R脚本,完成这些脚本需要几天的时间。这真是一个巨大的应用程序 问题 我希望以某种方式使这项任务自动化。理想情况下,我希望阅读所有R脚本

这是一个有点奇怪的问题。我不知道怎样才能最好地表达它,请跟我说

背景:
我们有一个闪亮的应用程序,它使用
shinny.i18n
包将应用程序翻译成多种语言。我们有多个开发人员在此应用程序上工作,有时他们不输入应该翻译成
translate.json
文件的文本,这意味着必须有人检查整个应用程序脚本,以检查json文件中是否有所有内容。。我们的应用程序目前总共包含384个R脚本,完成这些脚本需要几天的时间。这真是一个巨大的应用程序

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

但也可以包含用于较长字符串的
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文件中搜索匹配项,以查看缺少哪些匹配项

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


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

我相信您想做的事情可以通过四个步骤实现:

  • 复制文件
  • 请注意转义字符的使用,这是必要的,因为字符串中的某些字符是正则表达式的元字符


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

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

  • 复制文件
  • 请注意转义字符的使用,这是必要的,因为字符串中的某些字符是正则表达式的元字符


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

    这绝对是正确的方向!真是个传奇!现在只是想找出正确的正则表达式模式:)@rhyncogale哇。我明白我搞砸了什么(?这绝对是对的!真是一个传奇!现在只是想找出正确的正则表达式模式:)@rhyncogale哇。我明白我搞砸了什么(?
    code_vctr <- as.character()
    
    for(i in 1:length(r_scripts)){
    
        code_vctr <- cat(
          code_vctr,
          readLines(
            r_scripts[i]
          )
       )
    }
    
        infoBox(
          translate()$t(
            "Error"
          ),
          subtitle = translate()$t(
            "Failed to get this code to work"
          ),
          icon = icon(
            "thumbs-down",
            lib = "glyphicon"
          ),
          fill = TRUE,
          color = "red"
        )
      )
    
        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"
          )
        )
    
    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)
    
    #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)
    
    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)
    
    stringr::str_extract_all(script_vector, '(?<=(translate\\(\\)\\$t\\()[[:alpha:]]+(?=\\)')