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

2007-06-14 Thread Christophe Pallier
On 6/14/07, Gabor Grothendieck [EMAIL PROTECTED] 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))
 do.call(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





-- 
Christophe Pallier (http://www.pallier.org)

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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.


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

2007-06-14 Thread Christophe Pallier
Ah! I forgot to mention that it is possible to call awk from R:

a - system(awk -F'=' '/\\[/{a=$1;next}{print $1,$2,a}' example.ini,
intern=T)
z - textConnection(a)
read.table(z)


Christophe


On 6/14/07, Christophe Pallier [EMAIL PROTECTED] wrote:

 On 6/14/07, Gabor Grothendieck [EMAIL PROTECTED] 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))
  do.call(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





 --
 Christophe Pallier ( http://www.pallier.org)




-- 
Christophe Pallier (http://www.pallier.org)

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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.


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

2007-06-13 Thread Vladimir Eremeev

One more question, inspired by this one, just to increase my R skill level.

Earl F. Glynn wrote:
 
 I need to process some datasets where the configuration information was 
 stored in .INI-like files, i.e., text files with sections like this:
 
 [Section1]
 var1=value1
 var2=value2
 [Section2]
 A=value3
 B=value4
 
var1=value1, A=value3 is almost pure R code.
Is it possible to use this feature to solve the problem? 
That is, something like eval(as.expression(A=value3))  and assign (store)
the result in lst$Section2 environment.

lst is a newly created list, containing the contents of the file being
processed.
-- 
View this message in context: 
http://www.nabble.com/Read-Windows-like-.INI-files-into-R-data-structure--tf3908740.html#a11094865
Sent from the R help mailing list archive at Nabble.com.

__
R-help@stat.math.ethz.ch 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.


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

2007-06-13 Thread Christophe Pallier
 var1=value1, A=value3 is almost pure R code.
 Is it possible to use this feature to solve the problem?



Along the same lines: you may write a short script that converts the ini
file into R code that can be sourced.

From your example, you can generate  the  following R code:

Section1 - list()
Section1['var1'] - value1
Section1['var2'] - value2
Section2 - list()
Section2['A'] - value3
Section2['B'] - value4


with the following awk script (using awk -F'=' -f conv.awk example.ini 
example.R)

### conv.awk
$1 ~ /\[/ { gsub(/[\[\]]/,); # remove the brackets
   listname = $1;
   print $1  - list();
   next }
{ print listname [' $1 '] -  $2 }

(I know, it looks cryptic... so I am shooting myself in the foot after
claiming that awk scripts are typically quite readable ;-)

-- 
Christophe Pallier (http://www.pallier.org)

[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch 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.


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

2007-06-13 Thread Vladimir Eremeev


Christophe Pallier wrote:
 
 var1=value1, A=value3 is almost pure R code.
 Is it possible to use this feature to solve the problem?
 
 Along the same lines: you may write a short script that converts the ini
 file into R code that can be sourced.
 
From your example, you can generate  the  following R code:
 
 Section1 - list()
 Section1['var1'] - value1
 Section1['var2'] - value2
 Section2 - list()
 Section2['A'] - value3
 Section2['B'] - value4
 
 
 with the following awk script (using awk -F'=' -f conv.awk example.ini 
 example.R)
 
 ### conv.awk
 $1 ~ /\[/ { gsub(/[\[\]]/,); # remove the brackets
listname = $1;
print $1  - list();
next }
 { print listname [' $1 '] -  $2 }
 
 (I know, it looks cryptic... so I am shooting myself in the foot after
 claiming that awk scripts are typically quite readable ;-)
 
 Christophe Pallier (http://www.pallier.org)
 

It's sufficiently readable, but using something besides R is not sporty. ;)
-- 
View this message in context: 
http://www.nabble.com/Read-Windows-like-.INI-files-into-R-data-structure--tf3908740.html#a11095800
Sent from the R help mailing list archive at Nabble.com.

__
R-help@stat.math.ethz.ch 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.


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

2007-06-13 Thread Uwe Ligges


Vladimir Eremeev wrote:
 
 Christophe Pallier wrote:
 var1=value1, A=value3 is almost pure R code.
 Is it possible to use this feature to solve the problem?
 Along the same lines: you may write a short script that converts the ini
 file into R code that can be sourced.

 From your example, you can generate  the  following R code:

 Section1 - list()
 Section1['var1'] - value1
 Section1['var2'] - value2
 Section2 - list()
 Section2['A'] - value3
 Section2['B'] - value4


 with the following awk script (using awk -F'=' -f conv.awk example.ini 
 example.R)

 ### conv.awk
 $1 ~ /\[/ { gsub(/[\[\]]/,); # remove the brackets
listname = $1;
print $1  - list();
next }
 { print listname [' $1 '] -  $2 }

 (I know, it looks cryptic... so I am shooting myself in the foot after
 claiming that awk scripts are typically quite readable ;-)

 Christophe Pallier (http://www.pallier.org)

 
 It's sufficiently readable, but using something besides R is not sporty. ;)


OK, I try to be sporty, at least that is what my family doctor asks me 
to do all the time ;-)
Certainly there is much space for improvements...


X - readLines(file)
value1 - 1
value2 - 2
value3 - 3
value4 - 4
sections - grep(^\\[.*\\]$, X)
starts - sections + 1
ends - c(sections[-1] - 1, length(X))
L - vector(mode=list, length=length(sections))
names(L) - gsub(\\[|\\], , X[sections])
for(i in seq(along = sections)){
 env - new.env()
 eval(parse(text=X[seq(starts[i], ends[i])]), env = env)
 L[[i]] - as.list(env)
}

__
R-help@stat.math.ethz.ch 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.


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

2007-06-13 Thread Earl F. Glynn
[EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]...
 .Ini files are, for lack of a better description, ancient.

In this case a device is creating the INI files as part of an experiment, so 
the file format cannot be changed (at least easily).

I've looked at XML files from time to time and I'm amazed more don't 
complain how bloated, if not wasteful, they are.  I've seen XML files that 
were megabytes long when they held kilobytes worth of data.  INI files may 
be ancient, but they can be efficient and effective compared with XML.  In 
some cases, newer may not really be better (but newer may have the 
momentum behind it).


Gabor Grothendieck [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]...
 In thinking about this a bit more here is an even shorter solution where
 Lines.raw is as before:

 # Lines - readLines(myfile.ini)
 Lines - readLines(textConnection(Lines.raw))
 Lines2 - chartr([], ==, Lines)
 DF - read.table(textConnection(Lines2), as.is = TRUE, sep = =, fill = 
 TRUE)
 L - DF$V1 == 
 subset(transform(DF, V3 = V2[which(L)[cumsum(L)]])[1:3], V1 != )

Thanks for your helpful suggestions, Gabor.  Perhaps your zoo option is 
more elegant, but I try to use as few packages as possible, so this option 
seemed the best for me.

Since in my problem the structure of the INI sections is almost static and 
always present, I extended your example to create an in-memory list of 
everything in the INI file with this function:

# Prototype of how to read INI files to process olfactometer data
# efg, 13 June 2007
# Thanks to Gabor Grothendieck for helpful suggestions in the R-Help
# mailing list on how to parse the INI file.
Parse.INI - function(INI.filename)
{
  connection - file(INI.filename)
  Lines  - readLines(connection)
  close(connection)

  Lines - chartr([], ==, Lines)  # change section headers

  connection - textConnection(Lines)
  d - read.table(connection, as.is = TRUE, sep = =, fill = TRUE)
  close(connection)

  L - d$V1 == # location of section breaks
  d - subset(transform(d, V3 = V2[which(L)[cumsum(L)]])[1:3],
   V1 != )

  ToParse  - paste(INI.list$, d$V3, $,  d$V1,  - ',
d$V2, ', sep=)

  INI.list - list()
  eval(parse(text=ToParse))

  return(INI.list)
}


Here's an example of using the above function (I'll put the sample input 
file below):

INI1 - Parse.INI(sample.ini)

# Explore INI contents
summary(INI1)

INI1$SystemSetup$OlfactometerCode
INI1$DefaultLevels
unlist(INI1$DefaultLevels)
INI1$Map

INI1$Map$port1
as.integer( unlist( strsplit(INI1$Map$port1, ,) ) )

= = = = =
Sample output:

 INI1 - Parse.INI(sample.ini)

 # Explore INI contents
 summary(INI1)
  Length Class  Mode
SystemSetup   1  -none- list
Files 8  -none- list
DefaultLevels 4  -none- list
OdorNames 2  -none- list
Map   3  -none- list

 INI1$SystemSetup$OlfactometerCode
[1] 3
 INI1$DefaultLevels
$FC00
[1] 50

$FC01
[1] 100

$FC02
[1] 50

$FC10
[1] 50

 unlist(INI1$DefaultLevels)
 FC00  FC01  FC02  FC10
 50 100  50  50
 INI1$Map
$port0
[1] 0,0,0,0,0,0,0,0,0,0,0,0

$port1
[1] 0,0,0,0,0,0,0,0,0,0,0,0

$port2
[1] 0,0,0,0,0,0,0,0,0,0,0,0


 INI1$Map$port1
[1] 0,0,0,0,0,0,0,0,0,0,0,0
 as.integer( unlist( strsplit(INI1$Map$port1, ,) ) )
 [1] 0 0 0 0 0 0 0 0 0 0 0 0

= = = = =
Sample input file, sample.ini:

[SystemSetup]
OlfactometerCode=3
[Files]
prelog0=Part0.txt
date0=2:06:27.461 PM 6/9/2007
note0=group1-1
name0=group1
prelog1=Part1.txt
date1=2:09:16.809 PM 6/9/2007
note1=group1-1
name1=group1-1
[DefaultLevels]
FC00=50
FC01=100
FC02=50
FC10=50
[OdorNames]
port0=None
port1=None
[Map]
port0=0,0,0,0,0,0,0,0,0,0,0,0
port1=0,0,0,0,0,0,0,0,0,0,0,0
port2=0,0,0,0,0,0,0,0,0,0,0,0

= = = = =

Thanks again, Gabor!

efg

Earl F. Glynn
Scientific Programmer
Stowers Institute for Medical Research

__
R-help@stat.math.ethz.ch 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.


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

2007-06-13 Thread ngottlieb
Earl:

Really depends on the need. XML yes can get crazy (having had to deal
with some
ugly XML).

One can do a correctly formatted XML, that parses via the DOM which does
not mean well formatted XML. It's all 
a matter of design and data structures.

XML advantages: one can define own data types with attributes,
do data validation and nice searching with XPATH which
Is a whole subject in itself.

Sounds like XML is overkill for what you need.

Based on what you indicated, since not an R expert, writing a
Simple C function or Fortran routine would be best way to go,
Also gives you re-usable code if you are processing .ini
Files outside of the R environment.


If you program in Visual Basic or C you can develop a simple
DLL to call the old .ini functions which are document
On MSDN (Microsoft Developers Network). 

However, Looks like the R experts from threads gave a nice solution
using R.

Neil

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Earl F. Glynn
Sent: Wednesday, June 13, 2007 2:57 PM
To: r-help@stat.math.ethz.ch
Subject: Re: [R] Read Windows-like .INI files into R data structure?

[EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]...
 .Ini files are, for lack of a better description, ancient.

In this case a device is creating the INI files as part of an
experiment, so 
the file format cannot be changed (at least easily).

I've looked at XML files from time to time and I'm amazed more don't 
complain how bloated, if not wasteful, they are.  I've seen XML files
that 
were megabytes long when they held kilobytes worth of data.  INI files
may 
be ancient, but they can be efficient and effective compared with XML.
In 
some cases, newer may not really be better (but newer may have the 
momentum behind it).


Gabor Grothendieck [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]...
 In thinking about this a bit more here is an even shorter solution
where
 Lines.raw is as before:

 # Lines - readLines(myfile.ini)
 Lines - readLines(textConnection(Lines.raw))
 Lines2 - chartr([], ==, Lines)
 DF - read.table(textConnection(Lines2), as.is = TRUE, sep = =, fill
= 
 TRUE)
 L - DF$V1 == 
 subset(transform(DF, V3 = V2[which(L)[cumsum(L)]])[1:3], V1 != )

Thanks for your helpful suggestions, Gabor.  Perhaps your zoo option
is 
more elegant, but I try to use as few packages as possible, so this
option 
seemed the best for me.

Since in my problem the structure of the INI sections is almost static
and 
always present, I extended your example to create an in-memory list of 
everything in the INI file with this function:

# Prototype of how to read INI files to process olfactometer data
# efg, 13 June 2007
# Thanks to Gabor Grothendieck for helpful suggestions in the R-Help
# mailing list on how to parse the INI file.
Parse.INI - function(INI.filename)
{
  connection - file(INI.filename)
  Lines  - readLines(connection)
  close(connection)

  Lines - chartr([], ==, Lines)  # change section headers

  connection - textConnection(Lines)
  d - read.table(connection, as.is = TRUE, sep = =, fill = TRUE)
  close(connection)

  L - d$V1 == # location of section breaks
  d - subset(transform(d, V3 = V2[which(L)[cumsum(L)]])[1:3],
   V1 != )

  ToParse  - paste(INI.list$, d$V3, $,  d$V1,  - ',
d$V2, ', sep=)

  INI.list - list()
  eval(parse(text=ToParse))

  return(INI.list)
}


Here's an example of using the above function (I'll put the sample input

file below):

INI1 - Parse.INI(sample.ini)

# Explore INI contents
summary(INI1)

INI1$SystemSetup$OlfactometerCode
INI1$DefaultLevels
unlist(INI1$DefaultLevels)
INI1$Map

INI1$Map$port1
as.integer( unlist( strsplit(INI1$Map$port1, ,) ) )

= = = = =
Sample output:

 INI1 - Parse.INI(sample.ini)

 # Explore INI contents
 summary(INI1)
  Length Class  Mode
SystemSetup   1  -none- list
Files 8  -none- list
DefaultLevels 4  -none- list
OdorNames 2  -none- list
Map   3  -none- list

 INI1$SystemSetup$OlfactometerCode
[1] 3
 INI1$DefaultLevels
$FC00
[1] 50

$FC01
[1] 100

$FC02
[1] 50

$FC10
[1] 50

 unlist(INI1$DefaultLevels)
 FC00  FC01  FC02  FC10
 50 100  50  50
 INI1$Map
$port0
[1] 0,0,0,0,0,0,0,0,0,0,0,0

$port1
[1] 0,0,0,0,0,0,0,0,0,0,0,0

$port2
[1] 0,0,0,0,0,0,0,0,0,0,0,0


 INI1$Map$port1
[1] 0,0,0,0,0,0,0,0,0,0,0,0
 as.integer( unlist( strsplit(INI1$Map$port1, ,) ) )
 [1] 0 0 0 0 0 0 0 0 0 0 0 0

= = = = =
Sample input file, sample.ini:

[SystemSetup]
OlfactometerCode=3
[Files]
prelog0=Part0.txt
date0=2:06:27.461 PM 6/9/2007
note0=group1-1
name0=group1
prelog1=Part1.txt
date1=2:09:16.809 PM 6/9/2007
note1=group1-1
name1=group1-1
[DefaultLevels]
FC00=50
FC01=100
FC02=50
FC10=50
[OdorNames]
port0=None
port1=None
[Map]
port0=0,0,0,0,0,0,0,0,0,0,0,0
port1=0,0,0,0,0,0,0,0,0,0,0,0
port2=0,0,0,0,0,0,0,0,0,0,0,0

= = = = =

Thanks again, Gabor!

efg

Earl F. Glynn
Scientific Programmer
Stowers Institute

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

2007-06-13 Thread Gabor Grothendieck
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))
do.call(rbind, lapply(strsplit(Lines, =), f))


On 6/12/07, Gabor Grothendieck [EMAIL PROTECTED] wrote:
 Here is yet another simplification.  This one uses na.locf from the zoo
 package to shorten it further and also make it easier to understand.

 Below we have one line to read in the .ini file, one line to transform the
 characters [ and ] to = and =, the read.table line parses the result and
 the next line carries forward the section names and removes the section
 lines. Lines.raw is as before:

 library(zoo)

 # Lines - readLines(myfile.ini)
 Lines - readLines(textConnection(Lines.raw))
 Lines2 - chartr([], ==, Lines)
 DF - read.table(textConnection(Lines2), as.is = TRUE, sep = =, fill = TRUE)
 subset(transform(DF, V3 = na.locf(ifelse(V1 == , V2, NA))), V1 != )


 On 6/12/07, Gabor Grothendieck [EMAIL PROTECTED] wrote:
  In thinking about this a bit more here is an even shorter solution where
  Lines.raw is as before:
 
  # Lines - readLines(myfile.ini)
  Lines - readLines(textConnection(Lines.raw))
  Lines2 - chartr([], ==, Lines)
  DF - read.table(textConnection(Lines2), as.is = TRUE, sep = =, fill = 
  TRUE)
  L - DF$V1 == 
  subset(transform(DF, V3 = V2[which(L)[cumsum(L)]])[1:3], V1 != )
 
 
  On 6/12/07, Gabor Grothendieck [EMAIL PROTECTED] wrote:
   Here is some code. It replaces [ and ] with = sign and reads the result
   into a data frame, DF.  DF2 is similar except the section is now in V3.
   DF3 is like like DF2 except sections are carried forward and finally
   we remove the rows which only had sections.
  
   Lines.raw - [Section1]
   var1=value1
   var2=value2
   [Section2]
   A=value3
   B=value4
   
  
   Lines - readLines(textConnection(Lines.raw))
   Lines2 - chartr([], ==, Lines)
   DF - read.table(textConnection(Lines2), as.is = TRUE, sep = =, fill = 
   TRUE)
   DF2 - transform(DF, V3 = ifelse(V1 == , V2, NA))
   L - !is.na(DF2$V3)
   DF3 - transform(DF2, V3 = V3[c(NA, which(L))[cumsum(L)+1]])
   subset(DF3, V1 != )
  
   The result is:
  
  V1 V2   V3
   2 var1 value1 Section1
   3 var2 value2 Section1
   5A value3 Section2
   6B value4 Section2
  
  
   On 6/12/07, Earl F. Glynn [EMAIL PROTECTED] wrote:
I need to process some datasets where the configuration information was
stored in .INI-like files, i.e., text files with sections like this:
   
[Section1]
var1=value1
var2=value2
[Section2]
A=value3
B=value4
   
...
   
From Google and other searches I haven't found any package, or function
within a package, that reads .INI files into an R list, or other data
structure.
   
   
   
Any suggestions, or do I need to write my own?
   
efg
   
Earl F. Glynn
Stowers Institute for Medical Research
   
__
R-help@stat.math.ethz.ch 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.
   
  
 


__
R-help@stat.math.ethz.ch 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.


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

2007-06-12 Thread Earl F. Glynn
I need to process some datasets where the configuration information was 
stored in .INI-like files, i.e., text files with sections like this:

[Section1]
var1=value1
var2=value2
[Section2]
A=value3
B=value4

...

From Google and other searches I haven't found any package, or function 
within a package, that reads .INI files into an R list, or other data 
structure.



Any suggestions, or do I need to write my own?

efg

Earl F. Glynn
Stowers Institute for Medical Research

__
R-help@stat.math.ethz.ch 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.


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

2007-06-12 Thread ngottlieb
Earl:

.Ini files are, for lack of a better description, ancient.

There are old windows functions such as GetProfileString.
However you will have to make reference to load these from the windows
Kernel.dll.
Probably not worth the effort to code really old things as .ini files.

From what I see of packages, better to change these files to XML format
see if the XML package on CRAN will solve your requirement.

The section names would be top nodes with
XML tags containing the data at the sub level. XML is really
The best way to go; get away from .ini files.

Look at the XML package, reading nodes, parsing DOM.

Neil 

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Earl F. Glynn
Sent: Tuesday, June 12, 2007 12:48 PM
To: r-help@stat.math.ethz.ch
Subject: [R] Read Windows-like .INI files into R data structure?

I need to process some datasets where the configuration information was
stored in .INI-like files, i.e., text files with sections like this:

[Section1]
var1=value1
var2=value2
[Section2]
A=value3
B=value4

...

From Google and other searches I haven't found any package, or function
within a package, that reads .INI files into an R list, or other data
structure.



Any suggestions, or do I need to write my own?

efg

Earl F. Glynn
Stowers Institute for Medical Research

__
R-help@stat.math.ethz.ch 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.


 
 
This information is being sent at the recipient's request or with their 
specific understanding. The recipient acknowledges that by sending this 
information via electronic means, there is no absolute assurance that the 
information will be free from third party access, use, or further 
dissemination. This e-mail contains information that is privileged and/or 
confidential and may be subject to legal restrictions and penalties regarding 
its unauthorized disclosure or other use. You are prohibited from copying, 
distributing or otherwise using this information if you are not the intended 
recipient. Past performance is not necessarily indicative of future results. 
This is not an offer of or the solicitation for any security which will be made 
only by private placement memorandum that may be obtained from the applicable 
hedge fund. If you have received this e-mail in error, please notify us 
immediately by return e-mail and delete this e-mail and all attachments from 
your system. Than!
 k You.

__
R-help@stat.math.ethz.ch 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.


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

2007-06-12 Thread Gabor Grothendieck
Here is some code. It replaces [ and ] with = sign and reads the result
into a data frame, DF.  DF2 is similar except the section is now in V3.
DF3 is like like DF2 except sections are carried forward and finally
we remove the rows which only had sections.

Lines.raw - [Section1]
var1=value1
var2=value2
[Section2]
A=value3
B=value4


Lines - readLines(textConnection(Lines.raw))
Lines2 - chartr([], ==, Lines)
DF - read.table(textConnection(Lines2), as.is = TRUE, sep = =, fill = TRUE)
DF2 - transform(DF, V3 = ifelse(V1 == , V2, NA))
L - !is.na(DF2$V3)
DF3 - transform(DF2, V3 = V3[c(NA, which(L))[cumsum(L)+1]])
subset(DF3, V1 != )

The result is:

V1 V2   V3
2 var1 value1 Section1
3 var2 value2 Section1
5A value3 Section2
6B value4 Section2


On 6/12/07, Earl F. Glynn [EMAIL PROTECTED] wrote:
 I need to process some datasets where the configuration information was
 stored in .INI-like files, i.e., text files with sections like this:

 [Section1]
 var1=value1
 var2=value2
 [Section2]
 A=value3
 B=value4

 ...

 From Google and other searches I haven't found any package, or function
 within a package, that reads .INI files into an R list, or other data
 structure.



 Any suggestions, or do I need to write my own?

 efg

 Earl F. Glynn
 Stowers Institute for Medical Research

 __
 R-help@stat.math.ethz.ch 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.


__
R-help@stat.math.ethz.ch 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.


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

2007-06-12 Thread Gabor Grothendieck
In thinking about this a bit more here is an even shorter solution where
Lines.raw is as before:

# Lines - readLines(myfile.ini)
Lines - readLines(textConnection(Lines.raw))
Lines2 - chartr([], ==, Lines)
DF - read.table(textConnection(Lines2), as.is = TRUE, sep = =, fill = TRUE)
L - DF$V1 == 
subset(transform(DF, V3 = V2[which(L)[cumsum(L)]])[1:3], V1 != )


On 6/12/07, Gabor Grothendieck [EMAIL PROTECTED] wrote:
 Here is some code. It replaces [ and ] with = sign and reads the result
 into a data frame, DF.  DF2 is similar except the section is now in V3.
 DF3 is like like DF2 except sections are carried forward and finally
 we remove the rows which only had sections.

 Lines.raw - [Section1]
 var1=value1
 var2=value2
 [Section2]
 A=value3
 B=value4
 

 Lines - readLines(textConnection(Lines.raw))
 Lines2 - chartr([], ==, Lines)
 DF - read.table(textConnection(Lines2), as.is = TRUE, sep = =, fill = TRUE)
 DF2 - transform(DF, V3 = ifelse(V1 == , V2, NA))
 L - !is.na(DF2$V3)
 DF3 - transform(DF2, V3 = V3[c(NA, which(L))[cumsum(L)+1]])
 subset(DF3, V1 != )

 The result is:

V1 V2   V3
 2 var1 value1 Section1
 3 var2 value2 Section1
 5A value3 Section2
 6B value4 Section2


 On 6/12/07, Earl F. Glynn [EMAIL PROTECTED] wrote:
  I need to process some datasets where the configuration information was
  stored in .INI-like files, i.e., text files with sections like this:
 
  [Section1]
  var1=value1
  var2=value2
  [Section2]
  A=value3
  B=value4
 
  ...
 
  From Google and other searches I haven't found any package, or function
  within a package, that reads .INI files into an R list, or other data
  structure.
 
 
 
  Any suggestions, or do I need to write my own?
 
  efg
 
  Earl F. Glynn
  Stowers Institute for Medical Research
 
  __
  R-help@stat.math.ethz.ch 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.
 


__
R-help@stat.math.ethz.ch 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.


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

2007-06-12 Thread Gabor Grothendieck
Here is yet another simplification.  This one uses na.locf from the zoo
package to shorten it further and also make it easier to understand.

Below we have one line to read in the .ini file, one line to transform the
characters [ and ] to = and =, the read.table line parses the result and
the next line carries forward the section names and removes the section
lines. Lines.raw is as before:

library(zoo)

# Lines - readLines(myfile.ini)
Lines - readLines(textConnection(Lines.raw))
Lines2 - chartr([], ==, Lines)
DF - read.table(textConnection(Lines2), as.is = TRUE, sep = =, fill = TRUE)
subset(transform(DF, V3 = na.locf(ifelse(V1 == , V2, NA))), V1 != )


On 6/12/07, Gabor Grothendieck [EMAIL PROTECTED] wrote:
 In thinking about this a bit more here is an even shorter solution where
 Lines.raw is as before:

 # Lines - readLines(myfile.ini)
 Lines - readLines(textConnection(Lines.raw))
 Lines2 - chartr([], ==, Lines)
 DF - read.table(textConnection(Lines2), as.is = TRUE, sep = =, fill = TRUE)
 L - DF$V1 == 
 subset(transform(DF, V3 = V2[which(L)[cumsum(L)]])[1:3], V1 != )


 On 6/12/07, Gabor Grothendieck [EMAIL PROTECTED] wrote:
  Here is some code. It replaces [ and ] with = sign and reads the result
  into a data frame, DF.  DF2 is similar except the section is now in V3.
  DF3 is like like DF2 except sections are carried forward and finally
  we remove the rows which only had sections.
 
  Lines.raw - [Section1]
  var1=value1
  var2=value2
  [Section2]
  A=value3
  B=value4
  
 
  Lines - readLines(textConnection(Lines.raw))
  Lines2 - chartr([], ==, Lines)
  DF - read.table(textConnection(Lines2), as.is = TRUE, sep = =, fill = 
  TRUE)
  DF2 - transform(DF, V3 = ifelse(V1 == , V2, NA))
  L - !is.na(DF2$V3)
  DF3 - transform(DF2, V3 = V3[c(NA, which(L))[cumsum(L)+1]])
  subset(DF3, V1 != )
 
  The result is:
 
 V1 V2   V3
  2 var1 value1 Section1
  3 var2 value2 Section1
  5A value3 Section2
  6B value4 Section2
 
 
  On 6/12/07, Earl F. Glynn [EMAIL PROTECTED] wrote:
   I need to process some datasets where the configuration information was
   stored in .INI-like files, i.e., text files with sections like this:
  
   [Section1]
   var1=value1
   var2=value2
   [Section2]
   A=value3
   B=value4
  
   ...
  
   From Google and other searches I haven't found any package, or function
   within a package, that reads .INI files into an R list, or other data
   structure.
  
  
  
   Any suggestions, or do I need to write my own?
  
   efg
  
   Earl F. Glynn
   Stowers Institute for Medical Research
  
   __
   R-help@stat.math.ethz.ch 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.
  
 


__
R-help@stat.math.ethz.ch 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.