R/import_functions.R

Defines functions odb_import_taxons odb_import_persons odb_import_measurements odb_import_locations odb_import_individuals odb_get_affected_ids odb_get_log odb_get_jobs

Documented in odb_get_affected_ids odb_get_jobs odb_get_log odb_import_individuals odb_import_locations odb_import_measurements odb_import_persons odb_import_taxons

#' Import functions
#'
#' These are some basic functions to interact with the API to import data and monitor the submitted jobs. See the full help on the package vignettes
#' @examples
#' \dontrun{
#' data = data.frame(
#'   name = c("Euterpe oleracea", "Euterpe edulis"),
#'   parent = "Euterpe",
#'   level=odb_taxonLevelCodes('species'),
#' )
#' odb_import_taxons(data, cfg)
#' }
#' @export
#' @param odb_cfg list. A configuration object, as generated by \code{\link{odb_config}}
#' @param data The data to be imported. The format is quite flexible, but normally a named \code{data.frame} is the best choice. See package vignettes for details
#' @param common Optional. A named list containing fields which are common to all imported data.
#' @param params named list, named vector or character string. Represents the parameters to be sent for the server, such as "valid=1" to return only valid taxons. See \code{\link{odb_params}}
#' @param job_id numeric. A single job id
#' @param simplify logical. Should the results be 'unlisted' before being returned?
#' @return odb_import methods usually return the submitted job id retrieved with \code{\link{odb_config}}
#' @import jsonlite
#' @rdname import_functions
odb_get_jobs <- function(params = list(), odb_cfg = odb_config(), simplify = TRUE) {
 response = odb_send_get(params, odb_cfg, "jobs")
 format_get_response(response, simplify)
}

#' @export
#' @rdname import_functions
odb_get_log <- function(job_id, odb_cfg = odb_config()) {
 if (!is.numeric(job_id) | length(job_id) > 1) stop ("job_id must be a single number")
 job = odb_get_jobs(list(id = job_id, fields = "log"), odb_cfg)
 jsonlite::fromJSON(job$log[[1]])
}

#' @export
#' @rdname import_functions
odb_get_affected_ids <- function(job_id, odb_cfg = odb_config()) {
 if (!is.numeric(job_id) | length(job_id) > 1) stop ("job_id must be a single number")
 job = odb_get_jobs(list(id = job_id, fields = "affected_ids"), odb_cfg)
 jsonlite::fromJSON(job$affected_ids[[1]])
}


#' @export
#' @rdname import_functions
odb_import_individuals <- function(data, odb_cfg = odb_config(), common = list()) {
 if (class(data) != "data.frame")
  stop ("Currently odb_import_individuals only accepts data.frame, please convert your data")
 cat("Sending ODB request (filesize = " , utils::object.size(data), ")\n", sep="")
 response = odb_send_post(data, odb_cfg, "individuals", common)
 id = fromJSON(toJSON(content(response)))$userjob
 # Wait a second so that the job may start processing
 Sys.sleep(1)
 return (odb_get_jobs(list(id = id), odb_cfg))
}


#' @export
#' @rdname import_functions
odb_import_locations <- function(data, odb_cfg = odb_config(), common = list()) {
 if (class(data) != "data.frame")
  stop ("Currently odb_import_taxons only accepts data.frame, please convert your data")
 cat("Sending ODB request (filesize = " , utils::object.size(data), ")\n", sep="")
 response = odb_send_post(data, odb_cfg, "locations", common)
 id = fromJSON(toJSON(content(response)))$userjob
 # Wait a second so that the job may start processing
 Sys.sleep(1)
 return (odb_get_jobs(list(id = id), odb_cfg))
}

#' @export
#' @rdname import_functions
odb_import_measurements <- function(data, odb_cfg = odb_config(), common = list()) {
 response = odb_send_post(data, odb_cfg, "measurements", common)
 id = fromJSON(toJSON(content(response)))$userjob
 # Wait a second so that the job may start processing
 Sys.sleep(1)
 return (odb_get_jobs(list(id = id), odb_cfg))
}


#' @export
#' @rdname import_functions
odb_import_persons <- function(data, odb_cfg = odb_config(), common = list()) {
 if (class(data) != "data.frame")
  stop ("Currently odb_import_people only accepts data.frame, please convert your data")
 cat("Sending ODB request (filesize = " , utils::object.size(data), ")\n", sep="")
 response = odb_send_post(data, odb_cfg, "persons", common)
 id = fromJSON(toJSON(content(response)))$userjob
 # Wait a second so that the job may start processing
 Sys.sleep(1)
 return (odb_get_jobs(list(id = id), odb_cfg))
}



#' @export
#' @rdname import_functions
odb_import_taxons <- function(data, odb_cfg = odb_config(), common = list()) {
 response = odb_send_post(data, odb_cfg, "taxons", common)
 id = fromJSON(toJSON(content(response)))$userjob
 # Wait a second so that the job may start processing
 Sys.sleep(1)
 return (odb_get_jobs(list(id = id), odb_cfg))
}

#' @export
#' @rdname import_functions
odb_import_traits <- function (data, odb_cfg = odb_config(), common = list())
{
 response =  odb_send_post(data, odb_cfg, "traits", common)
 id = fromJSON(toJSON(content(response)))$userjob
 Sys.sleep(1)
 return(odb_get_jobs(list(id = id), odb_cfg))
}

#' @export
#' @rdname import_functions
odb_import_vouchers <- function (data, odb_cfg = odb_config(), common = list())
{
 if (class(data) != "data.frame")
  stop("Currently odb_import_vouchers only accepts data.frame, please convert your data")
 cat("Sending ODB request (filesize = ", utils::object.size(data), ")\n", sep = "")
 response = odb_send_post(data, odb_cfg, "vouchers", common)
 id = fromJSON(toJSON(content(response)))$userjob
 Sys.sleep(1)
 return(odb_get_jobs(list(id = id), odb_cfg))
}
opendatabio/opendatabio-r documentation built on Sept. 27, 2021, 1:32 a.m.