Skip to content

Commit

Permalink
working on benchmark rmd, trying to setup gh pages
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Jun 20, 2020
1 parent c9bf5c3 commit a3fd51a
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ ubuntu_16_installation.sh
ubuntu_17_installation.sh
^cran-comments\.md$
^codecov\.yml$
^_pkgdown\.yml$
^docs$
^pkgdown$
^\.github$
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
45 changes: 45 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
on:
push:
branches: master

name: pkgdown

jobs:
pkgdown:
runs-on: macOS-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v2

- uses: r-lib/actions/setup-r@master

- uses: r-lib/actions/setup-pandoc@master

- name: Query dependencies
run: |
install.packages('remotes')
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}

- name: Cache R packages
uses: actions/cache@v1
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-

- name: Install dependencies
run: |
install.packages("remotes")
remotes::install_deps(dependencies = TRUE)
remotes::install_dev("pkgdown")
shell: Rscript {0}

- name: Install package
run: R CMD INSTALL .

- name: Deploy package
run: pkgdown::deploy_to_branch(new_process = FALSE)
shell: Rscript {0}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ vignettes/*.md
.Rprofile
# Local files
/local/
docs
135 changes: 135 additions & 0 deletions sandbox/benchmark.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
---
title: "**tmap** benchmark"
author: "Martijn Tennekes"
date: "2020-06-20"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tmap)
library(readxl)
library(grid)
library(dplyr)
library(sf)
library(ggplot2)
library(microbenchmark)
```

## Large choroplth

```{r preprocess-poly,cache=TRUE,include = FALSE}
# function to obtain Food Environment Atlas data (2014)
get_food_envir_data <- function() {
dir <- tempdir()
if (!file.exists(file.path(dir, "DataDownload.xls"))) {
download.file("https://www.ers.usda.gov/webdocs/DataFiles/48731/February2014.xls?v=41688", destfile = file.path(dir, "DataDownload.xls"), mode = "wb")
}
res <- tryCatch({
read_excel(file.path(dir, "DataDownload.xls"), sheet = "HEALTH")
}, error = function(e) {
stop("The excel file cannot be read. Please open it, and remove all sheets except HEALTH. The location of the file is: ", normalizePath(file.path(dir, "DataDownload.xls")))
})
}
# function to obtain US county shape
get_US_county_2010_shape <- function() {
dir <- tempdir()
download.file("http://www2.census.gov/geo/tiger/GENZ2010/gz_2010_us_050_00_20m.zip", destfile = file.path(dir, "gz_2010_us_050_00_20m.zip"))
unzip(file.path(dir, "gz_2010_us_050_00_20m.zip"), exdir = dir)
US <- sf::read_sf(file.path(dir, "gz_2010_us_050_00_20m.shp"))
levels(US$NAME) <- iconv(levels(US$NAME), from = "latin1", to = "utf8")
US
}
# obtain Food Environment Atlas data
FEA <- get_food_envir_data()
# obtain US county shape
US <- get_US_county_2010_shape()
US$FIPS <- paste0(US$STATE, US$COUNTY)
# append data to shape
#US <- append_data(US, FEA, key.shp = "FIPS", key.data = "FIPS", ignore.duplicates = TRUE)
US <- left_join(US, FEA, by = c("FIPS", "FIPS"))
US_cont <- US %>%
subset(!STATE %in% c("02", "15", "72"))
```



```{r}
poly_tmap = function() {
print(tm_shape(US_cont) + tm_polygons("PCT_OBESE_ADULTS10"))
}
poly_sf = function() {
plot(US_cont[, "PCT_OBESE_ADULTS10"])
}
poly_ggplot2 = function() {
print(ggplot(data = US_cont, aes(fill = PCT_OBESE_ADULTS10)) + geom_sf())
}
```


```{r fig.cap="tmap choropleth"}
poly_tmap()
```

**sf**

```{r fig.cap="sf choropleth"}
poly_sf()
```

**ggplot2**

```{r fig.cap="ggplot2 choropleth"}
poly_ggplot2()
```


```{r fig.cap="Benchmark results",echo=FALSE}
write2png = function(e) {
tmp = tempfile(fileext = ".png")
png(tmp, width = 2000, height = 2000)
e
dev.off()
}
autoplot(microbenchmark(list = alist(
tmap = write2png(poly_tmap()),
sf = write2png(poly_sf()),
ggplot2 = write2png(poly_ggplot2())),
times = 3))
```

# Large dot map

```{r preprocess-poly,cache=TRUE,include = FALSE}
denuncia_v = read.csv("https://github.com/hbermeo/datos_espaciales/raw/master/denuncia_vial.csv", encoding = "UTF-8")
denuncias = st_as_sf(denuncia_v, coords = c("long", "lat"), crs = 4326)
```



```{r}
dot_tmap = function() {
print(tm_shape(denuncias) + tm_dots("genero", size = .1))
}
dot_ggplot2 = function() {
ggplot(data= denuncias, aes(color = genero)) + geom_sf()
}
dot_sf = function() {
plot(denuncias[, "genero"])
}
```

0 comments on commit a3fd51a

Please sign in to comment.