-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathejemplo2_graficos.R
121 lines (73 loc) · 2.66 KB
/
ejemplo2_graficos.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
109
110
111
112
113
114
115
116
117
118
119
library(tictoc)
library(tidyverse)
datos_paises <- gapminder::gapminder
# Primero una prueba de concepto ------------------------------------------
datos_paises %>%
filter(country == "Chile") %>%
ggplot(aes(x = year, y = lifeExp))+
geom_line(color = "plum")+
geom_point(color = "plum")+
facet_wrap(~country)
# Ahora hago la función ---------------------------------------------------
plot_pais <- function(pais){
datos_paises %>%
filter(country == pais) %>%
ggplot(aes(x = year, y = lifeExp))+
geom_line(color = "plum")+
geom_point(color = "plum")+
geom_smooth()+
facet_wrap(~country)
}
# Probar si funciona con otro país ----------------------------------------
plot_pais("Argentina")
plot_pais("Mexico")
algunos_paises <- sample(unique(datos_paises$country), 10)
system.time(map(algunos_paises, ~plot_pais(.x)))
# Ahora para todos los países ---------------------------------------------
system.time(map(unique(datos_paises$country), ~plot_pais(.x)))
library(tictoc)
tic()
map(unique(datos_paises$country), ~plot_pais(.x))
toc() # 23.623 sec elapsed
# Ahora paralelizado ------------------------------------------------------
library(furrr)
plan(strategy = "future::multisession", workers = future::availableCores() - 2)
tic()
furrr::future_map(unique(datos_paises$country), ~plot_pais(.x))
toc() # 24.738 sec elapsed
tic()
furrr::future_map(unique(datos_paises$country), ~plot_pais(.x))
toc() # 24.738 sec elapsed
# Ahora guardando gráficos ------------------------------------------------
plot_pais_guardar <- function(pais){
p1 <- datos_paises %>%
filter(country == pais) %>%
ggplot(aes(x = year, y = lifeExp))+
geom_line(color = "plum")+
geom_point(color = "plum")+
geom_smooth()+
facet_wrap(~country)
ggsave(filename = paste0("graficos/plot_", janitor::make_clean_names(pais), ".png"), plot = p1)
}
plot_pais_guardar("Chile")
plan("multisession", workers = 8)
tic()
furrr::future_map(unique(datos_paises$country), ~plot_pais_guardar(.x))
toc()
# Ahora hago la función pero con una carpeta de destino -------------------
plot_pais_guardar_carpeta <- function(pais, carpeta){
p1 <- datos_paises %>%
filter(country == pais) %>%
ggplot(aes(x = year, y = lifeExp))+
geom_line(color = "plum")+
geom_point(color = "plum")+
geom_smooth()+
facet_wrap(~country)
ggsave(filename = paste0("graficos/",carpeta, "/plot_", janitor::make_clean_names(pais), ".png"), plot = p1)
}
tic()
map(unique(datos_paises$country), ~plot_pais_guardar_carpeta(.x, "no_paralelo"))
toc()
tic()
furrr::future_map(unique(datos_paises$country), ~plot_pais_guardar_carpeta(.x, "paralelo"))
toc()