R can process input as a stream, it's just not the default. If readLines() or read.table() or similar are given a file path as a character value, they'll create the connection, read from it, and close it when done. To keep it open, you have to create the connection object yourself.
For your example, let's say the sample text is stored in a file named sample.txt. First, define a function that takes the 7 lines for each chunk and returns a nice object. I'm not sure what you want, so I'm thinking a list with 3 items: name, parameters as a named numeric, and a matrix.
parse_block <- function(block_lines) {
# Block name is just first line
name <- block_lines[1]
# Create the named parameters vector
# Includes defensive programming in case the line is blank
param_parts <- strsplit(block_lines[2], "\\s+")[[1]]
if (length(param_parts)) {
name_indices <- seq(1, length(param_parts), by = 2)
value_indices <- seq(2, length(param_parts), by = 2)
parameters <- as.numeric(param_parts[value_indices])
names(parameters) <- param_parts[name_indices]
} else {
parameters <- numeric(0)
}
# Split the array lines by spacing, then use them for a matrix
mat_strings <- unlist(strsplit(block_lines[3:7], "\\s+"))
mat <- matrix(
as.numeric(mat_strings),
nrow = 5,
byrow = TRUE
)
# Compose the output
list(
name = name,
parameters = parameters,
values = mat
)
}
Next, connect to the file. Read the top line, which helpfully gives the length of the result. We'll store the outputs in a list.
conn <- file("sample.txt", open = "r")
block_count <- scan(conn, nlines = 1)
block_count
# [1] 3
output <- vector("list", block_count)
Now we can read the connection in a loop, each time reading 7 lines from where the previous loop left off. This is because conn is not being closed.
for (ii in seq_len(block_count)) {
next_chunk <- readLines(conn, n = 7)
output[[ii]] <- parse_block(next_chunk)
}
close(conn)
output[[3]]
# $name
# [1] "PAR03"
#
# $parameters
# CONST F4.0
# 3 6
#
# $values
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] 123 123 123 123 123 123 123 123 123 123
# [2,] 999 999 999 999 999 999 999 999 999 999
# [3,] 111 111 111 111 111 111 111 111 111 111
# [4,] 111 111 111 111 111 111 111 111 111 111
# [5,] 999 999 999 999 999 999 999 999 999 999