[R] Read Windows-like .INI files into R data structure?
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Jun 13 01:23:20 CEST 2007
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.
> >
>
More information about the R-help
mailing list