2013-06-25 46 views
-1

我想读取R文件或脚本,修改正在读取的外部数据文件的名称并将修改的R代码导出到新的R文件或脚本中。除了正在读取的数据文件的名称(以及新的R文件的名称)之外,我希望两个R脚本是相同的。在R中使用cat创建格式化的R脚本

我可以走近,除了我不知道如何保留空白行我用于可读性和错误减少。

这是正在读取的原始R文件。请注意,这个文件中的一些代码是非感性的,但对我来说是无关紧要的。这段代码不需要运行。

# apple.pie.all.purpose.flour.arsc.Jun23.2013.r 

library(my.library) 

aa <- 10  # aa 
bb <- c(1:7) # bb 

my.data = convert.txt("../applepieallpurposeflour.txt", 

group.df = data.frame(recipe = 
      c("recipe1", "recipe2", "recipe3", "recipe4", "recipe5")), 
      covariates = c(paste("temp", seq_along(1:aa), sep=""))) 

ingredient <- c('all purpose flour') 

function(make.pie){ make a pie } 

这里是R代码我用来读取上面的文件,修改它并导出结果。此R代码运行并且是唯一需要运行以实现所需结果的代码(除了我无法准确获取新R脚本的格式与原始R脚本的格式相匹配,即原始R中存在的空行脚本是不是在新的R脚本存在):

setwd('c:/users/mmiller21/simple r programs/') 

# define new fruit 

new.fruit <- 'peach' 

# read flour file for original fruit 

flour <- readLines('apple.pie.all.purpose.flour.arsc.Jun23.2013.r') 

# create new file name 

output.flour <- paste(new.fruit, ".pie.all.purpose.flour.arsc.Jun23.2013.r", sep="") 

# add new file name 

flour.a <- gsub("# apple.pie.all.purpose.flour.arsc.Jun23.2013.r", 
       paste("# ", output.flour, sep=""), flour) 

# add line to read new data file 

cat(file = output.flour, 
      gsub("my.data = convert.txt\\(\"../applepieallpurposeflour.txt", 
      paste("my.data = convert.txt\\(\"../", new.fruit, "pieallpurposeflour.txt", 
      sep=""), flour.a), 
      sep=c("","\n"), fill = TRUE 
) 

这里是产生新的R脚本:

# peach.pie.all.purpose.flour.arsc.Jun23.2013.r 
library(my.library) 
aa <- 10  # aa 
bb <- c(1:7) # bb 

my.data = convert.txt("../peachpieallpurposeflour.txt", 
      group.df = data.frame(recipe = 
      c("recipe1", "recipe2", "recipe3", "recipe4", "recipe5")), 
      covariates = c(paste("temp", seq_along(1:aa), sep=""))) 
ingredient <- c('all purpose flour') 
function(make.pie){ make a pie } 

有一个在新创建的R档一个空行,但我怎么能插入原始R脚本中的所有空白行?感谢您的任何建议。

编辑:我似乎无法在StackOverflow上复制空白行。他们似乎被自动删除。 StackOverflow甚至删除我正在使用的缩进,我似乎无法取代它。为此事道歉。自动删除空白行和缩进问题在手边的问题具体是关于格式。我似乎无法修复该帖子以显示R代码格式在我的脚本中。但是,当我正在主动编辑帖子时,代码显示正确。

编辑:2013年6月27日:原始R文件和中间R文件的代码中的空行和缩进删除似乎与我的笔记本电脑,而不是与StackOverflow关联。当我在办公室桌面上查看这篇文章和我的答案时,格式是正确的。当我查看这篇文章和我的笔记本电脑的答案时,空行和缩进都消失了。也许我的笔记本电脑显示器有故障。对不起,最初假设问题出在StackOverflow上。

+2

你打算如何运行你的最终脚本,比如'apple.pie.all.purpose.flour.arsc.Jun23.2013.r'?我在问,因为有更好的方法来处理你的问题。如果您从命令行运行脚本,则可以使输入文件成为参数(请参阅'commandArgs')。如果你使用R中的'source'来源脚本,那么你可以将你的代码包装到一个函数中,该函数将文件名作为参数。 – flodel

+1

正如@ flodel所说,有很多更好的方法来处理这个问题,而不是文本消除。我会去自己的功能路线。 –

+1

@flodel R脚本将被提交给群集或超级计算机。如果我确定如何创建你建议的功能,我会在这里发布。无论文件如何创建,我的目标是两个R文件都是相同的,除了我在上面强调的两个区别。 –

回答

0

