簡體   English   中英

R - 在多個 R 腳本中自動查找字符串匹配

[英]R - Automatically find string matches in multiple R scripts

這是一個有點奇怪的問題。 而且我不知道如何最好地表達它,請與我聯系。

背景:
我們有一個閃亮的應用程序,它使用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 文件中的匹配項,以查看哪些丟失了..

請注意,我上面的代碼示例並不能很好地工作。 我似乎無法得到一個很好的工作示例......

任何建議將不勝感激! 先感謝您。

我相信你想做的事情可以通過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:]]+(?=\\)')

請注意使用轉義字符是必需的,因為字符串中的某些字符是正則表達式的元字符。

這實際上只是答案的近似值。 我敢打賭我在這里的某個地方犯了一個錯誤,但我發現這是一個有趣的問題。 祝你好運,如果您遇到問題,請隨時提出問題,我會看看我能做些什么。

R studio 在 RStudio IDE 中具有導航代碼的內置功能,可以在多個文件中搜索“關鍵字”並替換它們。 可能這會幫助您減少一些手動更改

R Studio 博客中提供了詳細說明在此處輸入圖片說明

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM