Skip to content

Instantly share code, notes, and snippets.

View yutannihilation's full-sized avatar
🍣
Nobody loves you

Hiroaki Yutani yutannihilation

🍣
Nobody loves you
View GitHub Profile
library(sf)
library(ggplot2)
do_bench <- function(n) {
d <- data.frame(
x = runif(n) * 360,
y = runif(n) * 180
)
d_sf <- st_multipoint(as.matrix(d)) %>%
library(sf)
# load data
nc <- read_sf(system.file("shape/nc.shp", package="sf"))
# get bbox
b <- st_bbox(nc)
# calculate tile number ---------------------------
library(ggplot2)
library(tidyverse)
library(patchwork)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, proj.4 4.9.3

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

nc_12 &lt;- tibble::rownames_to_column(nc[1:2,], "id")
library(ggplot2)
library(dplyr)
call_as_pipable <- function(g, ...) {
# get the current function name as string
fun_name <- deparse(match.call()[[1]])
# remove "add_" and find the correspondent function
fun <- get(gsub("^add_", "", fun_name),
envir = asNamespace("ggplot2"))
g + fun(...)
@yutannihilation
yutannihilation / grouped_summarise_benchmark.R
Created January 22, 2018 08:18
Performance of some variants of group_by() + summarise()
reprex::reprex_info()
#> Created by the reprex package v0.1.1.9000 on 2018-01-22
library(dplyr, warn.conflicts = FALSE)
data("flights", package = "nycflights13")
microbenchmark::microbenchmark(
summarise_only = flights %>%
group_by(year, month, day, origin) %>%
summarise(dep_delay_avg = mean(dep_delay),
# w/ purrrlyr
df.jp.prefs.hex %>%
purrrlyr::slice_rows("id") %>%
purrrlyr::by_slice(make_hex) %>% magrittr::use_series(.out) %>%
sf::st_sfc()
# w/ purrr
df.jp.prefs.hex %>%
split(.$id) %>%
purrr::map(make_hex) %>%
library(DBI)
observer <- getOption("connectionObserver")
observer$connectionOpened(type = "HOXOM",
displayName = "HOXOM",
host = "",
connectCode = "",
disconnect = function(...) NULL,
listObjectTypes = function(...) list(schema = list()),
listObjects = function(...) list(),
reprex::reprex_info()
#> Created by the reprex package v0.1.1.9000 on 2017-10-16
library(dplyr, warn.conflicts = FALSE)
replace_cols1 <- function(df, cols, pos) {
cbind(
df[, 1:(pos-1), drop=FALSE],
cols,
df[, (pos+1):ncol(df), drop=FALSE]
;; add load-paths
(add-to-list 'load-path "~/.emacs.d/elisp")
(require 'package)
;; MELPA
(add-to-list
'package-archives
'("melpa" . "http://melpa.org/packages/") t)
f <- function(return_true = TRUE) {
function(return_true = return_true) {
if (return_true) return(TRUE)
FALSE
}
}
f()
#> function(return_true = return_true) {
#> if (return_true) return(TRUE)
#> FALSE