[R] replace a few strings in a text file

baptiste auguie ba208 at exeter.ac.uk
Mon Oct 27 15:52:54 CET 2008


First of all, let me apologize for the 200 files that would result  
from running my "minimal example". I've just realised this on my  
Desktop; it seems i overlooked the arguments passed to sapply().

Thank you for your reply Duncan, I realize now my approach was quite  
silly. It is indeed much more natural to create and manipulate all the  
strings in R and write the new file every time in a single step.

Thanks,

baptiste


On 27 Oct 2008, at 10:47, Duncan Murdoch wrote:

> baptiste auguie wrote:
>> Dear all,
>>
>>
>> I wrote a wrapper to a FORTRAN program using R. The main program uses
>> a text file (~200 lines) as an input describing the simulation to be
>> run. I typically generate the file once with the right parameters
>> using a combination of file(), paste(), cat(). This is fine, and it
>> works well, however I then need to update only a few values in the
>> file many times (~200 times, typically). I've used Ruby for this task
>> in the past, and I wonder whether there is a simple and efficient way
>> to achieve this in R.
>>
>> Here's a minimal example,
>>
>>
>> myFile <- "test.txt"
>> writeHeader <-
>> function (out=myFile, N=5, wavelength=0.1)
>> {
>>     output <- file(paste(out), "w")
>> headerString <- c("Lorem ipsum dolor sit amet, consectetur  
>> adipisicing
>> elit,
>>      sed do eiusmod tempor incididunt ut labore et dolore magna  
>> aliqua.
>>      Ut enim ad minim veniam, quis nostrud exercitation ullamco  
>> laboris
>> nisi ut aliquip ex ea commodo consequat.
>>      Duis aute irure dolor in reprehenderit in voluptate velit esse  
>> cillum
>> dolore eu fugiat nulla pariatur.
>>      Excepteur sint occaecat cupidatat non proident, sunt in culpa  
>> qui
>> officia deserunt mollit anim id est laborum.
>> Variables", wavelength, N, "
>>
>> - wavelength
>> - ind_refMed
>>
>> Lorem ipsum dolor sit amet, consectetur adipisicing elit,
>> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
>> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
>> nisi ut aliquip ex ea commodo consequat.
>> Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
>> dolore eu fugiat nulla pariatur.
>> Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
>> officia deserunt mollit anim id est laborum.
>> ")
>>     cat(paste(headerString), file = output, sep = "\n")
>>
>>     close(output)
>> }
>>
>> writeHeader(out=myFile)
>>
>> system(paste("cat", myFile))
>>
>> system.time(sapply(1:200, writeHeader) -> b.quiet)
>>
>>
>> Now for the ruby replacement solution:
>>
>> #!/usr/bin/ruby -w
>> lambda = 0.1
>> N = 5
>>
>> input_file=IO.readlines('test.txt')
>> # replace wavelength
>> input_file[6]= lambda.to_f
>> input_file[7]= N.to_f
>> f=File.new("test2.txt","w")
>> f.puts input_file
>> f.close
>>
>> I think (unverified) that this approach is more efficient than  
>> calling
>> the writeHeader() each time. Please do let me know if I'm wrong on
>> this. The drawback of using this Ruby script is that I need to know
>> the numbers of the lines to be replaced (also, I don't know much in
>> Ruby). I'm not sure how I can find this other than manually, as there
>> is no regular pattern to look for. Ideally the generating script
>> writeHeader() would return these line numbers, I'm not sure how to
>> achieve this.
>>
>> Any comments are welcome.
>
> If there are only 200 lines, why not put them into a character vector,
> and edit them there?  That's more or less what your Ruby code does.   
> In
> R, it would be written
>
> input_file <- readLines("test.txt")
> input_file[6] <- sprintf("%f", lambda)
> input_file[7] <- sprintf("%f", N)
> writeLines("test2.txt", input_file)
>
> but if everything is in R, you don't need the first line.
>
> Duncan Murdoch
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag



More information about the R-help mailing list