How to use IP2Location in R Language

This tutorial demonstrates how to use the IP2Location library in the R programming language. We used RStudio, an open source IDE platform, for this tutorial explanation. For our geolocation library, we used the IP2Location Python Library as R has a ready package, reticulate, to interact with the python code.

To begin with, you will need to install the IP2Location python module and the reticulate package.

Instructions are as below:

Install IP2Location module:

  1. Install Python in your operating system (Windows, Linux, MacOS). We recommend you download Python 3.
  2. In the terminal, run the below command
    pip install IP2Location

Install reticulate package:

  1. Launch RStudio IDE.
  2. In the console window, run the below command
    install.packages("reticulate")

Use IP2Location Library in R

  1. Launch RStudio IDE after you have installed the reticulate package.
  2. In the console window, run the following command to load the package.
    library(reticulate)
  3. Run the code to import the python library. You need to use the py_run_string() function to execute the python code.
    py_run_string("import IP2Location")
  4. You need to instantiate the IP2Location python object by using the below syntax.
    py_run_string("IP2LocObj = IP2Location.IP2Location()")
  5. The next step is to load the IP2Location BIN file, by specifying the absolute path inside the param. You can download a free BIN database from https://lite.ip2location.com.
    py_run_string("IP2LocObj.open('IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY.SAMPLE.BIN')")
  6. Call the get_all() function to get all the information about the IP address. After that, you can do a print out of each fields inside the result object.
    py_run_string("rec = IP2LocObj.get_all('8.8.8.8')")
    py_run_string("print (rec.country_short)")
    py_run_string("print(rec.country_long)")
    py_run_string("print(rec.region)")
    py_run_string("print(rec.city)")
    py_run_string("print(rec.isp)")
    py_run_string("print(rec.latitude)")
    py_run_string("print(rec.longitude)")
    py_run_string("print(rec.domain)")
    py_run_string("print(rec.zipcode)")
    py_run_string("print(rec.timezone)")
    py_run_string("print(rec.netspeed)")
    py_run_string("print(rec.idd_code)")
    py_run_string("print(rec.area_code)")
    py_run_string("print(rec.weather_code)")
    py_run_string("print(rec.weather_name)")
    py_run_string("print(rec.mcc)")
    py_run_string("print(rec.mnc)")
    py_run_string("print(rec.mobile_brand)")
    py_run_string("print(rec.elevation)")
    py_run_string("print(rec.usage_type)") 
    py_run_string("print(rec.address_type)")
    py_run_string("print(rec.category)")

Output:

IP address lookup output

The complete code snippet can be found below.

py_run_string("import IP2Location")
py_run_string("IP2LocObj = IP2Location.IP2Location()")
py_run_string("IP2LocObj.open('IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY-SAMPLE.BIN')")
py_run_string("rec = IP2LocObj.get_all('8.8.8.8')")
py_run_string("print (rec.country_short)")
py_run_string("print(rec.country_long)")
py_run_string("print(rec.region)")
py_run_string("print(rec.city)")
py_run_string("print(rec.isp)")
py_run_string("print(rec.latitude)")
py_run_string("print(rec.longitude)")
py_run_string("print(rec.domain)")
py_run_string("print(rec.zipcode)")
py_run_string("print(rec.timezone)")
py_run_string("print(rec.netspeed)")
py_run_string("print(rec.idd_code)")
py_run_string("print(rec.area_code)")
py_run_string("print(rec.weather_code)")
py_run_string("print(rec.weather_name)")
py_run_string("print(rec.mcc)")
py_run_string("print(rec.mnc)")
py_run_string("print(rec.mobile_brand)")
py_run_string("print(rec.elevation)")
py_run_string("print(rec.usage_type)")
py_run_string("print(rec.address_type)")
py_run_string("print(rec.category)")

Was this article helpful?

Related Articles