This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install_from_source <- function (pkg, args = NULL, repos = "http://cran.rstudio.com", overwrite = FALSE, ...) { | |
if (!overwrite) { | |
if (pkg %in% installed.packages()) { | |
message(pkg, " is already installed") | |
return() | |
} | |
} | |
install.packages( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PY_CCl4_emission_data <- | |
PY(1997:2016) %>% | |
point_source_abated_emissions() %>% | |
filter_pollutants(list(CCl4 = 60)) | |
chart_data <- | |
PY_CCl4_emission_data %>% | |
mutate(fac_h1 = if_else(fac_id == 31, "Dow Chemical", "All Others")) %>% | |
annual_emissions_by(fac_h1) %>% | |
group_by(fac_h1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(UScensus2010) | |
library(UScensus2010tract) | |
library(UScensus2010blkgrp) | |
library(UScensus2010blk) | |
Cook_tracts <- UScensus2010::county(state = "Illinois", name = "Cook", level = "tract") | |
Cook_tracts@data[1:8, 1:8] | |
plot(Cook_tracts) | |
Cook_blkgrps <- UScensus2010::county(state = "Illinois", name = "Cook", level = "blkgrp") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(ggplot2) | |
require(proto) | |
StatRollApplyR <- proto(ggplot2:::Stat, { | |
required_aes <- c("x", "y") | |
default_geom <- function(.) GeomLine | |
objname <- "rollapplyr" | |
calculate_groups <- function(., data, scales, ...) { | |
.super$calculate_groups(., data, scales, ...) | |
} | |
calculate <- function(., data, scales, width, FUN, fill=NA, ...) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
merge.SpatialPolygonsDataFrame <- function (x, y, by, ...) { | |
require(sp) | |
y <- as.data.frame(y) | |
if (missing(by)) { | |
if (is.null(row.names(x@data)) || is.null(row.names(y))) { | |
warning("[merge.SpatialPolygonsDataFrame] merging by position") | |
i <- 1:nrow(x@data) | |
} else { | |
warning("[merge.SpatialPolygonsDataFrame] merging by row names") | |
i <- row.names(x@data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(htmlwidgets) | |
library(rpivotTable) | |
mtcars %>% | |
rpivotTable(rows = "gear", | |
cols = c("cyl", "carb"), | |
vals = "hp", | |
# Step 1. Make this line accomplish the same thing as the default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grouply <- function(f, ...) { | |
groups <- lazyeval::lazy_dots(...) | |
function(tbl, ...) { | |
dplyr::group_by_(tbl, .dots = groups) %>% | |
f(...) %>% | |
dplyr::ungroup() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $ brew install libssh2 | |
# $ brew install libgit2 | |
## $ export CMAKE_INCLUDE_PATH="/usr/local/Cellar/libssh2/1.7.0/include" | |
## $ export CMAKE_LIBRARY_PATH="/usr/local/Cellar/libssh2/1.7.0/lib" | |
# NOTE: re: git2r: https://github.com/ropensci/git2r/issues/236 | |
install_CRAN <- function (pkg, repos = "http://cran.rstudio.com", verbose = TRUE, ...) { | |
if (pkg %in% installed.packages()) message(pkg, " is already installed") | |
else install.packages(pkg, repos = repos, verbose = verbose, ...) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Context: | |
# | |
# https://cran.r-project.org/web/packages/ensurer/vignettes/ensurer.html says: | |
# "It is also possible to use the dot, ., directly in anonymous error handlers defined directly in the call to e.g. ensure_that." | |
# | |
# I must be misunderstanding the statement above! | |
# | |
library(ensurer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ensurer) # awesome | |
# Example 1 --- OK | |
groceries <- c("eggs", "bread", "milk") | |
check_that(groceries, "bread" %in% .) | |
# Example 2 --- :-( | |
# I'd really like this to work! It's quite readable. | |
# Doesn't work b/c `ensurer` (v1.1) doesn't first evaluate calls. | |
contains <- function (what) function (x) { what %in% x } |