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

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Tue, 12 Jun 2007 19:23:20 -0400

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 <ggrothendieck_at_gmail.com> 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
> 5 A value3 Section2
> 6 B value4 Section2
>
>
> On 6/12/07, Earl F. Glynn <efg_at_stowers-institute.org> 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_at_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_at_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. Received on Tue 12 Jun 2007 - 23:30:42 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 Wed 13 Jun 2007 - 19:32:01 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.