Skip to content

Instantly share code, notes, and snippets.

View hadley's full-sized avatar

Hadley Wickham hadley

View GitHub Profile

Using Google's generative APIs: lessons learned

Basics

  • The API key wasn't in the shell environment. It lives in ~/.Renviron, so the scripts fall back to reading GOOGLE_API_KEY from there.
  • Find model IDs by listing them, not by guessing marketing names: GET https://generativelanguage.googleapis.com/v1beta/models?pageSize=200 (the displayName field maps the two). The ones used:

May 2019

Statistical language R falls out of the TIOBE index top 20

After having been in the top 20 for about 3 years, statistical language R dropped out this month.

This is quite surprising because the field of statistical programming is still booming, especially thanks to the popularity of data mining and artificial intelligence.

Snapshot

@hadley
hadley / whimsy-quiz-scoring.md
Created September 14, 2026 18:28
Reverse engineering the BuzzFeed 'What Percent Whimsy Are You?' scoring algorithm

Reverse engineering "What Percent Whimsy Are You?"

Quiz: https://www.buzzfeed.com/laughingbunny255/what-percent-whimsy-are-you

The scoring is fully recoverable — the quiz data is embedded as JSON in the page (data.type = "personality", 9 questions, 4 result buckets).

The algorithm

Each question has exactly 4 answers, and each answer is worth points: 1 toward

LLM for code generation

Currently three main styles:

  1. Autocomplete
    • e.g. github copilot, windsurf
    • Inline "ghost text" as you type
    • Sometimes amazingly good; often pretty useless
    • Need to train yourself to ignore spurious suggestions

Convert <ingredients> to JSON using the following format:

  • If an ingredient has both weight and volume, extract only the weight:

    ¾ cup (150g) dark brown sugar
    [{"name": "dark brown sugar", "quantity": 150, "unit": "g"}]
    
  • If an ingredient only lists a volume, extract that.

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;%