Skip to content

Instantly share code, notes, and snippets.

View hadley's full-sized avatar

Hadley Wickham hadley

View GitHub Profile
library(tidyverse)
# https://twitter.com/buddyherms/status/1576966150680121344 --------------
# PROs: at, by, and regexp examples
# CONs: quite simple
vt_census <- tidycensus::get_decennial(
geography = "block",
state = "VT",
@hadley
hadley / loop.R
Last active November 10, 2022 20:11
f1 <- function(n) {
x <- numeric()
for (i in 1:n) {
x <- c(x, i)
}
x
}
f1.5 <- function(n) {
x <- numeric()
data(diamonds, package = "ggplot2")
# Most straightforward
diamonds$ppc <- diamonds$price / diamonds$carat
# Avoid repeating diamonds
diamonds$ppc <- with(diamonds, price / carat)
# The inspiration for dplyr's mutate
diamonds <- transform(diamonds, ppc = price / carat)
@hadley
hadley / rstats-spam.R
Last active September 26, 2022 12:43
library(rtweet)
library(tidyverse)
auth_setup_default()
json <- search_tweets("#rstats", n = 5000, include_rts = FALSE, parse = FALSE)[1:5]
tweets <- tibble(json = json) %>%
unnest_wider(json) %>%
select(statuses) %>%
unnest_longer(statuses) %>%
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(ggplot2)
x <- c("بقرة", "دجاج", "حصان")
df <- data.frame(x = x, y = 1:3)
labels_rtl <- function(x) paste0("\u202B", x)
ggplot(df, aes(x, y)) +
geom_point() +
scale_x_discrete(labels = labels_rtl) +
# 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")
# Code for quick exploration of
# https://github.com/rfordatascience/tidytuesday/tree/master/data/2020/2020-05-26
# Video at https://youtu.be/kHFmtKCI_F4
library(tidyverse)
cocktails <- readr::read_csv("boston_cocktails.csv")
# Are name and row_id equivalent? -----------------------------------------
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))
)
#!/bin/bash
#
# Installs the latest RStudio daily desktop build for OSX/macOS and Ubuntu(amd64)
#
# https://support.rstudio.com/hc/en-us/articles/203842428-Getting-the-newest-RStudio-builds
set -e
install_macos_daily() {
REDIRECT_URL="https://www.rstudio.org/download/latest/daily/desktop/mac/RStudio-latest.dmg"
echo "Discovering daily build from: ${REDIRECT_URL}"
# Perform a HEAD request to find the redirect target. We use the name of the