How can I determine the usage type of an IP address in R?

To determine the usage type of an IP address using IP2Location.io in R, you can use the IP2Location.io API to retrieve geolocation and related data. The usage type field specifies how the IP is being used. For usage types supported, please see API documentation for more info.

  1. We'll assume that you have already installed R & RStudio and we won't cover that here. You will also need a paid subscription to the Starter plan or higher. Subscribe now!
  2. In the R Console, run the command below to install the dependencies if you don't have them installed.
    R
    
    install.packages(c("httr", "jsonlite"))
    			
  3. Save the below code into a file called test.R on your computer.
    R
    
    library(httr)
    library(jsonlite)
    
    ip = "8.8.8.8"
    key = "YOUR_API_KEY"
    res <- GET("https://api.ip2location.io/",
               query = list(format = "json", ip = ip, key = key))
    
    if (res$status_code == 200) {
      raw <- rawToChar(res$content)
      data <- fromJSON(raw)
      if (!is.null(data$usage_type)) {
        print(paste0("The usage type for IP ", ip, " is ", data$usage_type, "."))
      } else{
        print(
          "ERROR: The usage_type field requires a paid subscription to the Starter plan or higher."
        )
      }
    } else if ((res$status_code == 400) || (res$status_code == 401)) {
      raw <- rawToChar(res$content)
      data <- fromJSON(raw)
      if (!is.null(data$error)) {
        print(paste0("ERROR: ", data$error$error_message))
      } else {
        print(data)
      }
    } else {
      print(paste0("ERROR: ", res$status_code))
    }
    			
  4. In the RStudio, open the test.R, select all of the codes and run them.

This script will output the usage type of specified IP address. Make sure to replace 8.8.8.8 with the IP address you want and replace YOUR_API_KEY to your own API key.

Other Languages

Unlock Location Insights For Free

Empower your applications with accurate IP geolocation information now.

Try It for Free