NASA API & MAP - Gavin Steele

with an introduction to Quarto

Author

Gavin Steele

Load Libraries

library(tidyverse)
library(jsonlite)  # Converting json data into data frames
library(magrittr)  # Extracting items from list objects using piping grammar
library(httr)
library(knitr)

NASA API

I want to use the open notify api accessed from http://open-notify.org.

Connecting to the API

url <- "http://api.open-notify.org/iss-now.json"

url_content <- url %>% 
  GET()

Trans-coding to JSON

url_useable <- 
  url_content %>% 
  content(as = "text",
          encoding = "UTF-8") %>% # UTF-8 is the <near> universal standard
  fromJSON() %>% 
  use_series(iss_position)

Sandwich it Together

entity <- function(){
url <- "http://api.open-notify.org/iss-now.json"


url_content <- 
  url %>% 
  GET() %>% 
  content(as = "text",encoding = "UTF-8") %>% # UTF-8 is the <near> universal
  fromJSON()


latitude <- 
    url_content %>% 
    use_series(iss_position) %>% 
    use_series(latitude)
  
longitude <- 
    url_content %>% 
    use_series(iss_position) %>% 
    use_series(longitude)

timestamp <-
  url_content %>% 
  use_series(timestamp)
observance <- data.frame(latitude,longitude, timestamp)

return(observance)
}
test <- entity()

Call in the cavalry because Use Series is foreign

several<- function(times){
  observances <- data.frame()
  for (i in 1:times){
    observances<-bind_rows(observances, entity())
  print((i/times))
    Sys.sleep(1)
  }

return(observances)    
}

sillygoose <- several(60)
[1] 0.01666667
[1] 0.03333333
[1] 0.05
[1] 0.06666667
[1] 0.08333333
[1] 0.1
[1] 0.1166667
[1] 0.1333333
[1] 0.15
[1] 0.1666667
[1] 0.1833333
[1] 0.2
[1] 0.2166667
[1] 0.2333333
[1] 0.25
[1] 0.2666667
[1] 0.2833333
[1] 0.3
[1] 0.3166667
[1] 0.3333333
[1] 0.35
[1] 0.3666667
[1] 0.3833333
[1] 0.4
[1] 0.4166667
[1] 0.4333333
[1] 0.45
[1] 0.4666667
[1] 0.4833333
[1] 0.5
[1] 0.5166667
[1] 0.5333333
[1] 0.55
[1] 0.5666667
[1] 0.5833333
[1] 0.6
[1] 0.6166667
[1] 0.6333333
[1] 0.65
[1] 0.6666667
[1] 0.6833333
[1] 0.7
[1] 0.7166667
[1] 0.7333333
[1] 0.75
[1] 0.7666667
[1] 0.7833333
[1] 0.8
[1] 0.8166667
[1] 0.8333333
[1] 0.85
[1] 0.8666667
[1] 0.8833333
[1] 0.9
[1] 0.9166667
[1] 0.9333333
[1] 0.95
[1] 0.9666667
[1] 0.9833333
[1] 1

Put it on a map?

library(leaflet)
leaflet(sillygoose) %>% 
  addTiles() %>% 
  addMarkers(as.numeric(sillygoose$longitude), as.numeric(sillygoose$latitude)) %>%
  addPolylines(as.numeric(sillygoose$longitude), as.numeric(sillygoose$latitude), color ="purple")