ropensci / neotoma

Programmatic R interface to the Neotoma Paleoecological Database.
https://docs.ropensci.org/neotoma
Other
30 stars 16 forks source link

get_geochron #201

Closed kcmaguire closed 8 years ago

kcmaguire commented 9 years ago

I get the following error when using the get_geochron function:

> get_geochron(8444)
API call was successful.
[[1]]
Error in x[[1]]$site : $ operator is invalid for atomic vectors

Here is a second example:

> get_geochron(9282)
API call was successful.
[[1]]
Error in x[[1]]$site : $ operator is invalid for atomic vectors

To overcome this, I modified the function as so:

get_geochron.Mod = function (x, verbose = TRUE) 
{
  if (!is.numeric(x)) {
    stop("datasetid must be numeric.")
  }
  get_sample <- function(x) {
    base.uri <- "http://api.neotomadb.org/v1/apps/geochronologies/"
    aa <- try(fromJSON(paste0(base.uri, "?datasetid=", x), 
                       nullValue = NA))
    if (inherits(aa, "try-error")) 
      return(aa)
    if (isTRUE(all.equal(aa[[1]], 0))) {
      stop(paste("Server returned an error message:\n", 
                 aa[[2]]), call. = FALSE)
    }
    if (isTRUE(all.equal(aa[[1]], 1) & length(aa[[2]]) == 
                 0)) {
      stop("No geochronological record is associated with this sample", 
           call. = FALSE)
    }
    if (isTRUE(all.equal(aa[[1]], 1) & length(aa[[2]]) > 
                 0)) {
      aa <- aa[[2]]
      if (verbose) {
        message(strwrap(paste0("API call was successful.")))
      }
      pull.rec <- function(x) {
        data.frame(sample.id = x$SampleID, depth = x$Depth, 
                   thickness = x$Thickness, age.type = x$AgeType, 
                   age = x$Age, e.older = x$ErrorOlder, e.young = x$ErrorYounger, 
                   delta13C = x$Delta13C, lab.no = x$LabNumber, 
                   material.dated = x$MaterialDated, geo.chron.type = x$GeochronType, 
                   notes = x$Notes, infinite = x$Infinite, stringsAsFactors = FALSE)
      }
      out <- list(x, do.call(rbind.data.frame, lapply(aa[[1]], # Modification made
                                                      pull.rec)))
      #class(out) <- c("geochronologic", "list") # Modification made
    }
    out
  }
  lapply(x, function(x) try(get_sample(x)))
}
SimonGoring commented 8 years ago

This problem seems to have been a problem with the print method (in part) rather than the get_geochron. That's why the fix (commenting out the class call) worked to resolve the problem (in part), because it over-rode the print command.

The API for geochron does not actually provide the dataset information which is requested when we use the print.geochronologic method.

To fix this I've made some changes to get_geochron in commit https://github.com/ropensci/neotoma/commit/db4e983b176df354a8251e8e477557c0f54e09e7. This should resolve the problems.