-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_from_web.r
108 lines (83 loc) · 2.67 KB
/
data_from_web.r
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
library(readr)
url_csv <- "http://s3.amazonaws.com/assets.datacamp.com/course/importing_data_into_r/swimming_pools.csv"
pools <- read_csv(url_csv)
str(pools)
url_delim <- "http://s3.amazonaws.com/assets.datacamp.com/course/importing_data_into_r/potatoes.txt"
potatoes <- read_tsv(url_delim)
str(potatoes)
## csv on https
urls_csv <- "https://s3.amazonaws.com/assets.datacamp.com/course/importing_data_into_r/swimming_pools.csv"
pools2 <- read.csv(urls_csv)
head(pools2)
## Rdata
url_rdata <- "https://s3.amazonaws.com/assets.datacamp.com/course/importing_data_into_r/wine.RData"
wine.fp <- file.path("data","wine.RData")
download.file(url_rdata,wine.fp)
load(wine.fp)
summary(wine)
head(wine)
## using httr
# Load the httr package
if(!require(httr)) install.packages("httr")
library(httr)
# Get the url, save response to resp
url <- "http://www.example.com/"
resp <- GET(url)
# Print resp
print(resp)
# Get the raw content of resp
raw_content <- content(resp,as='raw')
# Print the head of content
head(raw_content)
## variation:
# Get the url
url <- "https://www.omdbapi.com/?t=Annie+Hall&y=&plot=short&r=json"
resp <- GET(url)
# Print resp
print(resp)
# Print content of resp as text
print("text/html")
# Print content of resp
print(content(resp))
print(content(resp,as="text"))
str(resp)
## API's better if not in HTML:
rm0.uri <- "http://www.omdbapi.com/?i=tt0095953&r=json"
rm.uri <- "http://www.omdbapi.com/?t=Rain+Man&y=&plot=short&r=json"
resp <- GET(rm.uri)
resp
content(resp)
content(resp,as="text")
if(!require(jsonlite)) install.packages("jsonlite")
fromJSON(content(resp,as="text")) # same as content()
fromJSON(rm.uri) # the same again
str(fromJSON(rm.uri)) # looks much more meaningful
ls(package:jsonlite) # 14 functions,toJSON is also possible
toJSON(cars)
toJSON(mtcars)
## you can prettify it:
prettify(toJSON(mtcars))
toJSON(mtcars,pretty=T) # same with a switch
## and that one can be minified:
minify(prettify(toJSON(mtcars))) # back to original
## fromJSON:
wine_json <- '{"name":"Chateau Migraine", "year":1997, "alcohol_pct":12.4, "color":"red", "awarded":false}'
fromJSON(wine_json)
quandl_url <- "http://www.quandl.com/api/v1/datasets/IWS/INTERNET_INDIA.json?auth_token=i83asDsiWUUyfoypkgMz"
quandl_data <- fromJSON(quandl_url)
str(quandl_data)
## some json finger practice:
# Experiment 1
json1 <- '[1,2,3,4,5,6]'
#json1 <- 1:6
fromJSON(json1)
# Experiment 2
#json2 <- '{"a": [1, 2, 3]}'
json2 <-'{"a":[1,2,3],"b":[4,5,6]}'
fromJSON(json2)
# Experiment 3
json3 <- '[[1,2],[3,4]]'
fromJSON(json3)
# Experiment 4
json4 <- '[{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6}]'
fromJSON(json4)