Re: [R] Read Windows-like .INI files into R data structure?

From: Christophe Pallier <>
Date: Thu, 14 Jun 2007 09:46:03 +0200

On 6/14/07, Gabor Grothendieck <> wrote:
> Here is yet another solution. This is the simplest so far.
> Lines.raw is as before and the output is a 3 column character
> matrix.

section <- ""
> f <- function(x) {
> if (length(x) == 1) section <<- gsub("[\\[\\]]", "", x)
> if (length(x) <= 1) return()
> return(c(x, section))
> }
> # Lines <- readLines("myfile.ini")
> Lines <- readLines(textConnection(Lines.raw))
>"rbind", lapply(strsplit(Lines, "="), f))

The corresponding awk code fits in one line '/\[/{a=$1;next}{print $1,$2,a}'.

With the example.ini:

$ awk -F"=" '/\[/{a=$1;next}{print $1,$2,a}' example.ini var1 value1 [Section1]
var2 value2 [Section1]
A value3 [Section2]
B value4 [Section2]

The output can then be imported in R with read.table.

I know, I am not playing by the rules here... :) But with programming languages, like with human languages, it pays to be bi or tri-lingual (or polyglot, if one can).

I also realise that under Windows, it means using the command line, something that not so many people are comfortable with nowadays.

One reason people insist on using only one language to do everything may be due to the awkwardness and limitations of the default scripting language under Windows (DOS). Having everything done inside one single R script can seem simpler. But a divide-and-conquer approach, with a few small scripts, can actually work better in some complex cases.

I tend to do as much as possible in R but for all serious data analysis tasks, I use Makefile to "glue" the various stages of processing. Data preprocessing stages (performed with R or other tools) create files that are then processed with R. One advantage is that preprocessing is performed only when raw input data change; but the most important, is that when I come back years later, I can follow the exact flow of transformations. Again, "make" (like awk or R) is not without limits and idiosyncrasies. If someone has a simpler solution, I am interested to hear about it.


Christophe Pallier (

	[[alternative HTML version deleted]]

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Thu 14 Jun 2007 - 07:50:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 14 Jun 2007 - 08:31:57 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.