-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathejemplo3_reporte.R
90 lines (59 loc) · 2.87 KB
/
ejemplo3_reporte.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
library(tictoc)
library(tidyverse)
library(furrr)
plan("multisession", workers = 8)
datos_paises <- gapminder::gapminder
# Que funcione para 1 -----------------------------------------------------
rmarkdown::render(input = "reporte/reporte_template.Rmd",
params = list(pais = "Chile"),
output_dir = "reporte/",
output_file = "reporte_chile.docx"
)
# Ahora hacemos la función ------------------------------------------------
crea_reporte <- function(pais){
rmarkdown::render(input = "reporte/reporte_template.Rmd",
params = list(pais = pais),
output_dir = "reporte/",
output_file = paste0("reporte_", janitor::make_clean_names(pais), ".docx"))
}
crea_reporte("Peru")
crea_reporte_carpeta <- function(pais, carpeta){
rmarkdown::render(input = "reporte/reporte_template.Rmd",
params = list(pais = pais),
output_dir = paste("reporte/", carpeta),
output_file = paste0("/reporte_", janitor::make_clean_names(pais), ".docx"))
}
crea_reporte_carpeta("Peru", "a")
#
# Esto que sigue no es independiente --------------------------------------
# Pero cuando lo hago en serie (no paralelo) con map, funciona bien
tic()
map(as.character(unique(datos_paises$country)), ~crea_reporte_carpeta(.x, "no_paralelo"))
toc()
# Se cae porque no es independiente al intentar escribir el mismo archivo en la misma carpeta
tic()
furrr::future_map(as.character(unique(datos_paises$country)), ~crea_reporte_carpeta(.x, "paralelo"))
toc()
# tengo que prevenir que el archivo temporal no se haga siempre en la misma carpeta
crea_reporte_carpeta2 <- function(pais, carpeta){
rmarkdown::render(input = "reporte/reporte_template.Rmd",
params = list(pais = pais),
output_dir = paste("reporte/", carpeta),
intermediates_dir = paste("reporte/", carpeta), # acá cambio la ruta del archivo intermedio
output_file = paste0("/reporte_", janitor::make_clean_names(pais), ".docx"))
}
# sigue siendo no independiente
tic()
furrr::future_map(as.character(unique(datos_paises$country)), ~crea_reporte_carpeta2(.x, "paralelo"))
toc()
# Veamos ahora creando una carpeta para cada reporte
crea_reporte_carpeta3 <- function(pais, carpeta){
rmarkdown::render(input = "reporte/reporte_template.Rmd",
params = list(pais = pais),
output_dir = paste0(getwd(), "/reporte/", carpeta, "/", pais),
intermediates_dir =paste0(getwd(), "/reporte/", carpeta, "/", pais),
output_file = paste0("/reporte_", janitor::make_clean_names(pais), ".docx"))
}
tic()
furrr::future_map(as.character(unique(datos_paises$country)), ~crea_reporte_carpeta3(.x, "paralelo"))
toc()