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
lite.as.feather <- function(path){
data <- read.csv(path)
newpath <- paste0(sub(".csv", "", path), ".feather")
feather::write_feather(data, newpath)
}
@mkearney
mkearney / save_as_csv.R
Last active August 20, 2016 17:11
rtweet save_as_csv function(s)
## this function is now included in the development version
## of rtweets. install instructions can be found on the
## package readme (or my github rtweet repo)
## Read in the following functions
save_as_csv <- function(x, file_name) {
if (missing(file_name)) {
stop("must provide file_name.", call. = FALSE)
}
@mkearney
mkearney / split_txt.R
Last active September 22, 2016 14:20
> # generate text for txt file
> lipsum <- function() paste(sample(letters, runif(1, 3, 10)), collapse = "")
> lipsum_lines <- function() paste0(paste(replicate(10, lipsum()), collapse = " "), "\n")
>
> # create txt file
> cat(replicate(100, lipsum_lines()), file = "splitxt.txt")
>
> # read n lines from txt file
> spltxt <- readLines("splitxt.txt", n = 50)
> spltxt[1:5]
@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
#' }
@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 / 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 / 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 / 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
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())."
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:')]") %>%