-
Notifications
You must be signed in to change notification settings - Fork 5
/
_make.R
71 lines (55 loc) · 2.55 KB
/
_make.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
## Run all files to prepare "Women in Parliament -- data.table"
# Setup
library(here)
library(pdftools)
library(magick)
library(magrittr)
# Confirm directory location
here()
# Download the zipped data file and extract the files to the "data/" directory.
# First empty the "share"data/ folder and recreate the directory structure.
unlink(here("data/"), recursive = TRUE, force = TRUE)
dir.create(here("data"))
tmpZip <- tempfile()
download.file("http://api.worldbank.org/v2/en/indicator/SG.GEN.PARL.ZS?downloadformat=csv",
destfile = tmpZip)
unzip(tmpZip, exdir = here("data"))
WiPDataFile <- list.files(path = here("data"), pattern = "^API")
file.copy(here("data", WiPDataFile), here("data", "WB-WiP.csv"))
# Render the guide and produce the zip file for distribution.
rmarkdown::render(here("doc", "WiP-rdatatable.Rmd"))
# Create an image for the README.md file
tempPNGs <- c(tempfile(), tempfile())
pdf_convert(here("doc", "WiP-rdatatable.pdf"), "png", pages = 1:2,
filenames = tempPNGs)
img1 <- image_read(tempPNGs[1])
img2 <- image_read(tempPNGs[2])
image_append(c(image_border(img1, geometry = "3x3"),
image_border(img2, geometry = "3x3"))) %>%
image_write(.,path=here("images", "WiP-dt-guide.png"), format="png")
# Image for GitHub settings
ghTemp <- tempfile()
test <- pdf_convert(here("doc", "WiP-rdatatable.pdf"), "png", pages = 1, dpi = 155,
filenames = ghTemp)
image_read(ghTemp) %>%
image_crop(geometry_area(1280, 640, 0, 0), repage = FALSE) %>%
image_write(., path=here("images", "WiPdt-GitHub.png"), format="png")
# Extract the R code from Rmd file
knitr::purl(here("doc", "WiP-rdatatable.Rmd"), output=here("R", "WiP-rdatatable.R"))
# Create zip files to share with participants
# First empty the share folder and recreate the directory structure.
unlink(here("Share/"), recursive = TRUE, force = TRUE)
dir.create(here("Share"))
dir.create(here("Share", "data"))
# Populate the Share directories
file.copy(here("doc", "WiP-rdatatable.pdf"), here("Share"))
file.copy(here("doc", "WiP-Exercise.R"), here("Share"))
file.copy(here("data", "WB-WiP.csv"), here("Share", "data"))
# Creating (initialising) an RStudio project
rstudioapi::initializeProject(path = here("Share"))
file.rename(here("Share", "Share.Rproj"), here("Share", "WiP-dt.Rproj"))
# Using here() function with zip results in full paths in the zip files :(
# Not beautiful: Using setwd to overcome the full paths issue above.
setwd(here("Share"))
zip(here("Share", "WiP-rdatatable.zip"), "./", extras = "-FS")
setwd(here())