【问题标题】:R: How to batch create multiple copies of an .R script file with different variable names?R:如何批量创建具有不同变量名的 .R 脚本文件的多个副本?
【发布时间】:2023-04-06 06:38:01
【问题描述】:

这是我在这里的第一篇文章,我正在尽我所能遵循指南,所以请多多包涵。

我想创建大量相似的 .R 脚本文件,它们的区别仅在于使用的变量名称和提及这些变量的字符串。当然,这也可以通过搜索和替换来实现,但我想知道是否有更方便的解决方案可以更快地创建一堆。

让我们用这个编造的脚本(实际数据在这里无关紧要):

prefix.AnExemplaryRandomVariable <- rnorm(n = 100, mean = 0, sd = 1)
AnotherRandomVariable.suffix <- rnorm(n = 100, mean = 10, sd = 3)

plot(prefix.AnExemplaryRandomVariable, AnotherRandomVariable.suffix,
      type = "p", pch = "*", xlab = "An Exemplary Random Variable",
      ylab = "Another Random Variable", main = "A plot of An Exemplary
      Random Variable and Another Random Variable")

我的想法是定义两个向量,每个向量都有 k 个新名称。

newNamesVar1 <- c("prefix.FirstVariable", "prefix.SomeData")
newNamesVar2 <- c("SecondVariable.suffix", "CannotThinkOfMoreNames.suffix")

我正在寻找的结果是 k 个新的 .R 文件,如下所示:

prefix.FirstVariable <- rnorm(n = 100, mean = 0, sd = 1)
SecondVariable.suffix <- rnorm(n = 100, mean = 10, sd = 3)

plot(prefix.FirstVariable, SecondVariable.suffix, type = "p",
      pch = "*", xlab = "First Variable", ylab = "Second Variable",
      main = "A plot of First Variable and Second Variable")

prefix.SomeData <- rnorm(n = 100, mean = 0, sd = 1)
CannotThinkOfMoreNames.suffix <- rnorm(n = 100, mean = 10, sd = 3)

plot(prefix.SomeData, CannotThinkOfMoreNames.suffix, type = "p",
      pch = "*", xlab = "Some Data", ylab = "Cant Think Of More Names",
      main = "A plot of Some Data and Cannot Think Of More Names")

我看到了以下两个挑战:

  1. 用对应的向量条目替换原来的变量名
  2. 检查任何字符串与原始变量名的相似之处并替换它们,同时保持语法和格式(区分大小写、空格...)不变。

这是我第一次尝试将 R 用于实际数据分析之外的任何事情,因此我什至无法提供太多代码草稿。我能够使用ls() 获得变量名称,但我对下一步该做什么一无所知,主要是因为这些更改不会应用于当前活动的文件,而是应用于一个全新的文件。

感谢任何解决方案、提示、提示或轻推!

谢谢!

【问题讨论】:

  • 它有效,我不会忘记接受它,但我首先想检查在尝试将它应用于我的实际代码时是否遇到任何问题。接受答案后,我还能提出后续问题吗?
  • 你可以问一个新问题。每个帖子最好问一个问题

标签: r replace batch-processing


【解决方案1】:

这是一种方法。

设置此答案:

writeLines('
prefix.AnExemplaryRandomVariable <- rnorm(n = 100, mean = 0, sd = 1)
AnotherRandomVariable.suffix <- rnorm(n = 100, mean = 10, sd = 3)

plot(prefix.AnExemplaryRandomVariable, AnotherRandomVariable.suffix,
      type = "p", pch = "*", xlab = "An Exemplary Random Variable",
      ylab = "Another Random Variable",
      main = "A plot of An Exemplary Random Variable and Another Random Variable")
', "template.R")

要使用的替换值表,其中列名表示模板字符串,列值是替换文本。

replacements <- data.frame(
  "An Exemplary Random Variable" = c("First Variable", "Some Data"),
  "Another Random Variable" = c("Second Variable", "Cannot Think Of More Names"),
  check.names = FALSE
)
replacements
#   An Exemplary Random Variable    Another Random Variable
# 1               First Variable            Second Variable
# 2                    Some Data Cannot Think Of More Names

替换template.R 中的每个模板字符串的工作,进行替换,最终存储到新文件中。

code <- readLines("template.R")
for (row in seq_len(nrow(replacements))) {
  newcode <- code
  for (col in seq_along(replacements)) {
    if (!is.na(replacements[row,col])) {
      ptn1 <- colnames(replacements)[col] # original
      ptn2 <- gsub(" +", "", ptn1)        # "Title Case Sentence" to "TitleCaseSentence"
      repl1 <- replacements[row,col]
      repl2 <- gsub(" +", "", repl1)
      newcode <- gsub(paste0("\\b", ptn1, "\\b"), repl1,
                      gsub(paste0("\\b", ptn2, "\\b"), repl2, newcode))
    }
  }
  writeLines(newcode, sprintf("code_%s.R", row))
}

这具有附加功能,如果替换字符串(replacements 中特定单元格内的值)是 NA,则不会尝试替换该模式。

输出:

  • code_1.R

    prefix.FirstVariable <- rnorm(n = 100, mean = 0, sd = 1)
    SecondVariable.suffix <- rnorm(n = 100, mean = 10, sd = 3)
    
    plot(prefix.FirstVariable, SecondVariable.suffix,
          type = "p", pch = "*", xlab = "First Variable",
          ylab = "Second Variable",
          main = "A plot of First Variable and Second Variable")
    
  • code_2.R

    prefix.SomeData <- rnorm(n = 100, mean = 0, sd = 1)
    CannotThinkOfMoreNames.suffix <- rnorm(n = 100, mean = 10, sd = 3)
    
    plot(prefix.SomeData, CannotThinkOfMoreNames.suffix,
          type = "p", pch = "*", xlab = "Some Data",
          ylab = "Cannot Think Of More Names",
          main = "A plot of Some Data and Cannot Think Of More Names")
    

限制:

  • 模式字符串必须在自己的行上连续,所以请注意我将模板main=字符串更改为不跨越两行
  • 模式字符串不能紧跟在字母之前/之后;使用\\b(正则表达式单词边界)允许使用一些字符(如文字.),但这并没有试图变得更花哨

已编辑:完成后,我意识到用空格定义模式和替换字符串可能会更容易,然后删除第二个( TitleCase) 模式。这样可以避免一些歧义和按标题大小写拆分字符串的诡计。它还允许您的模式或替换是不是标题大小写。

【讨论】:

    猜你喜欢
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多