Learn R Programming

urlparse (version 0.2.1)

url_modify: Modifies a URL string by updating its components.

Description

This function modifies a URL string by updating its components such as scheme, user, password, host, port, query, raw query, and fragment. If any of these components are not provided (i.e., NULL), the existing components of the URL are retained.

Usage

url_modify(
  url,
  scheme = NULL,
  user = NULL,
  password = NULL,
  host = NULL,
  port = NULL,
  path = NULL,
  query = NULL,
  fragment = NULL
)

set_scheme(url, scheme)

set_user(url, user)

set_password(url, password)

set_host(url, host)

set_port(url, port)

set_path(url, path)

set_query(url, query)

set_fragment(url, fragment)

Value

A character string representing the modified URL.

Arguments

url

A character string representing the original URL.

scheme

A character string for the new scheme (e.g., "http" or "https") or NULL to keep it unchanged.

user

A character string for the username or NULL to keep it unchanged.

password

A character string for the new password or NULL to keep it unchanged.

host

A character string for the new host or NULL to keep it unchanged.

port

A character string for the new port or NULL to keep it unchanged.

path

A character string for the new path or NULL to keep it unchanged.

query

A list or character of new query parameters or NULL to keep it unchanged.

fragment

A character string for the new fragment or NULL to keep it unchanged.

Examples

Run this code
library(urlparse)

# Example 1: Modify the scheme and host of a URL
url_modify(
  "https://user:pass@host.com/path?query#fragment",
  scheme = "http",
  host = "example.com"
)

# Example 2: Add a query parameter to a URL
url_modify(
  "https://host.com/path", query = list(key1 = "value1", key2 = "value2")
)

# Example 3: Change the fragment of a URL
url_modify("https://host.com/path#old_fragment", fragment = "new_fragment")

Run the code above in your browser using DataLab