NASA data

NASA Data

This page demonstrates how to use the API of NASA’s data from Open Notify to find how many humans are in space right now and the current location of International Space Station (ISS). Those who are curious about space would want to use this to easily find information about this ISS and astronauts. Perhaps they could use the ISS location to map its path or calculate its distance from Earth. The creator of Open Notify created a lamp that lights up when the ISS flies over it.

Load packages

library(tidyverse) 
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.6
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.1     ✔ tibble    3.3.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(jsonlite) 

Attaching package: 'jsonlite'

The following object is masked from 'package:purrr':

    flatten
library(magrittr) 

Attaching package: 'magrittr'

The following object is masked from 'package:purrr':

    set_names

The following object is masked from 'package:tidyr':

    extract
library(httr)   

Setting up the API

The API is very easy to use. You either add “iss-now.json” to retrieve information about the international space station or “astros.json” to retrieve information about the astronauts in space right now, to the end of the base URL.

#base url- http://api.open-notify.org/

iss_url <- "http://api.open-notify.org/iss-now.json"
astro_info <- "http://api.open-notify.org/astros.json"

Calling the API

Table of all the astronauts in space right now

First let’s make a table of everyone’s name and craft that they are on in space right now.

First use GET function to retrieve the information from the API.

astros_data <-
  astro_info %>% 
  GET() %>% 
  content(as = "text",
          encoding = "UTF-8") %>% 
  fromJSON() 

Then use the use series function to select the “people” list of all astronauts and crafts in space right now.

astros_in_space  <-
  astros_data %>%
  use_series(people) 

What if we wanted to create a vector that shows the number of people in space right now? Use the use series function to select the “number” integer.

num_astros_in_space  <-
  astros_data %>%
  use_series(number)

Finding the longitude and latitude of the International Space Station right now

First use GET function to retrieve the information from the API.

iss_data <-
  iss_url %>% 
  GET() %>% 
  content(as = "text",
          encoding = "UTF-8") %>% 
  fromJSON() 

Then use the use series function to select the “iss_position” list.

coordinates <-
  iss_data %>%
  use_series(iss_position) 

Finally put the longitude and latitude as a number in a data frame.

latitude_longitude <- data.frame(
  latitude = as.numeric(coordinates$latitude),
  longitude = as.numeric(coordinates$longitude)
)