2015-11-05 50 views

回答

1

我在Windows 7上測試了它,它工作。本質上,您使用shell功能,並在窗口cmd上做的一切都很快。

write_beginning <- function(text, file){ 

    #write your text to a temp file i.e. temp.txt 
    write(text, file='temp.txt') 
    #print temp.txt to a new file 
    shell(paste('type temp.txt >' , 'new.txt')) 
    #append your file to new.txt 
    shell(paste('type', file, '>> new.txt')) 
    #remove temp.txt - I use capture output to get rid of the 
    #annoying TRUE printed by file.remove 
    dump <- capture.output(file.remove('temp.txt')) 
    #uncomment the last line below to rename new.txt with the name of your file 
    #and essentially modify your old file 
    #dump <- capture.output(file.rename('new.txt', file)) 
} 

#assuming your file is test.txt and you want to add 'hello' at the beginning just do: 
write_beginning('hello', 'test.txt')  

在Linux上,你只需要以一個文件發送到另一個找到相應的命令(我真的覺得你需要cat在Linux取代type但現在我無法測試)。

+0

感謝。我不知道殼牌 – panterasBox

+0

非常受歡迎。這是一個非常有用的功能:) – LyzandeR

0

你會使用系統()函數在一個Linux發行版:

system('cp file.txt temp.txt; echo " " > file.txt; cat temp.txt >> file.txt; rm temp.txt') 
相關問題