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
DT[ | |
id %in% 200:300, | |
.(reg.val = sum(value)), | |
by=region | |
][ | |
reg.val > 0, | |
range(reg.val) | |
] |
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
## Serialization Does Not Trigger Promises | |
fx <- ff(stop('boom')) | |
fxa <- ff(stop('boom')) | |
fy <- ff(stop('boom boom')) | |
identical(serialize(fx, NULL), serialize(fxa, NULL)) | |
## [1] TRUE | |
identical(serialize(fx, NULL), serialize(fy, NULL)) | |
## [1] FALSE |
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(inline) | |
frow <- cfunction(sig=c(x='list'), body=' | |
R_xlen_t J = XLENGTH(x); | |
R_xlen_t n = XLENGTH(VECTOR_ELT(x, 0)); | |
SEXP out = PROTECT(allocVector(INTSXP, n)); | |
int *outp = INTEGER(out); | |
for (int j=0; j<J; j++) { | |
int *col = INTEGER(VECTOR_ELT(x, j)); | |
for (int i=0; i<n; i++) { | |
if(j) {outp[i] += col[i];} else {outp[i] = col[i];} |
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
mk_name <- function(name=NULL) { | |
v <- as.character(packageVersion('rayrender')) | |
dir <- file.path('~', 'Downloads', 'rr-tests') | |
if(is.null(name)) { | |
name <- sprintf( 'a_%s.png', format(Sys.time(), "%Y-%m-%d_%H%M%S")) | |
} else { | |
name <- sprintf('%s.png', name) | |
} | |
file.path(dir, v, name) | |
} |
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
# Because there is no way to tell data.table | |
# "interpret this variable as a column name", it's possible to come up | |
# with corner cases. I'll grant these are unlikely to occur in day | |
# to day use, but any function that uses `data.table` must account for | |
# them | |
# Low odds, and yes, there are workarounds, but this is | |
# what I mean by you have to think carefully to avoid | |
# corner cases |
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
## GPL-2 | |
## Tweet 1 | |
## From https://github.com/djnavarro/ohno | |
add_with <- function(z) function(x, y) x + y + z | |
add_with(10)(1, 2) # works | |
butcher <- function(x, y, f) { | |
env <- new.env() # create a new environment | |
environment(f) <- env |
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
# Compute Device Size To Fully Fill With Plot | |
# | |
# This is not tested extensively and I know very little about messing with | |
# grid. | |
# | |
# GPL-2 | |
# | |
# @param gtable e.g. as produced by `ggplotGrob(ggplot() ...)` | |
# @param din device dimension in inches, ideally use defaults to avoid | |
# calculation issues with npc units. |
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
# started with `foghorn`, but that comes with dependencies and | |
# slows down R startup. `browseURL` just barely slows startup, | |
# even if actual page is slow to load. | |
check_cran_old <- function(email) { | |
utils::browseURL( | |
sprintf( | |
"https://cran.r-project.org/web/checks/check_results_%s.html", | |
gsub("[^A-Za-z0-9_:.-]", "_", sub("@", "_at_", email)) | |
) ) } |
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
# PNG to HTML | |
Code adapted from [@gdagstn](https://gist.github.com/gdagstn/1917e0e6f7dc91a8dde7cb789326c3bb). | |
```{r, echo=FALSE, comment="", results="asis"} | |
old.hooks <- fansi::set_knit_hooks(knitr::knit_hooks) | |
``` | |
```{r echo=FALSE} | |
library(crayon) |
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
## Tests based on https://mailund.dk/posts/scoping-rules-and-nse/ | |
# rm(list=ls()) | |
if(length(ls())) message("you have variables defined in the environment") | |
library(wrapr) | |
lm_prop2a <- function(x, y, d, prop, eval) { | |
let(c(x = x, y = y, prop = prop, d=d), | |
summary(lm(y ~ x, data = d))$prop, | |
eval = eval, envir=parent.frame()) | |
} |
NewerOlder