Inspired by https://adv-r.hadley.nz/function-operators.html#behavioural-fos
time_it <- function(f) {
force(f)
function(...) {
microbenchmark::microbenchmark(f(...), times = 10)
}
}
Inspired by https://adv-r.hadley.nz/function-operators.html#behavioural-fos
time_it <- function(f) {
force(f)
function(...) {
microbenchmark::microbenchmark(f(...), times = 10)
}
}
r4ds p. 85
[Use force when] creating closures with
lapply()
or a loop:
add <- function(x) {
force(x)
function(y) x + Y
}
lapply(1:10, add)
Note that we call the first argument .x: whenever you use ... to take arbitrary data, it’s good practice to give the other argument names a . prefix. This eliminates any ambiguity about who owns the argument, and in this case makes it possible to set the x attribute.
From Jim Hester r-lib/devtools#1279
library(git2r)
r <- repository("C:/Users/LeporeM/Dropbox/git_repos/allodb")
c <- commits(r)
authors <- lapply(c, `slot`, "author")
names <- vapply(authors, `slot`, character(1), "name")
Site name,Alternate site name/ abbreviation,State/ Province,Country,Biogeographic zone,GCB#,Latitude,Longitude,Elevation-min,Elevation-max,Topographic relief,Location & Elevation notes,n geographically distinct plots,Area,Plot Dimensions,Plot Area & Dimensions notes,First ForestGEO census year,First census year,Year added to network,Publication(s) describing site/plot,Affiliated web links,Number of Species,Number of Trees,Species & Number of trees notes,Forest Description ,FAO ecozone,Dominant vegetation type(s),Dominant Soil order(s),Soil Notes,Koppen Climate zone,MAT,MAP,Natural disturbance regime,Natural disturbance notes,Past anthropogenic disturbances,Current anthropogenic disturbances,Anthropogenic disturbance notes,PI1,PI2,PI3,PI4 | |
Amacayacu,,Amazonas,Colombia,Neotropic,42,-3.8091,-70.2678,,,,,1,25,500 x 500,,2006,,2006,"Arias Garcia et al., 2009",,1133,123790,,,Tropical rainforest,BE,Ult,Soil classification done by Turner: mainly Paleudults; underlying geology is the Pebas formation,Af,25.8,3215,Fl,inf |
When should I force arguments passed into a closure from a function factory? Always? Sometimes? I what cases?
E.g. fgeo.base::keep_drop()
.
temps <- seq(-10, 50, 10)
for (i in seq_along(temps)) {
if (temps[[i]] <= 0) {
print("freezing")
} else if (temps[[i]] <= 10) {
print("cold")
} else if (temps[[i]] <= 20) {
Based on code by Noam Ross (@noamross), developed during runconf18 (https://github.com/ropensci/unconf18)
#' Review dependency usage: Count used functions from external packages.
#'
#' @inheritParams functionMap::map_r_package
#'
#' @return A dataframe.
#' @export
#'