0

Shiny で for サイクルを書こうとしています。コードは json ファイルをインポートし、すべての出力を 1 つの大きな data.frame に結合する必要があります。私のアプローチは、最初の行からのjson出力で変数を初期化し、rbind関数を実行してループを実行して、ファイルを完成できるようにすべてを下部に追加することです。

通常の R 環境の関数は完全に機能し、期待どおりの結果を返します。ここにコード:

trips<-data.frame(
  time.start=as.numeric((substr((fromJSON(txt=res.clean[1,2])$recorded_at),1,10)))[1:(length(as.numeric((substr((fromJSON(txt=res.clean[1,2])$recorded_at),1,10))))-1)], 
  time.end=as.numeric((substr((fromJSON(txt=res.clean[1,2])$recorded_at),1,10)))[2:(length(as.numeric((substr((fromJSON(txt=res.clean[1,2])$recorded_at),1,10)))))], 
  long.start=matrix((as.vector(unlist(fromJSON(txt=res.clean[1,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[1:(length(as.vector(unlist(fromJSON(txt=res.clean[1,2])$loc, recursive = FALSE)))-1),1],
  long.end=matrix((as.vector(unlist(fromJSON(txt=res.clean[1,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[2:(length(as.vector(unlist(fromJSON(txt=res.clean[1,2])$loc, recursive = FALSE)))),1],
  lat.start=matrix((as.vector(unlist(fromJSON(txt=res.clean[1,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[1:(length(as.vector(unlist(fromJSON(txt=res.clean[1,2])$loc, recursive = FALSE)))-1),2],
  lat.end=matrix((as.vector(unlist(fromJSON(txt=res.clean[1,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[2:(length(as.vector(unlist(fromJSON(txt=res.clean[1,2])$loc, recursive = FALSE)))),2]
)

for(i in 2:dim(res.clean)[1]){
trips<-rbind(trips,data.frame(
  time.start=as.numeric((substr((fromJSON(txt=res.clean[i,2])$recorded_at),1,10)))[1:(length(as.numeric((substr((fromJSON(txt=res.clean[i,2])$recorded_at),1,10))))-1)], 
  time.end=as.numeric((substr((fromJSON(txt=res.clean[i,2])$recorded_at),1,10)))[2:(length(as.numeric((substr((fromJSON(txt=res.clean[i,2])$recorded_at),1,10)))))], 
  long.start=matrix((as.vector(unlist(fromJSON(txt=res.clean[i,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[1:(length(as.vector(unlist(fromJSON(txt=res.clean[i,2])$loc, recursive = FALSE)))-1),1],
  long.end=matrix((as.vector(unlist(fromJSON(txt=res.clean[i,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[2:(length(as.vector(unlist(fromJSON(txt=res.clean[i,2])$loc, recursive = FALSE)))),1],
  lat.start=matrix((as.vector(unlist(fromJSON(txt=res.clean[i,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[1:(length(as.vector(unlist(fromJSON(txt=res.clean[i,2])$loc, recursive = FALSE)))-1),2],
  lat.end=matrix((as.vector(unlist(fromJSON(txt=res.clean[i,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[2:(length(as.vector(unlist(fromJSON(txt=res.clean[i,2])$loc, recursive = FALSE)))),2]
))}

Shiny 環境でアプローチを複製しようとすると、エラーが発生します

Error: evaluation nested too deeply: infinite recursion / options(expressions=)?

ここにコード:

trips<-reactive({data.frame(
    time.start=as.numeric((substr((fromJSON(txt=res.clean()[1,2])$recorded_at),1,10)))[1:(length(as.numeric((substr((fromJSON(txt=res.clean()[1,2])$recorded_at),1,10))))-1)], 
    time.end=as.numeric((substr((fromJSON(txt=res.clean()[1,2])$recorded_at),1,10)))[2:(length(as.numeric((substr((fromJSON(txt=res.clean()[1,2])$recorded_at),1,10)))))], 
    long.start=matrix((as.vector(unlist(fromJSON(txt=res.clean()[1,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[1:(length(as.vector(unlist(fromJSON(txt=res.clean()[1,2])$loc, recursive = FALSE)))-1),1],
    long.end=matrix((as.vector(unlist(fromJSON(txt=res.clean()[1,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[2:(length(as.vector(unlist(fromJSON(txt=res.clean()[1,2])$loc, recursive = FALSE)))),1],
    lat.start=matrix((as.vector(unlist(fromJSON(txt=res.clean()[1,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[1:(length(as.vector(unlist(fromJSON(txt=res.clean()[1,2])$loc, recursive = FALSE)))-1),2],
    lat.end=matrix((as.vector(unlist(fromJSON(txt=res.clean()[1,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[2:(length(as.vector(unlist(fromJSON(txt=res.clean()[1,2])$loc, recursive = FALSE)))),2]
  )})

  trips<-reactive({

    for(i in 1:dim(res.clean())[1]){
    rbind(trips(),data.frame(
      time.start=as.numeric((substr((fromJSON(txt=res.clean()[i,2])$recorded_at),1,10)))[1:(length(as.numeric((substr((fromJSON(txt=res.clean()[i,2])$recorded_at),1,10))))-1)], 
      time.end=as.numeric((substr((fromJSON(txt=res.clean()[i,2])$recorded_at),1,10)))[2:(length(as.numeric((substr((fromJSON(txt=res.clean()[i,2])$recorded_at),1,10)))))], 
      long.start=matrix((as.vector(unlist(fromJSON(txt=res.clean()[i,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[1:(length(as.vector(unlist(fromJSON(txt=res.clean()[i,2])$loc, recursive = FALSE)))-1),1],
      long.end=matrix((as.vector(unlist(fromJSON(txt=res.clean()[i,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[2:(length(as.vector(unlist(fromJSON(txt=res.clean()[i,2])$loc, recursive = FALSE)))),1],
      lat.start=matrix((as.vector(unlist(fromJSON(txt=res.clean()[i,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[1:(length(as.vector(unlist(fromJSON(txt=res.clean()[i,2])$loc, recursive = FALSE)))-1),2],
      lat.end=matrix((as.vector(unlist(fromJSON(txt=res.clean()[i,2])$loc, recursive = TRUE))),ncol=2,byrow=T)[2:(length(as.vector(unlist(fromJSON(txt=res.clean()[i,2])$loc, recursive = FALSE)))),2]
    ))}
})

  output$table <- renderTable(trips())

trips 変数の初期化は正常に機能しますが、サイクルを追加しようとするとエラーが返されます。誰でも助けてもらえますか?

どうぞよろしくお願いいたします。

4

1 に答える 1