我正在尝试使用 R 读取非常大的 json 文件,并且我正在使用带有此推荐的 RJSON 库 json_data <- fromJSON(paste(readLines("myfile.json"), collapse=""))
问题是我收到此错误消息
Error in paste(readLines("myfile.json"), collapse = "") :
无法在 C 函数“R_AllocStringBuffer”中分配内存(2383 Mb)
谁能帮我解决这个问题
最佳答案
好吧,只是分享我关于read json file的经验.的进展
我试图读取 52.8MB、19.7MB、1.3GB、93.9MB、158.5MB json 文件花了我 30 分钟,最后自动恢复 R session ,之后尝试应用并行计算并希望看到进度但失败了。
https://github.com/hadley/plyr/issues/265
然后我尝试添加参数 pagesize = 10000,它的工作效率比以往任何时候都高。好吧,我们只需要读取一次,然后像 saveRDS 一样保存为 RData/Rda/Rds 格式。
> suppressPackageStartupMessages(library('BBmisc'))
> suppressAll(library('jsonlite'))
> suppressAll(library('plyr'))
> suppressAll(library('dplyr'))
> suppressAll(library('stringr'))
> suppressAll(library('doParallel'))
>
> registerDoParallel(cores=16)
>
> ## https://www.kaggle.com/c/yelp-recsys-2013/forums/t/4465/reading-json-files-with-r-how-to
> ## https://class.coursera.org/dsscapstone-005/forum/thread?thread_id=12
> fnames <- c('business','checkin','review','tip','user')
> jfile <- paste0(getwd(),'/yelp_dataset_challenge_academic_dataset/yelp_academic_dataset_',fnames,'.json')
> dat <- llply(as.list(jfile), function(x) stream_in(file(x),pagesize = 10000),.parallel=TRUE)
> dat
list()
> jfile
[1] "/home/ryoeng/Coursera-Data-Science-Capstone/yelp_dataset_challenge_academic_dataset/yelp_academic_dataset_business.json"
[2] "/home/ryoeng/Coursera-Data-Science-Capstone/yelp_dataset_challenge_academic_dataset/yelp_academic_dataset_checkin.json"
[3] "/home/ryoeng/Coursera-Data-Science-Capstone/yelp_dataset_challenge_academic_dataset/yelp_academic_dataset_review.json"
[4] "/home/ryoeng/Coursera-Data-Science-Capstone/yelp_dataset_challenge_academic_dataset/yelp_academic_dataset_tip.json"
[5] "/home/ryoeng/Coursera-Data-Science-Capstone/yelp_dataset_challenge_academic_dataset/yelp_academic_dataset_user.json"
> dat <- llply(as.list(jfile), function(x) stream_in(file(x),pagesize = 10000),.progress='=')
opening file input connection.
Imported 61184 records. Simplifying into dataframe...
closing file input connection.
opening file input connection.
Imported 45166 records. Simplifying into dataframe...
closing file input connection.
opening file input connection.
Found 470000 records...
关于json - 在 R 中读取一个巨大的 json 文件,问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29688946/