Data from API’s
In these slides we describe an example to get data from an API with R.
The content in these slides depend on the following packages:
Open Notify is an open source project to provide a simple programming interface (API) for some of NASA’s data.
This API is developed and maintained by Nathan Bergey.
URL:
http://api.open-notify.org/iss-now.json
Returned ISS Location 1 data in JSON format:
{"iss_position": {"longitude": "-58.8531", "latitude": "-28.5444"}, "message": "success", "timestamp": 1699732761}
timestamp is the number of seconds since Jan-01-1970 (UTC)
A simple approach to get the location of the ISS is to use readLines()
to import the data—returned by the URL—in R.
Because the data is in JSON format, we then use from JSON()
to convert it into an R list:
$iss_position
$iss_position$longitude
[1] "-58.8531"
$iss_position$latitude
[1] "-28.5444"
$message
[1] "success"
$timestamp
[1] 1699732761
"leaflet"
iss_loc_dat = data.frame(
lng = as.numeric(iss_loc_list$iss_position$longitude),
lat = as.numeric(iss_loc_list$iss_position$latitude)
)
leaflet(data = iss_loc_dat) |>
addTiles() |>
addCircleMarkers(
radius = 50,
stroke = FALSE,
fillOpacity = 0.3) |>
addMarkers() |>
setView(lng = iss_loc_dat$lng, lat = iss_loc_dat$lat, zoom = 3)
"leaflet"
, adding icon of ISS# url of ISS icon from Wikimedia Commons (png file)
wikimedia = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d0/"
iss = "International_Space_Station.svg/"
png = "320px-International_Space_Station.svg.png"
iss_icon = paste0(wikimedia, iss, png)
# define icon properties
issIcon <- makeIcon(
iconUrl = iss_icon,
iconWidth = 100,
iconHeight = 70,
iconAnchorX = 50,
iconAnchorY = 35)
# leaflet map
leaflet(data = iss_loc_dat) |>
addTiles() |>
addCircleMarkers(
lng = ~lng,
lat = ~lat,
radius = 70,
stroke = FALSE,
fillOpacity = 0.3) |>
addMarkers(
lng = ~lng,
lat = ~lat,
icon = issIcon) |>
setView(lng = iss_loc_dat$lng, lat = iss_loc_dat$lat, zoom = 2)
Error in get(x, envir = ns, inherits = FALSE): object '.b64EncodeFile' not found
Another approach to get the location of the ISS is to:
make an HTTP request with GET()
extract the content from the request object
Response [https://api.open-notify.org/astros.json]
Date: 2023-11-11 20:51
Status: 200
Content-Type: application/json
Size: 113 B
[1] "url" "status_code" "headers" "all_headers" "cookies"
[6] "content" "date" "times" "request" "handle"
[1] 7b 22 69 73 73 5f 70 6f 73 69 74 69 6f 6e 22 3a 20 7b 22 6c
[21] 61 74 69 74 75 64 65 22 3a 20 22 2d 31 32 2e 36 36 30 38 22
[41] 2c 20 22 6c 6f 6e 67 69 74 75 64 65 22 3a 20 22 2d 39 2e 35
[61] 30 37 33 22 7d 2c 20 22 6d 65 73 73 61 67 65 22 3a 20 22 73
[81] 75 63 63 65 73 73 22 2c 20 22 74 69 6d 65 73 74 61 6d 70 22
[101] 3a 20 31 36 39 39 38 39 34 37 31 32 7d
[1] "{\"iss_position\": {\"latitude\": \"-28.5444\", \"longitude\": \"-58.8531\"}, \"message\": \"success\", \"timestamp\": 1699732761}"
$iss_position
$iss_position$longitude
[1] "-58.8531"
$iss_position$latitude
[1] "-28.5444"
$message
[1] "success"
$timestamp
[1] 1699732761
URL:
http://api.open-notify.org/astros.json
Returned number of people 1 in JSON format:
{"message": "success", "people": [{"name": "Jasmin Moghbeli", "craft": "ISS"}, {"name": "Andreas Mogensen", "craft": "ISS"}, {"name": "Satoshi Furukawa", "craft": "ISS"}, {"name": "Konstantin Borisov", "craft": "ISS"}, {"name": "Oleg Kononenko", "craft": "ISS"}, {"name": "Nikolai Chub", "craft": "ISS"}, {"name": "Loral O'Hara", "craft": "ISS"}], "number": 7}
Response [https://api.open-notify.org/astros.json]
Date: 2023-11-11 20:52
Status: 200
Content-Type: application/json
Size: 360 B
$message
[1] "success"
$people
name craft
1 Jasmin Moghbeli ISS
2 Andreas Mogensen ISS
3 Satoshi Furukawa ISS
4 Konstantin Borisov ISS
5 Oleg Kononenko ISS
6 Nikolai Chub ISS
7 Loral O'Hara ISS
$number
[1] 7