R,设置循环以连续收集JSON

时间:2017-06-27 20:22:05

标签: json r

我正在尝试设置一个R脚本,该脚本将从OpenSky的REST API连续收集JSON数据(全天每15秒)。

我正在努力调整script meant to collect Car2Go data

我认为我有循环的基础知识,但是,我很困惑自己将所有数据合并到数据帧中,这些数据可以在当天结束时写入csv。

library(tidyverse)
library(jsonlite)

for (day in 1:100){

  flight.df.time <- data.frame()

  for (i in seq(1,5760)){
    rm(flight.df)

    flight.df <- data.frame()

    all_flights <- fromJSON('https://opensky-network.org/api/states/all')

    # Creates df from JSON and filters lat/long to DC Metro Area
    dc_flights <- as.data.frame(all_flights$states) %>%
      mutate(V6 = as.numeric(levels(V6))[V6],
             V7 = as.numeric(levels(V7))[V7]) %>% 
      filter(between(V6, -78.361647, -75.872761),
             between(V7, 38.197760, 39.646129))

    flight.df.time <- rbind(dc_flights, flight.df)

    print(Sys.time())
    Sys.sleep(15)

  }

  write.csv(flight.df.time, file = paste(day, '_flight.csv', sep = ''))
}

是否有人能够帮助我获取循环以持续向数据框添加数据而不像我现在所做的那样覆盖它?谢谢!

1 个答案:

答案 0 :(得分:0)

.btn-system {
  outline: none;
  box-sizing: border-box;
  height: 21px;
  padding: 0px 16px;
  font-size: 13px;
  border: 1px solid #d5d5d5;
  font-weight: 400;
  background-color: #fff;
  letter-spacing: 0.4px;
  border-radius: 4.5px;
  border-top-color: rgb(198, 198, 198);
  border-bottom-color: rgb(170, 170, 170);
  border-left-color: rgb(192, 192, 192);
  border-right-color: rgb(192, 192, 192);
}

.btn-system.btn-active:not([disabled]), .btn-system:active {
  /*color: #fff;
  font-weight: 300;
  letter-spacing: 0.8px;
  padding: 0px 16px;
  border-top-color: rgb(64, 150, 248);
  border-bottom-color: rgb(9, 85, 255);
  border-left-color: rgb(39, 122, 252);
  border-right-color: rgb(39, 122, 252);
  background-image: linear-gradient(rgb(94, 168, 249), rgb(14, 117, 255));*/
  letter-spacing: 0.8px;
  color: #fff;
  font-weight: 300;
  padding: 0px 17px 1px 16px;
  border-top-color: rgb(64, 150, 248);
  border-bottom-color: rgb(9, 85, 255);
  border-left-color: rgb(39, 122, 252);
  border-right-color: rgb(39, 122, 252);
  background-image: linear-gradient(rgb(94, 168, 249), rgb(14, 117, 255));
}
.btn-system:active {
  border-top-color: rgb(30, 114, 254);
  border-bottom-color: rgb(3, 56, 216);
  border-left-color: rgb(16, 82, 233);
  border-right-color: rgb(17, 82, 227);
  background-image: linear-gradient(rgb(64, 140, 253), rgb(11, 93, 224));
}