Skip to content

Instantly share code, notes, and snippets.

Avatar
🎲
RAND()

Mikhail Popov bearloga

🎲
RAND()
View GitHub Profile
@bearloga
bearloga / follower_bios.R
Last active Feb 23, 2016
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.
View follower_bios.R
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 / example.md
Created Mar 16, 2016
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.
View example.md
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 / data.R
Last active Feb 8, 2017
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.
View data.R
## 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 / wikipedia_multilang_subtitles.R
Created May 23, 2016
Visits every Wikipedia in every language and grabs the title + subtitle.
View wikipedia_multilang_subtitles.R
suppressMessages({
# Preamble ========================================
# ======== Web Scraping ===========================
library(rvest) # install.packages('rvest')
library(magrittr)
# ======== I/O ====================================
library(httr) # install.packages('httr')
})
html <- read_html("https://wikipedia.org")
View example.Rmd
---
title: "dyEvent Example"
output:
html_document:
css: style.css
---
```{r setup, echo = FALSE, warning = FALSE, message = FALSE}
library(xts)
library(dygraphs)
@bearloga
bearloga / clickable-imgs.js
Created Sep 28, 2016
Make the figures click-able in your RMarkdown=>HTML reports.
View clickable-imgs.js
<script language="JavaScript">
$(function() {
/* Lets the user click on the images to view them in full resolution. */
$("div.figure img").wrap(function() {
var link = $('<a/>');
link.attr('href', $(this).attr('src'));
link.attr('title', $(this).attr('alt'));
link.attr('target', '_blank');
return link;
});
@bearloga
bearloga / upgrade_packages.R
Last active Oct 8, 2020
The script can be used to re-install packages after upgrading R (on Linux or Mac), since libraries cannot be reused between different minor versions (e.g. when upgrading 3.2.3 to 3.3.2). It detects when a package was installed from CRAN vs GitHub/Git and re-installs it using the appropriate func. Usage: `Rscript upgrade_packages.R`
View upgrade_packages.R
# WMF only:
if (file.exists("/etc/wikimedia-cluster")) {
message('Detected that this script is being run on a WMF machine ("', Sys.info()["nodename"], '"). Setting proxies...')
Sys.setenv("http_proxy" = "http://webproxy.eqiad.wmnet:8080")
Sys.setenv("https_proxy" = "http://webproxy.eqiad.wmnet:8080")
}
# General use:
message("Checking for a personal library...")
if (!dir.exists(Sys.getenv("R_LIBS_USER"))) {
View keybase.md

Keybase proof

I hereby claim:

  • I am bearloga on github.
  • I am mikhailpopov (https://keybase.io/mikhailpopov) on keybase.
  • I have a public key ASCf0zmMPu2vZKS1L64MOIaEEZ2n4ZsLRhW64H1k0sO5TQo

To claim this, I am signing this object:

@bearloga
bearloga / test_2016-12-16_23:48:33.log.md
Last active Dec 20, 2016
Rscript test.R --start_date=2016-12-01 --end_date=2016-12-02 >> test_`date +%F_%T`.log.md 2>&1
View test_2016-12-16_23:48:33.log.md

Test Run

Parameters

start_date end_date omit_times disable_forecasts help
2016-12-01 2016-12-02 FALSE FALSE FALSE

Reports

@bearloga
bearloga / app.R
Created Jan 24, 2017
A Shiny app with the header (title) panel fixed in place at the top as the user scrolls down.
View app.R
header <- headerPanel("Hello Shiny!")
header[[2]]$attribs$id = "header"
ui <- fluidPage(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")
),
header,
sidebarLayout(
sidebarPanel(sliderInput("obs", "Number of observations:", min = 0, max = 1000, value = 500)),