迭代数组中的嵌套对象(R,leaflet,json)

时间:2017-12-10 16:21:11

标签: json r leaflet

我试图在R中的数组中调用对象的纬度和经度,并与传单一起使用。

dMVStations <- fromJSON(file = "stations.json")
# "stations.json" is an array of 88 objects with 36 attributes each.
# two of those attributes are latitude and longitude.

...
# setting up the leaflet map
...

leafletProxy("map", data = dMVStations) %>%
  addCircles(
    lng =~ dMVStations$[[1]...[88]]$longitude,
    lat =~ dMVStations$[[1]...[88]]$latitude,
    ...
  )

如何指向dMVStations中每个对象的纬度和经度?

str(dMVStations)返回:

name: dMVStations
type: list[88]
value: 'List of length 88'

这88个对象中的每一个都是

named by the index [[1]...[88]]
has type: list[36]
has value: 'List of length 36'

谢谢和欢呼,

1 个答案:

答案 0 :(得分:0)

str可以看出,dMVStations包含88个条目。每个条目都是一个包含36个值的列表(向量),正如您描述的JSON文件&#34; 88个对象,每个对象有36个属性&#34;。

假设纬度和经度分别是从JSON文件读取的属性列表中的属性5和9。要访问位置 i ,您可以使用以下语法:

lat <- dMVStations[[i]][5]
lon <- dMVStations[[i]][9]

如果列已命名,您应该能够使用以下内容访问它们:

lat <- dMVStations$latitude[i]
lon <- dMVStations$longitude[i]
相关问题