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
# R only has one package namespace | |
# All packages exist in a single, non-hierarchical namespace | |
# Once a package name is used, it cannot be reused | |
# CRAN hosts most existing R packages; Bioconductor holds many more; once claimed on either service, a package name is reserved | |
# GitHub hosts many other packages with no "claim" to a given package name and there's no list of such packages | |
# | |
# "Writing R Extensions" says this about names: | |
## "The mandatory ‘Package’ field gives the name of the package. | |
## This should contain only (ASCII) letters, numbers and dot, have | |
## at least two characters and start with a letter and not end in |
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("httr") | |
library("XML") | |
x <- content(httr::GET("https://cran.r-project.org/web/packages/available_packages_by_date.html"), as = "text") | |
p <- readHTMLTable(x, stringsAsFactors = FALSE)[[1]] | |
names(p) <- trimws(names(p)) | |
p$Days <- Sys.Date() - as.Date(p$Date) | |
h <- hist(-as.numeric(p$Days), breaks = seq(from = -(365*11), to = 0, by = 365/4), plot = FALSE) | |
barplot(h$counts, space = 0, las = 1, |
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
# packages | |
library("dplyr") | |
library("reshape") | |
library("microbenchmark") | |
set.seed(1) | |
# prep data | |
m <- mtcars | |
n <- 50L # number of data.frames |
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("shiny") | |
library("shinyjs") | |
ui <- shinyUI(pageWithSidebar( | |
titlePanel("", "3D Perspective Plot for Interaction Effects"), | |
sidebarPanel( | |
tabsetPanel( | |
tabPanel("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
# recoding is basically a function | |
# x is a variable | |
# f() is a function | |
# f(x) is a recoding operation | |
# | |
# implementation | |
# if x is discrete, then f() is simple to implement as a map between input values and output values: | |
# 1 = 0 | |
# 2 = 1 | |
# |
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
# relevant to recoding: https://gist.github.com/leeper/c669fd4a6dcb9ac23612 | |
# assertion function | |
assert <- function(x, length, class, min, max, has, excludes, of, miss) { | |
if (!missing(length)) { | |
stopifnot(length(x) == length) | |
} | |
if (!missing(class)) { | |
stopifnot(inherits(x, as.character(substitute(class)))) | |
} |
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
charsub <- function(x, i, value) { | |
out <- lapply(strsplit(x, ""), function(z) { | |
z <- `[<-`(z, i, value) | |
z[is.na(z)] <- " " | |
paste0(z, collapse = "") | |
}) | |
unlist(out) | |
} | |
# SINGLE STRING |
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
set.seed(1) | |
n <- 300 | |
x <- rbinom(n, 1, .5) | |
y <- 4 + (2 * x) + rnorm(n, 0, 3) | |
m <- tapply(y, x, mean) | |
wm <- weighted.mean(c(mean(y[x==1]), mean(y[x==0])), c(length(y[x==1]), length(y[x==0]))) | |
ci <- function(formula, conf.level, var.equal = FALSE) { | |
(tt <- t.test(formula, var.equal = var.equal, conf.level = conf.level)) | |
rect(0, wm - diff(tt$conf.int)/2, |
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
# Check URLs in a document | |
## This code will extract URLs from a text document using regex, | |
## then execute an HTTP HEAD request on each and report whether | |
## the request failed, whether a redirect occurred, etc. It might | |
## be useful for cleaning up linkrot. | |
if (!require("httr")) { | |
install.packages("httr", repos = "http://cran.rstudio.com/") | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
</head> | |
<body> | |
<div> | |
<form> | |
<div> | |
<p>Mouse: <input type="text" id="xmove" /> | |
<input type="text" id="ymove" /> |