[R] Subscript Error

Peter Ehlers ehlers at ucalgary.ca
Thu Apr 5 04:11:34 CEST 2012


On 2012-04-04 14:25, z2.0 wrote:
>
>
> json_dir is a list of JSON lists mapping lat/long route points between
> locations using CloudMade's API.
> post_url is the URL of the HTTP request
>
>      for (n in json_dir) {
>          i = i + 1
>              if (typeof(json_dir[[i]]) != "NULL") {
>                  if (i == 1) {
>                      dat_add<- ldply(json_dir[[i]], function(x)
> t(data.frame(x)), .progress = "text")
>                      names(dat_add)<- c("lat", "lon")
>                      json_path<- list(dat_add)
>                  } else {
>                      dat_add<- ldply(json_dir[[i]], function(x)
> t(data.frame(x)), .progress = "text")
>                      names(dat_add)<- c("lat", "lon")
>                      json_path<- c(json_path, list(dat_add))
>                  }
>
>                  p = p + geom_path(aes(lon, lat), data = json_path[[i]])
>          }
>          print(paste("Processed ", i, " of ", as.character(length(json_dir)),
> " in route set.", sep = ""))
>      }
>
> This runs until i = 101 and then errors out with,
> "Error in json_path[[i]] : subscript out of bounds"
>
> typeof(json_dir[[101]]) = "list", so it's not that the first if-block is
> somehow resetting json_path in an errant fashion.
>
> Do lists have a default, built-in limit on no. of elements? Each element I'm
> passing contains hundreds or thousands of lat/long pairs, so it's also
> possible I'm hitting some upper bound on per-object memory, if that exists,
> but Googling around leads me to think that's not the case.
>

I'm guessing that your problem is with the for() statement; you
probably want to replace json_dir by a sequence, e.g.

   for(n in seq_along(json_dir)) {....

Example:

   L <- list(1,2,3,14)
   for( i in L) cat( i, L[[i]], "\n" )
   for( i in seq_along(L)) cat( i, L[[i]], "\n" )

> I think I've fucked something up in my logic, but I'm not sure what.

Hmm, this list is generally more polite.

Peter Ehlers

>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Subscript-Error-tp4533219p4533219.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list