rOpengov/mpg,遍历 VIN 号码 returns 错误还是单次使用?

rOpengov/mpg, looping through VIN numbers returns error vs single use?

我正在尝试循环 mpg 包中的 fevehicle() 函数,由:

https://github.com/rOpenGov/mpg

我一直在尝试为该函数提供多个 vinid,甚至在循环之间给该函数 5 秒的休息时间以防万一,但我一直收到 HTTP 错误,即使单独使用,该函数也能正常工作。任何想法可能是什么?下面是代码:

#using a loop
vin = c("19UUA86209A000532", "19UUA86239A021598", "19UUA8F20CA037748", "19UUA8F21CA008002", "19UUA8F21CA017878")
for (i in vin) {
  library(mpg)
  print(i)
  print(substr(i, 13, 17))
  q = substr(i, 13, 17)
  z = feVehicle(q)
  Sys.sleep(5)
  z = t(unlist(z))

}

or
#using lapply to see a difference
lapply(vin, feVehicle)

都抛出以下错误:

[1] "19UUA86209A000532"
[1] "00532"
failed to load HTTP resource
Error in t.default(unlist(z)) : argument is not a matrix
> lapply(vin, feVehicle)
failed to load HTTP resource
failed to load HTTP resource
failed to load HTTP resource
failed to load HTTP resource
failed to load HTTP resource    

但是当我 运行 它一次只打开一个时,它工作正常: mpg::feVehicle(00532)

Vehicle data:
                                    value
atvType                            Diesel
barrels08              16.616739130434784
barrelsA08                            0.0
c240Dscr                             NULL
c240bDscr                            NULL
charge120                             0.0
charge240                             0.0
charge240b                            0.0
city08                                 21
city08U                               0.0
cityA08                                 0
cityA08U                              0.0
city

这是因为在您的单个示例中您给出了一个数字,但在循环中您使用了一个字符:

#using a loop
vin = c("19UUA86209A000532", "19UUA86239A021598", "19UUA8F20CA037748", "19UUA8F21CA008002", "19UUA8F21CA017878")
for (i in vin) {
  library(mpg)
  print(i)
  print(substr(i, 13, 17))
  q = substr(i, 13, 17)
  z = feVehicle(as.numeric(q))
  Sys.sleep(5)
  z = t(unlist(z))

}
[1] "19UUA86209A000532"
[1] "00532"
[1] "19UUA86239A021598"
[1] "21598"
[1] "19UUA8F20CA037748"
[1] "37748"
[1] "19UUA8F21CA008002"
[1] "08002"
[1] "19UUA8F21CA017878"
[1] "17878"