无限递归Shiny R

时间:2017-06-12 19:33:11

标签: json r for-loop shiny infinite-recursion

我想在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变量的初始化工作正常,但是当我尝试添加循环时它会返回错误。有人可以帮忙吗?

非常感谢你的时间。

1 个答案:

答案 0 :(得分: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 
})