Modify components of a URL. The default value of each argument, NULL
,
means leave the component as is. If you want to remove a component,
set it to ""
. Note that setting scheme
or hostname
to ""
will
create a relative URL.
Usage
url_modify(
url,
scheme = NULL,
hostname = NULL,
username = NULL,
password = NULL,
port = NULL,
path = NULL,
query = NULL,
fragment = NULL
)
Arguments
- url
A string or parsed URL.
- scheme
The scheme, typically either
http
orhttps
.- hostname
The hostname, e.g.,
www.google.com
orposit.co
.- username, password
Username and password to embed in the URL. Not generally recommended but needed for some legacy applications.
- port
An integer port number.
- path
The path, e.g.,
/search
. Paths must start with/
, so this will be automatically added if omitted.- query
Either a query string or a named list of query components.
- fragment
The fragment, e.g.,
#section-1
.
Examples
url_modify("http://hadley.nz", path = "about")
#> [1] "http://hadley.nz/about"
url_modify("http://hadley.nz", scheme = "https")
#> [1] "https://hadley.nz/"
url_modify("http://hadley.nz/abc", path = "/cde")
#> [1] "http://hadley.nz/cde"
url_modify("http://hadley.nz/abc", path = "")
#> [1] "http://hadley.nz/"
url_modify("http://hadley.nz?a=1", query = "b=2")
#> [1] "http://hadley.nz/?b=2"
url_modify("http://hadley.nz?a=1", query = list(c = 3))
#> [1] "http://hadley.nz/?c=3"