Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 为什么一次只能读取一个.json文件?_R_For Loop_Jsonlite - Fatal编程技术网

R 为什么一次只能读取一个.json文件?

R 为什么一次只能读取一个.json文件?,r,for-loop,jsonlite,R,For Loop,Jsonlite,我有500+个.json文件,我正试图从中获取特定元素。我不明白为什么我一次只能读一本书 这项工作: library (jsonlite) files<-list.files(‘~/JSON’) file1<-fromJSON(readLines(‘~/JSON/file1.json),flatten=TRUE) result<-as.data.frame(source=file1$element$subdata$data) 还更新了函数,这次出现UTF*错误。。。 文件 f

我有500+个.json文件,我正试图从中获取特定元素。我不明白为什么我一次只能读一本书

这项工作:

library (jsonlite)
files<-list.files(‘~/JSON’)
file1<-fromJSON(readLines(‘~/JSON/file1.json),flatten=TRUE)
result<-as.data.frame(source=file1$element$subdata$data)
还更新了函数,这次出现UTF*错误。。。
文件
  • for
    循环从不返回任何内容,因此必须自己保存所有有价值的数据
  • 您将
    称为.data.frame(i)
    ,它创建的框架正好包含一个元素,即文件名,可能不是您想要保留的内容
  • (次要)使用来自JSON的
    (文件(i),…)
  • 由于您希望将这些内容捕获到一个帧中,因此我建议如下:

    out <- lapply(files, function(fn) {
      o <- fromJSON(file(fn), flatten = TRUE)
      as.data.frame(o)$element$subdata$data
    })
    allout <- do.call(rbind.data.frame, out)
    ### alternatives:
    allout <- dplyr::bind_rows(out)
    allout <- data.table::rbindlist(out)
    
    out
    
  • for
    循环从不返回任何内容,因此必须自己保存所有有价值的数据
  • 您将
    称为.data.frame(i)
    ,它创建的框架正好包含一个元素,即文件名,可能不是您想要保留的内容
  • (次要)使用来自JSON的
    (文件(i),…)
  • 由于您希望将这些内容捕获到一个帧中,因此我建议如下:

    out <- lapply(files, function(fn) {
      o <- fromJSON(file(fn), flatten = TRUE)
      as.data.frame(o)$element$subdata$data
    })
    allout <- do.call(rbind.data.frame, out)
    ### alternatives:
    allout <- dplyr::bind_rows(out)
    allout <- data.table::rbindlist(out)
    

    out感谢您的回复。我要试试这个!顺便说一句,你知道为什么.json(fromJSON)在一个geoup操作中给出错误,但在调用个人时却没有给出错误吗?我不知道“组操作”是什么意思。对不起,就像我试图运行“fromJSON”并遍历文件列表时一样。由于通常与非UTF-8关联的无效字符字节而返回错误。是与
    fromJSON(文件(f))
    还是
    fromJSON(读取行(f))
    ?我不知道这是否有区别。这样的错误通常与单独执行或在某种形式的循环(可能还有一个新问题)中执行无关。您的更新不起作用-但是,它帮助我找到了解决方案:)!我的猜测是,如果.json文件被读取为pretty print vice raw,那么您的解决方案(以及我以前的尝试)将起作用——这是我以前的尝试和您的建议都被挂断的错误。我将要添加到我原来的帖子中,请看一看!感谢您的回复。我要试试这个!顺便说一句,你知道为什么.json(fromJSON)在一个geoup操作中给出错误,但在调用个人时却没有给出错误吗?我不知道“组操作”是什么意思。对不起,就像我试图运行“fromJSON”并遍历文件列表时一样。由于通常与非UTF-8关联的无效字符字节而返回错误。是与
    fromJSON(文件(f))
    还是
    fromJSON(读取行(f))
    ?我不知道这是否有区别。这样的错误通常与单独执行或在某种形式的循环(可能还有一个新问题)中执行无关。您的更新不起作用-但是,它帮助我找到了解决方案:)!我的猜测是,如果.json文件被读取为pretty print vice raw,那么您的解决方案(以及我以前的尝试)将起作用——这是我以前的尝试和您的建议都被挂断的错误。我将要添加到我原来的帖子中,请看一看!
    json<-readLines('~/JSON')
    jsonread<-fromJSON(json)
    jsondf<-as.data.frame(jsonread$element$subdata$data)
    #returns a dataframe with the correct information
    
    files<-list.files('~/JSON')
    for (i in files){
    a<-readLines(i)
    o<-fromJSON(file(a),flatten=TRUE)
    as.data.frame(i)$element$subdata}
    
    #keeping the same 'files' variable as earlier
    raw_data<-lapply(files,readLines)
    dat<-do.call(rbind,raw_data)
    dat2<-as.data.frame(dat,stringsasFactors=FALSE)
    #check to see json contents were read-in
    dat2[1,1]
    
    library(tidyr)
    dat3<-separate_rows(dat2,sep='')
    x<-unlist(raw_data)
    x<-gsub('[[:punct:]]', ' ',x)
    
    #Identify elements wanted in original .json and apply regex
    y<-regmatches(x,regexc('.*SubElement2 *(.*?) *Text.*',x))
    
    out <- lapply(files, function(fn) {
      o <- fromJSON(file(fn), flatten = TRUE)
      as.data.frame(o)$element$subdata$data
    })
    allout <- do.call(rbind.data.frame, out)
    ### alternatives:
    allout <- dplyr::bind_rows(out)
    allout <- data.table::rbindlist(out)