Skip to contents

This is a vectorized predicate to test if character values are valid URLs, i.e. have a scheme and at least one of: hostname or path (according to RFC3986).

Usage

is_uri(x, empty_ok = TRUE)

Arguments

x

A character vector.

empty_ok

Whether to allow empty hosts and paths, as a boolean (default: TRUE). RFC3986 allows for empty paths & hosts, potentially making a scheme alone valid and, therefore, the default. However, it is often desirable to validate that at least one of these is NOT empty, since a scheme alone is rarely useful in practice.

Value

A logical vector indicating which values of x are valid URIs.

Notes

While all URIs are valid CURIEs (see is_curie(def = "w3c")), not all CURIEs are valid URIs (e.g. URIs cannot start with _).

Examples

.uri <- c(
    # always TRUE
    "http://purl.obolibrary.org/obo/DOID_0001816",
    "https://google.com",
    "mailto:fake.name@blah.com",
    # TRUE, if empty_ok = FALSE
    "file://",
    "mailto:",
    # never TRUE
    "blah",
    ""
)

is_uri(.uri)
#> [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
is_uri(.uri, empty_ok = FALSE)
#> [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE