2. Introduction
• 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.
3. Installation
• Install IP2Location module:
• Install python in your OS(Python 3 recommended)
• Run the below command in the terminal
• Install reticulate package:
• In console window, run the below command
pip install IP2Location
install.packages("reticulate")
4. Use IP2Location Library in R
1. Launch RStudio IDE after installed the reticulate package
2. Run the below command to load the package in the console window
3. Next, need to use a function to execute the python code
4. Instantiate the IP2Location python object by using the below syntax
Library(reticulate)
py_run_string("import IP2Location")
py_run_string("IP2LocObj = IP2Location.IP2Location()")
5. Use IP2Location Library in R
5. Load the IP2Location BIN file by specifying the absolute path inside
the param.You also 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-SAMPLE.BIN')")
6. Use IP2Location Library in R
6. Last, call the get_all() function to get all the information about the IP
address.You can also print out the results. For example,
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)")