Earl F. Glynn
2007-Jun-12 16:47 UTC
[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 functionwithin 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
ngottlieb at marinercapital.com
2007-Jun-12 17:15 UTC
[R] Read Windows-like .INI files into R data structure?
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 formatsee 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: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Earl F. Glynn Sent: Tuesday, June 12, 2007 12:48 PM To: r-help at 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 functionwithin 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. -------------------------------------------------------- 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. Thank You.
Gabor Grothendieck
2007-Jun-12 17:42 UTC
[R] Read Windows-like .INI files into R data structure?
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. >
Vladimir Eremeev
2007-Jun-13 08:20 UTC
[R] Read Windows-like .INI files into R data structure?
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.