A phone vector stores phone numbers parsed with libphonenumber for formatting and further processing.

phone(
  x = character(),
  region = character(),
  show_progress = getOption("dialr.show_progress")
)

phone_reparse(x)

is.phone(x)

# S3 method for phone
print(x, n = 10, ...)

# S3 method for phone
format(
  x,
  format = c("E164", "NATIONAL", "INTERNATIONAL", "RFC3966"),
  home = NULL,
  clean = TRUE,
  strict = FALSE,
  ...
)

# S3 method for phone
as.character(x, raw = TRUE, ...)

Arguments

x

A character vector of phone numbers.

region

A character vector of ISO country codes with the default region for each phone number in x. A region vector of length 1 will be recycled to the length of x.

If NA or "", numbers written in international format (with a leading +) will be parsed without a default region.

show_progress

Should a progress bar be displayed? Defaults to the option dialr.show_progress.

n

Number of elements to print.

...

Additional arguments for specific methods.

format

Phone number format to use from one of four standards:

See notes from the libphonenumber javadocs for more details.

format defaults to "E164". The default can be set in option dialr.format.

home

ISO country code for home region. If provided, numbers will be formatted for dialing from the home region.

clean

Should non-numeric characters be removed? If TRUE, keeps numbers and leading "+".

strict

Should invalid phone numbers be removed? If TRUE, invalid phone numbers are replaced with NA.

raw

If TRUE, the raw phone number is returned. Otherwise elements are cleaned with format().

Details

libphonenumber defines the PhoneNumberUtil class, with a set of functions for extracting information from and performing processing on a parsed Phonenumber object. A text phone number must be parsed before any other operations (e.g. checking phone number validity, formatting) can be performed. When parsing a phone number a "default region" is required to determine the processing context for non-international numbers.

A phone vector stores the raw phone number, the default region and a java Phonenumber object for each element. The java object is cached so should persist between R sessions. In case of issues, use phone_reparse() to recreate the phone vector from the original phone number and region.

Phone number parsing functions display a progress bar in interactive sessions by default. This can be disabled globally by setting option dialr.show_progress to FALSE, or locally using the show_progress function argument.

libphonenumber reference

phone(): Phone numbers are parsed using PhoneNumberUtil.parseAndKeepRawInput(). A phone vector stores the returned Phonenumber.PhoneNumber object alongside the original raw text and default region for later reference.

format(): PhoneNumberUtil.format() by default, or PhoneNumberUtil.formatOutOfCountryCallingNumber() if home is provided.

See also

Examples

  # Create a phone vector
  x <- phone(c(0, 0123, "0412 345 678", "61412987654", "03 9123 4567", "+12015550123"), "AU")
  
  is.phone(x)
#> [1] TRUE
  print(x)
#> # Parsed phone numbers: 6 total, 5 successfully parsed
#> [1] 0            123          0412 345 678 61412987654  03 9123 4567
#> [6] +12015550123
  as.character(x)
#> [1] "0"            "123"          "0412 345 678" "61412987654"  "03 9123 4567"
#> [6] "+12015550123"
  format(x)
#> [1] NA             "+61123"       "+61412345678" "+61412987654" "+61391234567"
#> [6] "+12015550123"
  format(x, home = "AU")
#> [1] NA                "123"             "0412345678"      "0412987654"     
#> [5] "0391234567"      "001112015550123"
  
  # Parse international number with no default region
  phone("+61412345678", NA)
#> # Parsed phone numbers: 1 total, 1 successfully parsed
#> [1] +61412345678
  
  # Will fail to parse if number is not in international format
  phone("0412345678", NA)
#> # Parsed phone numbers: 1 total, 0 successfully parsed
#> [1] 0412345678
  
  # A combination can be used
  phone(c("+61412345678", "0412345678"), c(NA, "AU"))
#> # Parsed phone numbers: 2 total, 2 successfully parsed
#> [1] +61412345678 0412345678