Skip to content

Instantly share code, notes, and snippets.

View hadley's full-sized avatar

Hadley Wickham hadley

View GitHub Profile
@hadley
hadley / s3.r
Created May 7, 2013 13:16
Implementation of request signing for Amazon's S3 in R.
library(httr)
library(digest)
library(XML)
s3_request <- function(verb, bucket, path = "/", query = NULL,
content = NULL, date = NULL) {
list(
verb = verb,
bucket = bucket,
path = path,
library("shiny")
x <- runApp(shinyApp(
fluidPage(
"Password:",
tags$input(id = "password", type = "password"),
actionButton("done", "Done")
),
function(input, output) {
observe({
@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(tidyverse)

simple_data <- tibble(
  group = factor(rep(c("A", "B"), each = 15)),
  subject = 1:30,
  score = c(rnorm(15, 40, 20), rnorm(15, 60, 10))
)

simple_data_se &lt;- simple_data %&gt;% 
library(tidyverse)
library(gapminder)
gapminder
gapminder <- gapminder %>% mutate(year1950 = year - 1950)
# Nested data -------------------------------------------------------------
by_country <- gapminder %>%
group_by(continent, country) %>%
# need + validate -----------------------------------------------------------
validate(
need(
mzfinder::check_mzr_object(ms_object$mzr_connection),
"Wasn't able to connect to MS file"
)
)
validate(
need(!is.null(input$ppm_input), "ppm_input must not be null"),
need(input$ppm_input > 0L, "ppm_input must be > 0")
# =======================================
# = Enhancements to data tidying =
# = Hadley Wickham =
# = https://rstd.io/tidyhancements-2019 =
# =======================================
# What is tidy data? ----------------------------------------------------------
# 1. Each column is a variable.
# 2. Each row is an observation.
# 3. Each cell is a value.
library(dplyr, warn.conflicts = FALSE)
library(gapminder)
probs <- c(0.1, 0.5, 0.9)
gapminder %>%
group_by(continent) %>%
summarise(
probs = probs,
across(is.numeric & !year, ~ quantile(.x, probs))
)
library(tidyverse)
library(gh)
library(lubridate)
library(glue)
repo <- tibble(json = unclass(gh("/user/repos", .limit = Inf)))
repo <- repo %>%
hoist(json,
owner = c("owner", "login"),
@hadley
hadley / sql.r
Created October 24, 2013 20:31
Some useful functions for generating SQL
#' @include DBConnection.R
NULL
#' Generate SQL: CREATE TABLE
#'
#' @param conn A subclass of \code{\linkS4class{DBIConnection}} describing
#' an active database connection.
#' @param name A string giving the table name to create. Escaped with
#' \code{\link{dbQuoteIdentifier}}
#' @param fields,types Character vectors of equal lengths giving the names