尺寸错误(尺寸错误[[:尺寸错误])

时间:2018-07-29 14:58:22

标签: r shiny leaflet

我正在开发一个应用程序,该应用程序可以从API获取一些数据并在地图上进行渲染。该应用程序将纬度和经度坐标作为点放置在地图上。该应用程序每5秒钟从API获取新数据并更新地图上的坐标。在某个时间点(任意)运行该应用程序时,出现错误:

  Warning: Error in [: incorrect number of dimensions
  [No stack trace available]

我花了一些时间调试它,但是我无法管理它。我看了很多视频,读了很多有关Shiny和Leaflet的文档,但仍然无济于事。如果有人可以指出我在代码中的错误之处,我会感到很高兴。

我从API提取数据:

 str(trams_data)
'data.frame':   187 obs. of  8 variables:
 $ Status   : chr  "RUNNING" "RUNNING" "RUNNING" "RUNNING" ...
 $ FirstLine: chr  "27 " "28 " "15 " "15 " ...
 $ Lon      : num  20.9 21 21 20.9 21 ...
 $ Lines    : chr  "27             " "28             " "15             " "15             " ...
 $ Time     : chr  "2018-07-29T16:05:43" "2018-07-29T16:05:43" "2018-07-29T16:05:41" "2018-07-29T16:05:45" ...
 $ Lat      : num  52.2 52.3 52.2 52.2 52.3 ...
 $ LowFloor : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
 $ Brigade  : chr  "3   " "6   " "2   " "8   " ...

这是我的代码:

library("httr")
library("jsonlite")
library("shiny")
library("leaflet")

ui <- fluidPage(
  navbarPage("TEST APP",
             tabPanel("MAP",
  leafletOutput("mymap", width = "auto", height = "560px"),
  p()))
  #actionButton("refresh", "Refresh")
)

server <- function(input, output, session) {

  base <- "xxx"
  id <- "xxx"
  apikey <- "xxx"

  call1 <- paste(base,"?","id=",id,"&","apikey=",apikey, sep="")

  autoInvalidate <- reactiveTimer(5000)

  reData <- eventReactive(autoInvalidate(), {

      get_trams <- GET(call1)
      get_trams_text <- content(get_trams, "text")
      get_trams_json <- fromJSON(get_trams_text, flatten = TRUE)
      trams_data <- get_trams_json$result

      return(trams_data)
      }, ignoreNULL = FALSE)

  points <- eventReactive(autoInvalidate(), {
    cbind(reData()$Lon, reData()$Lat)
  }, ignoreNULL = FALSE)

  labels <- eventReactive(autoInvalidate(), {
    reData()$FirstLine
  },ignoreNULL = FALSE)

  output$mymap <- renderLeaflet({
    leaflet() %>%
      addTiles()
  })

  observeEvent(autoInvalidate(), {
    leafletProxy("mymap") %>%
      clearMarkers() %>%
      addMarkers(data = points(), label = labels())
  },ignoreNULL = FALSE)
}

shinyApp(ui, server)

更新:

我在要点和标签中添加了print(reData())并标记了eventReactive函数,这就是我得到的:

[1] 21.00236 20.92381 21.04991 21.00834 20.92970 20.97194 20.98439 20.98094 20.98558 21.11893
 [11] 21.05004 20.90097 21.04956 20.98154 21.00422 21.04862 21.11299 21.03048 20.91009 21.00150
 [21] 21.08492 20.98849 21.01590 21.03400 21.00282 20.92914 20.98119 20.92794 21.08572 21.04540
 [31] 21.03368 20.96239 21.02378 21.11908 21.08418 20.97873 21.01179 20.98203 21.08369 21.02177
 [41] 21.02183 21.03386 20.91772 20.95533 21.03402 21.00361 20.92503 21.02952 20.98528 20.98369
 [51] 21.01313 20.97889 20.94261 20.94379 20.96459 20.92710 21.06943 20.98204 20.99330 20.98516
 [61] 20.96897 20.93020 20.90097 21.00500 20.97187 20.94782 21.03708 20.98205 20.98543 21.03536
 [71] 20.94371 20.98172 21.01701 20.98339 20.95808 20.95806 21.06327 21.00252 20.96055 21.01672
 [81] 21.01521 20.94463 20.95484 20.99154 20.96325 20.92914 20.94370 21.09014 20.92505 21.00013
 [91] 21.06433 20.98833 21.01230 21.11877 20.97675 20.98781 21.03094 21.00284 20.93012 21.02629
[101] 21.01077 20.98874 20.97987 21.00488 20.98178 21.02320 21.06315 21.00485 20.92425 20.98544
[111] 20.97876 21.02219 20.99133 20.98091 20.92475 20.98205 20.94226 21.00127 21.00874 20.91714
[121] 21.00126 21.00075 20.90055 21.02245 20.94096 20.93538 21.04203 21.01680 21.01603 20.96511
[131] 21.01723 20.94171 20.97463 20.99874 20.92974 20.96233 20.97762 20.99828 21.08459 21.05105
[141] 20.99000 21.06103 20.99899 21.09449 20.99381 21.00237 20.96146 21.00208 21.02289 21.03244
[151] 20.95446 20.90474 20.89984 20.93108 20.90099 21.03473 20.97787 20.99308 21.10694 20.93102
[161] 20.94328 20.93228 20.94475 20.93131 20.99394 21.05970 20.92985 21.00460 20.94332 20.92484
[171] 21.03078 20.91706 20.92492 21.04726 20.94388
  [1] 52.25875 52.26031 52.24744 52.23558 52.29198 52.27273 52.25514 52.21608 52.26979 52.23814
 [11] 52.25276 52.23863 52.23772 52.23426 52.24562 52.24757 52.24022 52.23376 52.23888 52.22765
 [21] 52.24457 52.22554 52.16806 52.25441 52.20702 52.28993 52.25301 52.26373 52.24520 52.25089
 [31] 52.25591 52.19505 52.29787 52.23812 52.24533 52.21046 52.22988 52.23349 52.24527 52.29846
 [41] 52.29844 52.25566 52.22077 52.24733 52.25542 52.18848 52.26034 52.25253 52.23915 52.25330
 [51] 52.26186 52.23650 52.32425 52.17610 52.19743 52.26312 52.24621 52.20978 52.24220 52.25552
 [61] 52.20235 52.29201 52.23867 52.21758 52.27279 52.24708 52.26848 52.20983 52.25123 52.25420
 [71] 52.27098 52.21690 52.16821 52.21880 52.24744 52.31645 52.24656 52.22790 52.19292 52.23098
 [81] 52.21996 52.17643 52.18645 52.24428 52.26663 52.28991 52.17584 52.24496 52.26046 52.22016
 [91] 52.24078 52.22432 52.22998 52.23821 52.21105 52.22393 52.28786 52.22628 52.29200 52.28011
[101] 52.22010 52.22522 52.23723 52.22078 52.25648 52.29865 52.24040 52.22213 52.28131 52.22923
[111] 52.21050 52.29856 52.24453 52.27531 52.26022 52.20990 52.27260 52.24445 52.23486 52.22279
[121] 52.17868 52.17873 52.23867 52.18055 52.32728 52.29926 52.24979 52.18898 52.16800 52.26617
[131] 52.22115 52.33263 52.23539 52.24948 52.29218 52.31183 52.21216 52.25568 52.24472 52.23804
[141] 52.22517 52.23999 52.24885 52.24411 52.24149 52.29036 52.23265 52.29070 52.26115 52.25370
[151] 52.28577 52.23905 52.26342 52.22484 52.23879 52.25475 52.24256 52.24167 52.24146 52.22478
[161] 52.24688 52.24698 52.22689 52.22474 52.24195 52.25818 52.29210 52.25928 52.29745 52.26018
[171] 52.28809 52.25050 52.26015 52.25184 52.17593
  [1] "6  " "28 " "6  " "15 " "6  " "27 " "28 " "15 " "15 " "6  " "28 " "70 " "25 " "24 " "13 "
 [16] "6  " "9  " "22 " "26 " "7  " "9  " "11 " "4  " "23 " "33 " "33 " "1  " "11 " "3  " "7  "
 [31] "3  " "7  " "3  " "3  " "3  " "1  " "22 " "22 " "22 " "25 " "3  " "3  " "26 " "23 " "23 "
 [46] "17 " "23 " "23 " "26 " "33 " "1  " "11 " "17 " "9  " "15 " "24 " "26 " "1  " "33 " "1  "
 [61] "9  " "33 " "26 " "33 " "17 " "23 " "1  " "25 " "17 " "26 " "22 " "7  " "4  " "25 " "24 "
 [76] "17 " "26 " "7  " "9  " "9  " "70 " "9  " "7  " "33 " "22 " "22 " "9  " "9  " "35 " "35 "
 [91] "24 " "9  " "9  " "24 " "9  " "9  " "4  " "17 " "11 " "25 " "4  " "1  " "1  " "17 " "24 "
[106] "1  " "9  " "33 " "33 " "1  " "1  " "1  " "17 " "15 " "35 " "35 " "35 " "35 " "15 " "70 "
[121] "31 " "31 " "70 " "31 " "2  " "2  " "28 " "31 " "31 " "35 " "15 " "2  " "26 " "35 " "2  "
[136] "2  " "15 " "70 " "26 " "9  " "24 " "24 " "70 " "9  " "70 " "70 " "70 " "70 " "20 " "13 "
[151] "6  " "28 " "20 " "27 " "28 " "20 " "27 " "20 " "6  " "13 " "20 " "20 " "13 " "27 " "20 "
[166] "13 " "6  " "6  " "6  " "23 " "3  " "24 " "24 " "7  " "7  "
Warning: Error in [: incorrect number of dimensions
  [No stack trace available]

此外我还执行了dput(head(trams_data)),结果如下:

structure(list(Status = c("RUNNING", "RUNNING", "RUNNING", "RUNNING", 
"RUNNING", "RUNNING"), FirstLine = c("33 ", "35 ", "24 ", "9  ", 
"17 ", "23 "), Lon = c(20.9289761, 20.9779778, 21.0421829, 20.9471664, 
20.9794827, 21.001812), Lines = c("33             ", "35             ", 
"24             ", "9              ", "17             ", "23             "
), Time = c("2018-07-29T18:54:14", "2018-07-29T18:54:12", "2018-07-29T18:54:14", 
"2018-07-29T18:54:15", "2018-07-29T18:54:13", "2018-07-29T18:54:11"
), Lat = c(52.2801132, 52.21064, 52.2361641, 52.1791573, 52.2628021, 
52.2445641), LowFloor = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE), 
    Brigade = c("13  ", "10  ", "1   ", "11  ", "10  ", "3   "
    )), row.names = c(NA, 6L), class = "data.frame")

我现在看到的是,尺寸与长度od 175相同。

有趣的是,如果我从leafletProxy中删除标签,那么一切正常,没有任何错误,但是我不知道为什么会这样。

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案。该API每隔几分钟返回一个空响应。打印反应性变量的内容不是解决方案,因为打印是在发生错误之后进行的。