Skip to content

Instantly share code, notes, and snippets.

View bearloga's full-sized avatar
🎲
RAND()

Mikhail Popov bearloga

🎲
RAND()
View GitHub Profile
@bearloga
bearloga / wikipedia_multilang_subtitles.R
Created May 23, 2016 21:25
Visits every Wikipedia in every language and grabs the title + subtitle.
suppressMessages({
# Preamble ========================================
# ======== Web Scraping ===========================
library(rvest) # install.packages('rvest')
library(magrittr)
# ======== I/O ====================================
library(httr) # install.packages('httr')
})
html <- read_html("https://wikipedia.org")
@bearloga
bearloga / data.R
Last active February 8, 2017 16:43
Scripts for scraping divorce demographics by country from Wikipedia and plotting it in R with ggplot2 with the respective country flags in place of points.
## Script for scraping Wikipedia for data to use with the geom_flag() prototype
## CONTACT: Mikhail Popov (@bearloga // mikhail[at]mpopov[dot]com)
## URL: https://gist.github.com/bearloga/519a701a6a9bc7c3ba9f
# install.packages("import")
library(rvest) # install.packages("rvest")
library(magrittr)
import::from(dplyr, mutate, select, keep_where = filter, left_join, distinct)
@bearloga
bearloga / example.md
Created March 16, 2016 23:24
Augments dplyr::top_n() to also return a summary of the remainder of the dataset that didn't make the cut. Useful when working with proportions.
library(magrittr)
library(dplyr)
library(stringr)

df <- mtcars; df$car <- rownames(df)

df %>%
  mutate(make = str_extract(car, "^[A-Za-z]+\\b")) %>%
 group_by(make) %&gt;%
@bearloga
bearloga / follower_bios.R
Last active February 23, 2016 21:19
Scrapes some basic info from my Twitter followers' bios. I used this to get an approximate lower bound on how many data science-y folks follow me.
your_handle <- "bearloga"
# Note: must be logged in to twitter to view your own or anyone's list of followers
library(magrittr) # install.packages('magrittr')
library(rvest) # install.packages('rvest')
library(RSelenium) # install.packages('RSelenium')
# Scrolling solution by NicE (http://stackoverflow.com/a/29965233/1091835):
checkForServer()
startServer()
@bearloga
bearloga / Interactive_Presentations.Rmd
Created December 4, 2015 19:32
This allows the presenter to write and execute R code within the presentation without going back and forth between the presentation and the R console.
---
title: "Interactive Presentations"
author: "Mikhail Popov (@bearloga)"
date: "December 4, 2015"
output: ioslides_presentation
runtime: shiny
---
## Reduce Demo
@bearloga
bearloga / classify.R
Created September 10, 2015 19:27
A function which launches a Shiny app for hand coding (manually classifying) data.
#' Manual classification of observations
#'
#' \code{classify} launches a Shiny app to manually classify a subset of observations.
#'
#' @param x A character vector.
#' @param btn_labels A character vector of length 2 corresponding to 0 and 1.
#' @return A vector of 0/1 for each element in \code{x}.
#' @export
#' @examples \dontrun{
#' foo <- sprintf('%s (%.2f miles per gallon)', rownames(mtcars), mtcars$mpg)
@bearloga
bearloga / Swimming.R
Created June 5, 2015 18:16
To answer the question "Does time saved by swimming better make up for time spent panickedly taking off pants?" (https://twitter.com/R0mination/status/606874583283597312)
d <- 20
time_pants <- 15 # seconds
# Average human swimming speed: 2 miles per hour
speed_avg <- birk::conv_unit(2, "mph", "m_per_sec") # install.packages('birk')
perc <- seq(0.25, 0.95, 0.01)
speed_slow <- speed_avg * perc
plot(perc * 100, (d / speed_slow)/60, type = "l", las = 1, lwd = 2,
xlab = "Speed in pants as % of average speed (2 mph ~ 0.89 m/s). In other words, how much the pants slow you down.",
@bearloga
bearloga / magic_8ball.R
Last active August 29, 2015 14:20
For Oliver Keyes (@Quominus) who tweeted that he "would like a magic 8-ball only instead of the future it tells [him] if [he's] being insecure and/or nuts."
library(shape) # install.packages('shape')
magic_8ball <- function(x = c("yes","no")) {
greys <- greycol(50, interval = c(0, 1))
colours <- shadepalette("#000079", "blue", n = 20)
emptyplot(xlim = c(-5, 5), ylim = c(-5, 5), col = "white", frame.plot = FALSE)
filledellipse(rx1 = 4.9, ry1 = 4.9, col = "black")
filledellipse(rx1 = 2, ry1 = 3.5, col = greys[2:50], angle = -45, dr = .1, mid = c(-2, 2))
filledellipse(rx1 = 0.25, ry1 = 0.25, col = "white", mid = c(-2, 2))
filledellipse(rx1 = 2.8, ry1 = 2.8, col = "white")
@bearloga
bearloga / licenses.R
Last active August 29, 2015 14:19
Obtaining licensing metadata for task-specific R packages on CRAN (…for the purpose of, say, avoiding GPL-licensed packages)
# devtools::install_github("RcppCore/Rcpp")
# devtools::install_github("hadley/xml2")
# devtools::install_github("hadley/rvest")
# devtools::install_github("metacran/crandb")
# devtools::install_github('ironholds/practice')
# install.packages('dplyr')
library(rvest); library(crandb)
get_licensing_for_task <- function(task_url) {
@bearloga
bearloga / Embed Plot in HTML.R
Created February 6, 2015 20:33
Sometimes you just want to send a report to someone as a single HTML file (e.g. when PDF isn't an option) and you want to avoid the hassle of external images. This code lets you convert a plot in R into base64 string that you can include in the HTML source.
library(base64) # install.packages('base64')
pngfile <- tempfile()
png(pngfile,width=600,height=600,pointsize=12) # adjust as needed
# Plotting code...
dev.off()
# Prints the HTML code to console
cat(img(pngfile))
# Copy and paste the whole output into your HTML source to embed the image