Open In App

How to find IP address information using R

Last Updated : 25 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be extracting IP information using IPinfo API.

IPinfo is a free API that is used to get information on specific IP addresses such as their location. We will be using httr GET() to make URL requests and store data in a variable. Now we will need JSON data so we have to convert the data into char format because GET() returns raw data so we will need to use rawToChar() and to convert char data in JSON format we will be using fromJSON() built-in jsonlite package.

Note: You need not include install.package() in the script if you already have the package installed.

Example:

R




# installing required packages
install.packages("httr")
install.packages("jsonlite")
 
# importing packages/libraries
library(httr)
library(jsonlite)
 
# storing url in a variable
# you can also pass the url directly in GET()
 
# requesting raw data from URL using GET()
# and storing in data variable.
data <- GET(url1)
 
# converting raw data to char format.
rawdata <- rawToChar(data$content)
 
# converting char data to json format.
jsondata <- fromJSON(rawdata)
 
# printing output
# we are using cat() instead of print function
# so that we can use new line
cat(paste("IP:", jsondata$ip,"\nCITY:",
          jsondata$city,"\nRegion:",
          jsondata$region,"\nCOUNTRY:",
          jsondata$country,"\nCOORDINATES:",
          jsondata$loc))


Output:

IP: 161.185.160.93  

CITY: New York City  

Region: New York  

COUNTRY: US  

COORDINATES: 40.7143,-74.0060

Example: Getting input manually from the user

To get input from the user we will be readline() and convert it in character using as.character(). Now we will use paste0() to complete the URL and pass it in GET() function.

R




# installing required packages
install.packages("httr")
install.packages("jsonlite")
 
# importing packages/libraries
library(httr)
library(jsonlite)
 
# storing incomplete url in a variable
 
# manually taking ip as a input from user
ip = readline("Enter your IP address:")
 
# converting input ip address to character
ip = as.character(ip)
 
# completing url
url1 = paste0(url,ip,"/geo")
 
# requesting raw data from URL using GET()
# and string in rawdata variable.
rawdata <- GET(url1)
 
# converting raw data to char format.
chardata <- rawToChar(rawdata$content)
 
# converting char data to json format.
jsondata <- fromJSON(chardata)
 
# printing output
# we are using cat() instead of print function
# so that we can use new line
cat(paste("IP:", jsondata$ip,"\nCITY:",
          jsondata$city,"\nRegion:",
          jsondata$region,"\nCOUNTRY:",
          jsondata$country,"\nCOORDINATES:",
          jsondata$loc))


Output:

Enter your IP address:161.185.160.93

IP: 161.185.160.93  

CITY: New York City  

Region: New York  

COUNTRY: US  

COORDINATES: 40.7143,-74.0060



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads