-
Notifications
You must be signed in to change notification settings - Fork 24
/
purrr.R
97 lines (58 loc) · 1.86 KB
/
purrr.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
library(tidyverse)
library(stringr)
gapminder <- gapminder::gapminder
gapminder
gapminder %>%
filter(str_detect(country, "Rep")) %>%
select(country) %>%
distinct()
dr_string <- "Congo, Dem. Rep."
dr_string
dr_pattern <- ", Dem\\. Rep\\."
str_detect(dr_string, dr_pattern)
str_replace(dr_string, dr_pattern, "")
str_c("Democratic Republic of ",
str_replace(dr_string, dr_pattern, ""))
start <- str_locate(dr_string, dr_pattern)[1,1]
start
str_sub(dr_string, start = start)
str_sub(dr_string, end = start-1)
gapm_text <- read_lines("https://www.gapminder.org/wp-content/themes/gapminder/cronJobs/json.js")
gapm_text
g_pattern <- "var indicatorsJson="
str_replace(gapm_text, g_pattern, "")
str_locate(gapm_text, g_pattern)
gapm_text <- str_sub(gapm_text, start=20)
gapm_json <- str_c("[", gapm_text, "]")
gapm_list <- jsonlite::fromJSON(gapm_json,
simplifyVector = FALSE)
gapm_list %>% View
# subsetting lists
# does not peel off the packaging
gapm_list[1] %>% View
# extracts the content of a list.
# peeling off the packaging
gapm_list[[1]] %>% View
gapm_list <- gapm_list[[1]]
length(gapm_list)
gapm_list[[1]] %>% View
# reaching out to 5th element of 1 element of the list
gapm_list[[1]][[5]]
gapm_list[[1]][["dataprovider_link"]]
gapm_list[[1]]$dataprovider_link
map(gapm_list, 5) %>% View
map(gapm_list, "indicatorName") %>% View
map(gapm_list, "indicatorName") %>%
simplify()
#
# map(<LIST>, <FUNCTION>, <ARGUMENTS_IF_ANY>)
a <- 1:5
b <- 4:9
tibble(sheet_id=names(gapm_list),
indicatorName=map(gapm_list, "indicatorName") %>% simplify(),
category=map_chr(gapm_list, "category"),
subcategory=map_chr(gapm_list, "subcategory"),
dataprovider=map_chr(gapm_list, "dataprovider"),
dataprovider_link=map_chr(gapm_list, "dataprovider_link")
) %>%
write_csv("gapminder_datasources.csv")