-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdemonstration.Rmd
107 lines (72 loc) · 2.48 KB
/
demonstration.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
---
title: "Using R to Orchestrate APIs -- Demonstration"
author: "John Little"
date: '`r Sys.Date()`'
output:
html_notebook: default
---
## Demonstration
### To Follow Along
- Open the Web Page - http://www.johnlittle.info/project/custom/rcs2017/handson.nb.html
**OR**
1. Open the Notebook in an RStudio Docker Container - https://vm-manage.oit.duke.edu/containers/rstudio
2. File > New Project...
3. Version Control > Git
4. Repository URL = https://github.com/libjohn/r-api-json.git > Create Project
5. Open *demonstration.Rmd* file
+ Run All
---
### Overview of OMDB api
- http://www.omdb.org/
- like http://imdb.com/
- no API keys required
- **The API** http://www.omdbapi.com/
---
### Demonstration
```{r load-library-package}
# https://cran.r-project.org/web/packages/jsonlite/vignettes/json-aaquickstart.html
library(jsonlite)
```
### Single JSON object
When the API response is a single JSON object, JSONlite puts the response into a single item list which allows for a simple view the data.
```{r singleJSONresult}
oneJSONresult <- fromJSON("http://www.omdbapi.com/?t=rocky&y=&plot=full&r=json")
```
---
```{r}
oneJSONresult
```
---
##### The elements of the list behave as you would expect in R.
- List all the variable names.
```{r}
names(oneJSONresult)
```
- List an individual element
```{r}
oneJSONresult$Title
```
```{r}
oneJSONresult$Awards
```
---
### Multiple objects in API response
Note: The **results of this code-snippet react differently** between the *console*, the *Notebook script* (console), and the *Notebook HTML* output. In the script-output you can find the object names of the list, in this case dollar-search: `$Search`. Or, you can use bracket notation: `[[1]]`. Once you identify the object name, it's easier to identify the variable names within the `$Search` data frame.
Below, the structure of the API response for a series search is to have a data.frame with the search results (`$Search`), and two other list components (`$TotalResults`, `$Response`). We're interested in the results, the `$Search` data.frame.
```{r}
jsonSeriesResultsList <- fromJSON("http://www.omdbapi.com/?s=rocky&type=series&r=json&page=1")
jsonSeriesResultsList
```
---
#### Search results of the JSON list as a data frame.
```{r}
jsonSeriesResultsList$Search
```
---
#### Title vector of the Search Data Frame
```{r}
jsonSeriesResultsList$Search$Title
```
---
## Next
- [Handson.Rmd](http://www.johnlittle.info/project/custom/rcs2017/handson.nb.html)