json - Iteratively read a fixed number of lines into R -
i have josn file i'm working contains multiple json objects in single file. r unable read file whole. since each object occurs @ regular intervals, iteratively read fixed number of lines r.
there number of questions on reading single lines r have been unable extend these solutions fixed number of lines. problem need read 16 lines r @ time (eg 1-16, 17-32 etc)
i have tried using loop can't seem syntax right:
## file file <- "results.json" ## create connection con <- file(description=file, open="r") ## loop on file connection for(i in 1:1000) { tmp <- scan(file=con, nlines=16, quiet=true) data[i] <- fromjson(tmp) }
the file contains on 1000 objects of form:
{ "object": [ [ "a", 0 ], [ "b", 2 ], [ "c", 2 ] ] }
with @tomtom inspiration able find solution.
## file file <- "results.json" ## loop on file for(i in 1:1000) { tmp <- paste(scan(file=file, what="character", sep="\n", nlines=16, skip=(i-1)*16, quiet=true),collapse=" ") assign(x = paste("data", i, sep = "_"), value = fromjson(tmp)) }
i couldn't create connection each time tried connection close before file had been read. got rid of step.
i had include what="character"
variable scan() seems expect number default.
i included sep="\n"
, paste() , collapse=" "
create single string rather vector of characters scan() creates default.
finally changed final assignment operator have bit more control on names of output.
Comments
Post a Comment