Hi,
I extended the code to generalize and create a list which contains all data in the format as you present it. It should work if the structure of your file is not deviating too much from your example
The file
#Comment
#Comment
#Comment
[specchannel10]
iVersion=TRUE
fFmax=test
fRPMean=18.46
[specdata0]
4.7287827E-5
3.806267E-5
2.3926008E-5
2.3086282E-5
3.781592E-5
5.438561E-5
5.82364E-5
1.0197797E-4
1.5383417E-5
2.9165532E-5
4.7294132E-5
6.0461047E-5
3.9730767E-5
#--finish--
The processing
library(stringr)
library(readr)
myFile = readLines("test.txt")
myResult = list()
#Find the position of the variables that are between []
#We add the last line number as a position as well
vars = c(which(str_detect(myFile, "^\\[.*\\]\\s*$") == T), length(myFile))
#Get the content for each variable
for(i in 1:(length(vars)-1)){
myData = myFile[vars[i]:(vars[i +1] - 1)]
#Remove lines that are comments or blank
myData = myData[!str_detect(myData, "^\\s*#|^\\s*$")]
#If the content is a list of variables, create them as a list
if(str_detect(myData[2], "=")){
content = str_split(myData[-1], "=")
result = lapply(lapply(content, "[", 2), parse_guess)
names(result) = sapply(content, "[", 1)
} else {
#If the content just a vector of data, extract it
result = parse_guess(myData[-1])
}
#Create the variable as a list item and assign the content
myResult[[str_remove_all(myData[1], "\\[|\\]")]] = result
}
The result
> myResult
$specchannel10
$specchannel10$iVersion
[1] TRUE
$specchannel10$fFmax
[1] "test"
$specchannel10$fRPMean
[1] 18.46
$specdata0
[1] 4.728783e-05 3.806267e-05 2.392601e-05 2.308628e-05 3.781592e-05 5.438561e-05 5.823640e-05
[8] 1.019780e-04 1.538342e-05 2.916553e-05 4.729413e-05 6.046105e-05 3.973077e-05
This code generates a list in which every variable between square brackets is a sublist and the content of each sublist is either a list of variables or a vector of data.
Hope this helps,
PJ