0

我正在尝试在 Shiny 中编写一个 for 循环。代码应该导入一个 json 文件并将所有输出合并到一个大 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

0

关于什么:

trips<-reactive({
  data <- 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]){
    data <-rbind(data ,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]
    ))}

  data 
})
于 2017-06-12T21:03:03.250 回答