Skip to content

Instantly share code, notes, and snippets.

Avatar

Hadley Wickham hadley

View GitHub Profile
View server.R
# server.R
library("shiny")
shinyServer(
function(input, output){
output$displayCounter <- renderText({
c("value of counter:", updateCounter())
})
View vectorisation.R
vectorised_r <- function(x, y) {
exp(-abs(x - y))
}
devectorised_r <- function(x, y) {
r <- rep(NA, length(x))
for(i in seq_along(x)) {
r[i] <- exp(-abs(x[i] - y[i]))
}
r
View lazy.frame.R
# lazy data frame
#
# like idata.frame, but exploits lazy evaluation + macro code generation
# instead of active bindings
library(vadr)
lazy.frame <- function(df, enclos=parent.frame(), ...) UseMethod("lazy.frame")
lazy.frame.lazy.frame <- function(df, ...) df
View latex-math.r
# User facing function
#
# to_math(x[1] + 1^{2 + 4} + 5 + sqrt(y) / 5 %/% 10)
# to_math(paste(x^2, y - 1, z_i))
# to_math(hat(tilde(ring(x))))
# to_math(pi*r^2)
# to_math(unknown_call(x, floor(sqrt(z))))
# to_math(u1(x) + u2(x))
to_math <- function(x) {
x <- substitute(x)
View load-shape-visualise-with-R.md

Load Data With Hmisc

Often times when working with data in R, you find yourself needing to summarise a data.frame by a column, a SELECT SUM(..) or SELECT COUNT(..) in MySQL if you will.

For instance, you may have a lot of page-visits to analyse and you aren't quite certain how to summarise some of that data using R.

Let's consider the following (trivial) dataset: