-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.Rmd
118 lines (87 loc) · 5.32 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = here::here("man/figures/README-"),
out.width = "100%"
)
```
<!-- badges: start -->
[![Travis build status](https://travis-ci.org/eringrand/astropic.svg?branch=master)](https://travis-ci.org/eringrand/astropic)
<!-- badges: end -->
# astropic
The goal of `astropic` is to connect R to the [NASA APOD API](https://github.com/nasa/apod-api). The APOD API supports one image at a time. In order to supply more than that, this package also includes creating time ranges (of less than 2000 days at a time) and some historical data in tibble format.
Thanks to Michael W. Kearney, author of [rtweet](http://rtweet.info), for having a robust package based on connecting to an API. I didn't know much about APIs when I started this project and looking at his source code helped a ton!
Credit to the APOD API [contributors](https://github.com/nasa/apod-api/graphs/contributors) for all of their work in making the API, and the recent re-org.
## Installation
You can install the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("eringrand/astropic")
```
## API Key
To start, you'll need a NASA API key. If you do not have one, you can get one [here](https://api.nasa.gov/index.html#apply-for-an-api-key). Once you put in your information, a key will be emailed to you.
Save this to your environment as `NASA_KEY`. e.g `Sys.setenv(NASA_KEY = "YOURKEYHERE")`.
## Query
The query parameters are described on the [APOD API Github page](https://github.com/nasa/apod-api) as such...
- `date` A string in YYYY-MM-DD format indicating the date of the APOD image (example: 2014-11-03). Defaults to today's date. Must be after 1995-06-16, the first day an APOD picture was posted. There are no images for tomorrow available through this API.
- `concept_tags` A Boolean True|False indicating whether concept tags should be returned with the rest of the response. The concept tags are not necessarily included in the explanation, but rather derived from common search tags that are associated with the description text. (Better than just pure text search.) Defaults to False.
- `hd` A Boolean True|False parameter indicating whether or not high-resolution images should be returned. This is present for legacy purposes, it is always ignored by the service and high-resolution urls are returned regardless.
- `count` A positive integer, no greater than 100. If this is specified then count randomly chosen images will be returned in a JSON array. Cannot be used in conjunction with date or `start_date` and `end_date`.
- `start_date` A string in YYYY-MM-DD format indicating the start of a date range. All images in the range from `start_date` to end_date will be returned in a JSON array. Cannot be used with date.
- `end_date` A string in YYYY-MM-DD format indicating that end of a date range. If `start_date` is specified without an `end_date` then `end_date` defaults to the current date.
thumbs A Boolean parameter True|False indicating whether the API should return a thumbnail image URL for video files. If set to True, the API returns URL of video thumbnail. If an APOD is not a video, this parameter is ignored.
## Example
This is a basic example to retrieve APOD data.
### Returned fields
- `date` Date of image. Included in response because of default values.
- `explanation` The supplied text explanation of the image.
- `hdurl` The URL for any high-resolution image for that day. Will be omitted in the response IF it does not exist originally at APOD.
- `media_type` The type of media (data) returned. May either be 'image' or 'video' depending on content.
- `service_version` The service version used.
- `title` The title of the image.
- `url` The URL of the APOD image or video of the day.
- `copyright` The name of the copyright holder.
### Basic Example
```{r example1, warning=FALSE}
library(astropic)
get_apod() # no inputs will get today's image
```
### Providing a date range
You can also supply a start and end date to get a range of image results back.
```{r}
get_apod(query = list(start_date = "2018-04-01", end_date = "2018-04-03"))
```
### Count - `n` random images
```{r}
get_apod(query = list(count = 5))
```
### Magic
With a little `magick` you can also save the APOD image to your computer for use later. This is a demonstration of a picture in APOD I helped to create.
```{r m31, message=FALSE, warning=FALSE}
library(magick)
library(here)
library(dplyr)
save_image <- function(url){
image <- try(magick::image_read(url), silent = FALSE)
image_name <- gsub(".*/([^/]+$)", '\\1', m31$hdurl)
image_loc <- here::here("man/figures/README", image_name)
if(class(image)[1] != "try-error"){
image %>%
magick::image_write(image_loc)
}
return(image)
}
m31 <- get_apod(query = list(date = "2009-09-17")) # only providing a start date will give the image just for that day
dplyr::pull(m31, explanation)
save_image(m31$hdurl)
```
## Contact
Come find me on twitter @[astroeringand](https://twitter.com/astroeringrand)
## Code of Conduct
Please note that this project is released with a [Contributor Code of Conduct](.github/CODE_OF_CONDUCT.md).
By participating in this project you agree to abide by its terms.