/
as.location.R
46 lines (40 loc) · 1.09 KB
/
as.location.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#' Convert a path or URL to a location object.
#'
#' @export
#'
#' @param x Input.
#' @param ... Ignored.
#' @examples \dontrun{
#' # A file
#' file <- system.file("examples", "zillow_or.geojson", package = "geojsonio")
#' as.location(file)
#'
#' # A URL
#' url <- "https://raw.githubusercontent.com/glynnbird/usstatesgeojson/master/california.geojson"
#' as.location(url)
#' }
as.location <- function(x, ...) UseMethod("as.location")
#' @export
as.location.character <- function(x, ...) check_location(x, ...)
#' @export
as.location.location_ <- function(x, ...) x
check_location <- function(x, ...) {
if (is.url(x)) {
as_location(x, "url")
} else {
if (!file.exists(x)) stop("File does not exist. Create it, or fix the path.", call. = FALSE)
as_location(path.expand(x), "file")
}
}
as_location <- function(x, type) {
structure(x, class = "location_", type = type)
}
#' @export
print.location_ <- function(x, ...) {
cat("<location>", "\n")
cat(" Type: ", attr(x, "type"), "\n")
cat(" Location: ", x[[1]], "\n")
}
is.url <- function(x, ...) {
grepl("^(ftp|http)s?://", x)
}