2013-06-25 43 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]))