Skip to content

Instantly share code, notes, and snippets.

View hadley's full-sized avatar

Hadley Wickham hadley

View GitHub Profile
library(class)
library(mass)
library(mva)
tst <- read.table("e:/uni/stats766/puktest.txt", header = TRUE)
tst.v <- tst[,1:7]
tst.g <- tst[,8]
trn <- read.table("e:/uni/stats766/puktrain.txt", header = TRUE)
trn.v <- trn[,1:7]
trn.g <- trn[,8]
@hadley
hadley / .Renviron
Last active October 22, 2020 16:44
R_COMPLETION=FALSE
R_LIBS=~/R
TMPDIR=/tmp
_R_CHECK_FORCE_SUGGESTS_=false
GITHUB_PAT=[redacted]
DO_PAT=[redacted]
library("shiny")
x <- runApp(shinyApp(
fluidPage(
"Password:",
tags$input(id = "password", type = "password"),
actionButton("done", "Done")
),
function(input, output) {
observe({
@hadley
hadley / rxive.md
Last active August 29, 2015 14:11
  • Only accepted if R CMD CHECK passes with no WARNINGS or ERRORs
  • Long-term archive, and difficult to remove
  • Provides registry of R packages
  • Zenodo DOIs
library(stringi)
locales <- stri_locale_list()
main <- locales[!stri_detect_fixed(locales, "_")]
main_col <- lapply(main, stri_opts_collator)
sorted <- lapply(main_col, function(x) stri_sort(letters, opts_collator = x))
names(sorted) <- main
noquote(simplify2array(Filter(function(x) !identical(x, letters), sorted)))
@hadley
hadley / date-parser.R
Last active August 29, 2015 14:10
Go style date formats for R
library("stringi")
ref <- ISOdatetime(2006, 01, 02, 15, 4, 0, "MST")
formats <- c("%H", "%Y", "%y", "%b", "%B", "%m", "%d", "%e", "%a",
"%A", "%I", "%l", "%p", "%Z", "%z", "%M", "%S")
# Must manually respecify TZ because strftime doesn't use the time zone
# stored in ref
conv <- vapply(formats, function(x) strftime(ref, x, tz = "MST"), character(1))
recode <- function(df, ..., match = c("first", "last")) {
match <- match.arg(match)
cases <- lapply(list(...), as.case)
if (identical(match, "last")) cases <- rev(cases)
n <- nrow(df)
out <- rep(NA, length(n)) # logical will be upcast as needed
# Simple loop-y implementation
`[.my_df` <- function (x, i, j) {
if (missing(i) && missing(j)) return(x)
# First, subset columns
if (!missing(j)) {
x <- .subset(x, j)
}
# Next, subset rows
if (!missing(i)) {
iclass <- function(x) {
c(
if (is.matrix(x)) "matrix",
if (is.array(x) && !is.matrix(x)) "array",
if (is.double(x)) "double",
if (is.integer(x)) "integer",
mode(x)
)
}
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
std::string escape_one(std::string x) {
int n = x.size();
std::string out = "\"";
out.reserve(n + 2);
for (int i = 0; i < n; ++i) {