diff --git a/DESCRIPTION b/DESCRIPTION index 23d27d5..a8c584f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,7 +22,7 @@ Description: A fast JSON parser and generator optimized for statistical data package verify that all edge cases are encoded and decoded consistently for use with dynamic data in systems and applications. Suggests: - curl (>= 0.5), + httr, plyr, testthat, knitr, diff --git a/inst/doc/json-aaquickstart.html b/inst/doc/json-aaquickstart.html index 4ec5fbb..e415fc6 100644 --- a/inst/doc/json-aaquickstart.html +++ b/inst/doc/json-aaquickstart.html @@ -9,20 +9,21 @@ - + + - + - - - - + + + + - - + + + + @@ -52,15 +81,43 @@ color: inherit; background-color: rgba(0, 0, 0, 0.04); } -img { - max-width:100%; - height: auto; +img { + max-width:100%; + height: auto; +} +.tabbed-pane { + padding-top: 12px; +} +button.code-folding-btn:focus { + outline: none; } + +
+ + + + + + + + + + +

Getting started with JSON and jsonlite

@@ -73,6 +130,12 @@

Getting started with JSON and jsonlite

Simplification

Simplification is the process where JSON arrays automatically get converted from a list into a more specific R class. The fromJSON function has 3 arguments which control the simplification process: simplifyVector, simplifyDataFrame and simplifyMatrix. Each one is enabled by default.

++++++ @@ -216,6 +279,8 @@

Matrices and Arrays

+ + + - - - - + + + + - - + + + + @@ -52,17 +81,43 @@ color: inherit; background-color: rgba(0, 0, 0, 0.04); } -img { - max-width:100%; - height: auto; +img { + max-width:100%; + height: auto; +} +.tabbed-pane { + padding-top: 12px; +} +button.code-folding-btn:focus { + outline: none; } + +
+ + + + + + + + + + + +

CitiBike NYC

@@ -125,7 +180,7 @@

CitiBike NYC

[15] "altitude" "testStation" [17] "lastCommunicationTime" "landMark"
nrow(stations)
-
[1] 509
+
[1] 508

Ergast

@@ -192,20 +247,20 @@

ProPublica

New York Times

-

The New York Times has several APIs as part of the NYT developer network. These interface to data from various departments, such as news articles, book reviews, real estate, etc. Registration is required (but free) and a key can be obtained at here. The code below includes some example keys for illustration purposes.

+

The New York Times has several APIs as part of the NYT developer network. These interface to data from various departments, such as news articles, book reviews, real estate, etc. Registration is required (but free) and a key can be obtained at here. The code below includes some example keys for illustration purposes.

#search for articles
 article_key <- "&api-key=c2fede7bd9aea57c898f538e5ec0a1ee:6:68700045"
 url <- "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=obamacare+socialism"
 req <- fromJSON(paste0(url, article_key))
 articles <- req$response$docs
 colnames(articles)
-
 [1] "web_url"          "snippet"          "lead_paragraph"  
- [4] "abstract"         "print_page"       "blog"            
- [7] "source"           "multimedia"       "headline"        
-[10] "keywords"         "pub_date"         "document_type"   
-[13] "news_desk"        "section_name"     "subsection_name" 
-[16] "byline"           "type_of_material" "_id"             
-[19] "word_count"      
+
 [1] "web_url"           "snippet"           "lead_paragraph"   
+ [4] "abstract"          "print_page"        "blog"             
+ [7] "source"            "multimedia"        "headline"         
+[10] "keywords"          "pub_date"          "document_type"    
+[13] "news_desk"         "section_name"      "subsection_name"  
+[16] "byline"            "type_of_material"  "_id"              
+[19] "word_count"        "slideshow_credits"
#search for best sellers
 bestseller_key <- "&api-key=5e260a86a6301f55546c83a47d139b0d:3:68700045"
 url <- "http://api.nytimes.com/svc/books/v2/lists/overview.json?published_date=2013-01-01"
@@ -225,19 +280,17 @@ 

New York Times

req <- fromJSON(paste0(url, movie_key)) reviews <- req$results colnames(reviews)
-
 [1] "nyt_movie_id"     "display_title"    "sort_name"       
- [4] "mpaa_rating"      "critics_pick"     "thousand_best"   
- [7] "byline"           "headline"         "capsule_review"  
-[10] "summary_short"    "publication_date" "opening_date"    
-[13] "dvd_release_date" "date_updated"     "seo_name"        
-[16] "link"             "related_urls"     "multimedia"      
+
 [1] "display_title"    "mpaa_rating"      "critics_pick"    
+ [4] "byline"           "headline"         "summary_short"   
+ [7] "publication_date" "opening_date"     "date_updated"    
+[10] "link"             "multimedia"      
reviews[1:5, c("display_title", "byline", "mpaa_rating")]
-
       display_title         byline mpaa_rating
-1    Tom at the Farm Stephen Holden          NR
-2     A Little Chaos Stephen Holden           R
-3           Big Game   Andy Webster        PG13
-4          Balls Out   Andy Webster           R
-5 Mad Max: Fury Road    A. O. Scott           R
+
           display_title          byline mpaa_rating
+1                Dheepan     A. O. SCOTT           R
+2  Mothers and Daughters NEIL GENZLINGER       PG-13
+3 Phantom of the Theater  DANIEL M. GOLD            
+4    Beautiful Something  STEPHEN HOLDEN   Not Rated
+5           Elstree 1976 NEIL GENZLINGER            

CrunchBase

@@ -255,12 +308,18 @@

Sunlight Foundation

drone_bills <- fromJSON(paste0("http://openstates.org/api/v1/bills/?q=drone", key)) drone_bills$title <- substring(drone_bills$title, 1, 40) print(drone_bills[1:5, c("title", "state", "chamber", "type")]) -
                                     title state chamber type
-1                            WILDLIFE-TECH    il   lower bill
-2 Criminalizes the unlawful use of an unma    ny   lower bill
-3 Criminalizes the unlawful use of an unma    ny   lower bill
-4 Relating to: criminal procedure and prov    wi   lower bill
-5 Relating to: criminal procedure and prov    wi   upper bill
+
                                     title state chamber
+1    An act relating to privacy protection    vt   upper
+2             Crimes: emergency personnel.    ca   lower
+3                    DRONE TASK FORCE APPT    il   lower
+4 Omnibus K-12 and higher education policy    mn   lower
+5 Relates to prohibiting civilian drone us    ny   upper
+                                   type
+1                                  bill
+2 bill, fiscal committee, local program
+3                                  bill
+4                                  bill
+5                                  bill
#Congress mentioning "constitution"
 res <- fromJSON(paste0("http://capitolwords.org/api/1/dates.json?phrase=immigration", key))
 wordcount <- res$results
@@ -268,11 +327,11 @@ 

Sunlight Foundation

summary(wordcount)
     count              day               raw_count      
  Min.   :   1.00   Min.   :1996-01-02   Min.   :   1.00  
- 1st Qu.:   3.00   1st Qu.:2001-01-22   1st Qu.:   3.00  
- Median :   8.00   Median :2005-11-16   Median :   8.00  
- Mean   :  25.27   Mean   :2005-10-02   Mean   :  25.27  
- 3rd Qu.:  21.00   3rd Qu.:2010-05-12   3rd Qu.:  21.00  
- Max.   :1835.00   Max.   :2015-08-05   Max.   :1835.00  
+ 1st Qu.: 3.00 1st Qu.:2001-03-26 1st Qu.: 3.00 + Median : 8.00 Median :2006-03-21 Median : 8.00 + Mean : 24.91 Mean :2006-01-30 Mean : 24.91 + 3rd Qu.: 21.00 3rd Qu.:2010-12-08 3rd Qu.: 21.00 + Max. :1835.00 Max. :2016-04-27 Max. :1835.00
#Local legislators
 legislators <- fromJSON(paste0("http://congress.api.sunlightfoundation.com/",
   "legislators/locate?latitude=42.96&longitude=-108.09", key))
@@ -290,10 +349,9 @@ 

Twitter

consumer_secret = "OIDC4MdfZJ82nbwpZfoUO4WOLTYjoRhpHRAWj6JMec"; #Use basic auth -library(httr) -secret <- RCurl::base64(paste(consumer_key, consumer_secret, sep = ":")); -req <- POST("https://api.twitter.com/oauth2/token", - add_headers( +secret <- openssl::base64_encode(paste(consumer_key, consumer_secret, sep = ":")); +req <- httr::POST("https://api.twitter.com/oauth2/token", + httr::add_headers( "Authorization" = paste("Basic", secret), "Content-Type" = "application/x-www-form-urlencoded;charset=UTF-8" ), @@ -305,23 +363,25 @@

Twitter

#Actual API call url <- "https://api.twitter.com/1.1/statuses/user_timeline.json?count=10&screen_name=Rbloggers" -req <- GET(url, add_headers(Authorization = token)) -json <- content(req, as = "text") +req <- httr::GET(url, add_headers(Authorization = token)) +json <- httr::content(req, as = "text") tweets <- fromJSON(json) substring(tweets$text, 1, 100)
-
 [1] "Analysing longitudinal data: Multilevel growth models (II) http://t.co/unUxszG7VJ #rstats"           
- [2] "RcppDE 0.1.4 http://t.co/3qPhFzoOpj #rstats"                                                         
- [3] "Minimalist Maps http://t.co/fpkNznuCoX #rstats"                                                      
- [4] "Tutorials freely available of course I taught: including ggplot2, dplyr and shiny http://t.co/WsxX4U"
- [5] "Deploying Shiny apps with shinyapps.io http://t.co/tjef1pbKLt #rstats"                               
- [6] "Bootstrap Evaluation of Clusters http://t.co/EbY7ziKCz5 #rstats"                                     
- [7] "Add external code to Rmarkdown http://t.co/RCJEmS8gyP #rstats"                                       
- [8] "Linear models with weighted observations http://t.co/pUoHpvxAGC #rstats"                             
- [9] "dplyr 0.4.3 http://t.co/ze3zc8t7qj #rstats"                                                          
-[10] "xkcd survey and the power to shape the internet http://t.co/vNaKhxWxE4 #rstats"                      
+
 [1] "Rblpapi 0.3.4 https://t.co/NoGluTGSld #rstats #DataScience"                                        
+ [2] "Introducing DohaR – A new R User Group in Doha, Qatar https://t.co/MWSKgMap9I #rstats #DataScience"
+ [3] "FSA v0.8.7 Released https://t.co/2nnGMGINA7 #rstats #DataScience"                                  
+ [4] "Too Much Parallelism is as Bad https://t.co/t0RffUvLYs #rstats #DataScience"                       
+ [5] "Coming up: principal components analysis https://t.co/f9dUnhuC9n #rstats #DataScience"             
+ [6] "Shiny Apps Gallery using Plotly in R https://t.co/cEFvgy1VPY #rstats #DataScience"                 
+ [7] "Who is going down? Bundesliga Betting Odds – updated https://t.co/uvpcmOmcaf #rstats #DataScience" 
+ [8] "BH 1.60.0-2 https://t.co/kZHVjPzG65 #rstats #DataScience"                                          
+ [9] "Red herring bites https://t.co/IJQwBQkImx #rstats #DataScience"                                    
+[10] "R Tools for Visual Studio 3.0 now available https://t.co/JWKthTwEnC #rstats #DataScience"          
+ +
+ - - - - + + + + - - + + + + @@ -52,17 +81,43 @@ color: inherit; background-color: rgba(0, 0, 0, 0.04); } -img { - max-width:100%; - height: auto; +img { + max-width:100%; + height: auto; +} +.tabbed-pane { + padding-top: 12px; +} +button.code-folding-btn:focus { + outline: none; } + +
+ + + + + + + + + + + + + +
JSON structure