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中的循环_R_Loops_For Loop_Shiny_Cycle - Fatal编程技术网

用于R中的循环

用于R中的循环,r,loops,for-loop,shiny,cycle,R,Loops,For Loop,Shiny,Cycle,我试图在Shiny中创建一个应用程序,它以某种方式接收输入并返回输入。该守则应按如下方式运作: 输入是一个表,称为trips,通过以下方式进行: >head(trips) time.start time.end long.start long.end lat.start lat.end 1 1476450598 1476450713 9.03913 9.03924 45.61335 45.61362 2 1476450713 1476450727 9.03924

我试图在Shiny中创建一个应用程序,它以某种方式接收输入并返回输入。该守则应按如下方式运作:

输入是一个表,称为trips,通过以下方式进行:

>head(trips)
  time.start   time.end long.start long.end lat.start  lat.end
1 1476450598 1476450713    9.03913  9.03924  45.61335 45.61362
2 1476450713 1476450727    9.03924  9.03995  45.61362 45.61365
3 1476450727 1476450751    9.03995  9.04005  45.61365 45.61340
4 1476450751 1476450777    9.04005  9.04017  45.61340 45.61406
5 1476450777 1476450873    9.04017  9.03949  45.61406 45.61419
6 1476450873 1476450920    9.03949  9.03496  45.61419 45.61319
操作如下(在标准R环境中)

正如你所看到的,这并没有什么特别的,尽管在R中,Shiny并没有像预期的那样工作。下面是代码:

第一部分是获取初始trips表,所有工作都很顺利

trips<-reactive({
    data.frame(
    time.start=as.numeric((substr(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$recorded_at,1,10)))[1:(length(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$recorded_at)-1)],
    time.end=as.numeric((substr(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$recorded_at,1,10)))[2:(length(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$recorded_at))],
    long.start=matrix((as.vector(unlist(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$loc, recursive = TRUE))),ncol=2,byrow=T)[1:dim(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2])))[1]-1,1],
    long.end=matrix((as.vector(unlist(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$loc, recursive = TRUE))),ncol=2,byrow=T)[2:dim(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2])))[1],1],
    lat.start=matrix((as.vector(unlist(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$loc, recursive = TRUE))),ncol=2,byrow=T)[1:dim(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2])))[1]-1,2],
    lat.end=matrix((as.vector(unlist(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$loc, recursive = TRUE))),ncol=2,byrow=T)[2:dim(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2])))[1],2]
  )[-(cumsum(sapply(lapply(res.clean()[,2],fromJSON), dim)[1,])),]
  })
有什么我遗漏的吗


感谢您的帮助

您正在反复呼叫trips1。每次使用
trips1()
时,它都在运行反应函数。反动剂不是那样的。当您调用
renderTable(trips1())
时,它或多或少地调用了
renderTable(trips1(trips1(…))
谢谢您的帮助,我如何更改代码以使其正常工作?请尝试类似以下操作:
triploop
> head(trips)
  time.start   time.end long.start long.end lat.start  lat.end  distance time.diff      speed color
1 1476450598 1476450713    9.03913  9.03924  45.61335 45.61362  31.25292       115  0.9783524 green
2 1476450713 1476450727    9.03924  9.03995  45.61362 45.61365  55.38651        14 14.2422459 green
3 1476450727 1476450751    9.03995  9.04005  45.61365 45.61340  28.89870        24  4.3348057 green
4 1476450751 1476450777    9.04005  9.04017  45.61340 45.61406  74.06267        26 10.2548313 green
5 1476450777 1476450873    9.04017  9.03949  45.61406 45.61419  54.89125        96  2.0584219 green
6 1476450873 1476450920    9.03949  9.03496  45.61419 45.61319 369.88687        47 28.3317600 green
trips<-reactive({
    data.frame(
    time.start=as.numeric((substr(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$recorded_at,1,10)))[1:(length(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$recorded_at)-1)],
    time.end=as.numeric((substr(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$recorded_at,1,10)))[2:(length(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$recorded_at))],
    long.start=matrix((as.vector(unlist(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$loc, recursive = TRUE))),ncol=2,byrow=T)[1:dim(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2])))[1]-1,1],
    long.end=matrix((as.vector(unlist(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$loc, recursive = TRUE))),ncol=2,byrow=T)[2:dim(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2])))[1],1],
    lat.start=matrix((as.vector(unlist(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$loc, recursive = TRUE))),ncol=2,byrow=T)[1:dim(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2])))[1]-1,2],
    lat.end=matrix((as.vector(unlist(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2]))$loc, recursive = TRUE))),ncol=2,byrow=T)[2:dim(do.call(rbind,lapply(lapply(res.clean()[,2],fromJSON),function(x) x[,1:2])))[1],2]
  )[-(cumsum(sapply(lapply(res.clean()[,2],fromJSON), dim)[1,])),]
  })
trips1<-reactive({

  for (i in 1:dim(trips())[1])

    {
    trips1()$distance[i]<-as.numeric(distm(c(trips()$long.start[i],trips()$lat.start[i]), c(trips()$long.end[i],trips()$lat.end[i]), fun = distHaversine))
    trips1()$time.diff[i]<-as.numeric(difftime(anytime(trips()$time.end[i]),anytime(trips()$time.start[i])),units="secs")
    trips1()$speed[i]<-if (trips()$distance[i] ==0) {0} else{
      (trips1()$distance[i]/as.numeric(difftime(anytime(trips()$time.end[i]),
                                             anytime(trips()$time.start[i])),units="secs"))*3.6}
    trips1()$color[i]<-if(trips()$speed[i] < 50){"green"} else if (trips()$speed[i] < 70){"yellow"}else if (trips()$speed[i] > 70){"red"}
  }

})
output$table <- renderTable(trips1())
invalid (NULL) left side of assignment