Skip to content

Instantly share code, notes, and snippets.

View mkearney's full-sized avatar
📈
Data Sciencing

Michael W. Kearney mkearney

📈
Data Sciencing
View GitHub Profile
@mkearney
mkearney / get-gh-stars.R
Last active February 22, 2019 09:16
Get a tidy data frame of information about all of your Github stars (repos you've starred)
## install {remotes} pkg
if (!requireNamespace("remotes", quietly = TRUE)) {
install.packages("remotes")
}
## install {tfse} from github
remotes::install_github("mkearney/tfse")
## load {tfse}
library(tfse)

Keybase proof

I hereby claim:

  • I am mkearney on github.
  • I am kearneymw (https://keybase.io/kearneymw) on keybase.
  • I have a public key ASAprxnmWoBc1OCGFVW6Lid1F4IXNx6uabYjq-hOiDx_hQo

To claim this, I am signing this object:

library(V8)
library(rvest)
library(hrbrthemes)
library(tidyverse)
ctx <- v8()
pg <- read_html("https://www.tiobe.com/tiobe-index/")
html_nodes(pg, xpath=".//script[contains(., 'series:')]") %>%
jlfacts <- function(dir = path.expand("~")) {
if (file.exists(file.path(dir, ".jeffleekfacts.rds"))) {
jlrtfacts <- readRDS(file.path(dir, ".jeffleekfacts.rds"))
} else if ("rtweet" %in% install.packages()) {
jlrt <- rtweet::search_tweets("\"jeff leek\"", n = 500, include_rts = FALSE)
jlrtfacts <- trimws(unique(gsub("@[[:alnum:]_:]+", "", jlrt$text)))
jlrtfacts <- grep("http", jlrtfacts, invert = TRUE, value = TRUE)
saveRDS(jlrtfacts, path.expand(file.path("~", ".jeffleekfacts.rds")))
} else {
jlrtfacts <- "jeff leek doesn't use soap; he uses rm(list = ls())."
@mkearney
mkearney / rRMD.r
Created April 13, 2017 02:09
Converts Rmd (rmarkdown) files into tidy R script files.
#' R script from Rmarkdown file
#'
#' Converts Rmarkdown to R script file
#'
#' @param x File name of Rmd file
#' @param verbose Logical indicating whether to print (cat) the
#' script code. Defaults to TRUE.
#' @return String with script syntax.
rRMD <- function(x, verbose = TRUE) {
## read Rmd file
@mkearney
mkearney / search_tweets-ex-txlege.R
Last active November 16, 2019 17:41
Search tweets example using #txlege tweets
library(rtweet)
## number of iterations
n <- 5
## to do a certain task (e.g., searching for tweets on a topic),
## twitter policy says you should use one token
## so select one token
token <- get_tokens()
if (!"Token" %in% class(token)) {
@mkearney
mkearney / ts_plot-ex.hacking-stream.R
Last active January 7, 2017 04:52
Example of ts_plot() and stream_tweets() from rtweet pkg
library(rtweet)
## use stream_tweets() to capture an hour of tweets
"wikileaks,assange,hacking,russia,putin" %>%
stream_tweets(
parse = FALSE,
timeout = (60 * 60),
file_name = "wikileaks.json")
## read and parse data
rt <- parse_stream("wikileaks.json")
@mkearney
mkearney / ts_plot-ex.wikileaks.R
Created January 7, 2017 04:27
Example of ts_plot() from rtweet pkg
## download dev version of rtweet (from github)
install.packages("devtools")
devtools::install_github("mkearney/rtweet")
library(rtweet)
## search for 15k tweets using keywords
## (my goal was to explore current political opinions
## so it made sense to me to exclude retweets and
## use personal/opinion keywords, e.g., "i think")
rt <- search_tweets(
@mkearney
mkearney / as.df.R
Last active December 15, 2016 05:50
Easy as df
dots <- function(...) {
as.character(eval(substitute(alist(...))))
}
unL <- function(x) unlist(x, use.names = FALSE)
## easy as df
as.df <- function(x, ...) {
vars <- dots(...)
if (identical(vars, "FALSE")) {
as.data.frame(x, stringsAsFactors = FALSE)
@mkearney
mkearney / xiny.R
Last active October 20, 2016 16:32
#' xiny
#'
#' Returns logical value indicating whether named
#' object includes var name. Functions do the following:
#'
#' \itemize{
#' \item \code{\%xy\%} returns logical for each value of x
#' \item \code{\%any\%} returns TRUE if y contains any of x
#' \item \code{\%all\%} returns TRUE if y contains all of x
#' }