下面是一个解决方案,它重现原始R脚本(除了两个所需的更改外),同时还在新的R脚本中保留原始R脚本的格式。

setwd('c:/users/mmiller21/simple r programs/') 

new.fruit <- 'peach' 

flour <- readLines('apple.pie.all.purpose.flour.arsc.Jun23.2013.r') 

output.flour <- paste(new.fruit, ".pie.all.purpose.flour.arsc.Jun23.2013.r", sep="") 

flour.a <- gsub("# apple.pie.all.purpose.flour.arsc.Jun23.2013.r", 
       paste("# ", output.flour, sep=""), flour) 

flour.b <- gsub("my.data = convert.txt\\(\"../applepieallpurposeflour.txt", 
paste("my.data = convert.txt\\(\"../", new.fruit, "pieallpurposeflour.txt", sep=""), flour.a) 

for(i in 1:length(flour.b)) { 

if(i == 1) cat(flour.b[i], file = output.flour, sep=c("\n"), fill=TRUE    ) 
if(i > 1) cat(flour.b[i], file = output.flour, sep=c("\n"), fill=TRUE, append = TRUE) 

} 

再次,我很抱歉无法以可读的方式格式化上述R代码。我从来没有在StackOverflow上遇到过这个问题,也不知道解决方案。无论如何,上面的R脚本解决了我在原帖中描述的问题。

要查看原始R脚本的格式,您必须单击原始帖子下的编辑按钮。

编辑:2013年6月25日

我不知道我在做什么不同的昨天,但今天我发现下面的简单cat声明,立即就位以上for-loop的,创建而新R脚本保留原始R脚本的格式。

cat(flour.b, file = output.flour, sep=c("\n")) 
1

这是一个函数,它将为每个两个变量的组合创建一个新的R文件。抱歉,下面的代码格式不好。代码确实运行并且按照预期工作(假设原始R文件的名称以“.arsc.Jun26.2013.r”结尾,而不是在原始文章中使用的“.arsc.Jun23.2013.r”中):

setwd('c:/users/mmiller21/simple r programs/') 

# define fruits of interest 

fruits <- c('apple', 'pumpkin', 'pecan') 

# define ingredients of interest 

ingredients <- c('all.purpose.flour', 'sugar', 'ground.cinnamon') 

# define every combination of fruit and ingredient 

fruits.and.ingredients <- expand.grid(fruits, ingredients) 

old.fruit <- as.character(rep('apple', nrow(fruits.and.ingredients))) 
old.ingredient <- as.character(rep('all.purpose.flour', nrow(fruits.and.ingredients))) 

fruits.and.ingredients2 <- cbind(old.fruit , as.character(fruits.and.ingredients[,1]), 
          old.ingredient, as.character(fruits.and.ingredients[,2])) 

colnames(fruits.and.ingredients2) <- c('old.fruit', 'new.fruit', 'old.ingredient', 'new.ingredient') 


# begin function 

make.pie <- function(old.fruit, new.fruit, old.ingredient, new.ingredient) { 

new.ingredient2 <- gsub('\\.', '', new.ingredient) 
old.ingredient2 <- gsub('\\.', '', old.ingredient) 

new.ingredient3 <- gsub('\\.', ' ', new.ingredient) 
old.ingredient3 <- gsub('\\.', ' ', old.ingredient) 

# file name 

old.file <- paste(old.fruit, ".pie.", old.ingredient, ".arsc.Jun26.2013.r", sep="") 
new.file <- paste(new.fruit, ".pie.", new.ingredient, ".arsc.Jun26.2013.r", sep="") 

# read original fruit and original ingredient 

flour <- readLines(old.file) 

# add new file name 

flour.a <- gsub(paste("# ", old.file, sep=""), 
       paste("# ", new.file, sep=""), flour) 

# read new data file 

old.data.file <- print(paste("my.data = convert.txt(\"../", old.fruit, "pie", old.ingredient2, ".txt\",", sep=""), quote=FALSE) 

new.data.file <- print(paste("my.data = convert.txt(\"../", new.fruit, "pie", new.ingredient2, ".txt\",", sep=""), quote=FALSE) 

flour.b <- ifelse(flour.a == old.data.file, new.data.file, flour.a) 

flour.c <- ifelse(flour.b == paste('ingredient <- c(\'', old.ingredient3, '\')', sep=""), 
          paste('ingredient <- c(\'', new.ingredient3, '\')', sep=""), flour.b) 

cat(flour.c, file = new.file, sep=c("\n")) 

} 

apply(fruits.and.ingredients2, 1, function(x) make.pie(x[1], x[2], x[3], x[4]))