2015-05-18 63 views
1

我正在編寫一個R腳本,它讀入模板.R文件,日期列表,並創建一組對應於日期的文件夾幷包含.R其中已經在R中執行了文本替換以定製給定日期的每個腳本。將對象保存爲R中的.R文件,並保持格式

雖然我寫出了.R文件,但由於格式和/或字符表示方式不斷被搞砸,所以我被卡住了。

這裏有一個最小的,可重複的例子:

RMapsDemo <- readLines("https://raw.githubusercontent.com/hack-r/RMapsDemo/master/RMapsDemo.R") 
RMapsDemo <- gsub("## File: RMapsDemo.R", "## File: RMapsDemo.R ####", RMapsDemo) 
save(RMapsDemo, file = "RMapsDemo.R")   # Doesn't work right 
save(RMapsDemo, file = "RMapsDemo.R", ascii = T) # Doesn't work right 
dput(RMapsDemo, file = "RMapsDemo.R")   # Close, but no cigar 
dput(RMapsDemo, file = "RMapsDemo.R", control = c("keepNA", "keepInteger")) # Close, but no cigar 
+1

你試過'writeLines()'嗎? –

+0

@RicardoSaporta不好,好主意。我會試試看。 –

+2

@RicardoSaporta工作!謝謝!! –

回答

1

裏卡多·薩波塔指出了意見的解決方案 - 使用writeLines

我覺得自己沒有想到這件事會很愚蠢。它的作品非常漂亮。

writeLines(RMapsDemo, con = "RMapsDemo.R") 
+1

@RicardoSaporta ARGH!對不起 - 修正了它。理查德是我在這裏與其他人交談的其他人的名字。我很抱歉。 –