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 / rtweets-revenge.R
Last active April 22, 2023 02:09
Using rtweet v0.7.0 to search Twitter's standard API
#------------------------------------------------------------------------------#
# API KEYS/SECRETS
#------------------------------------------------------------------------------#
# twitter keys - these are fake replace these with your twitter keys found
# at https://developer.twitter.com/en
# be careful with these – do not share or save to cloud. use environment
# variables, follow best practices, don't be careless :)
consumer_key = "IYWxHaPxjpalssdfkj"
consumer_secret = "Cyx8OqcOtniCe63ZmaPxvvq6HvstV6PuS1AjwY9g1awnbaIUp4"
@mkearney
mkearney / lav-clpm-3var.R
Created August 27, 2019 14:33
Example of a three-variable cross lagged panel model in R using {lavaan}
## generate data set
x2 <- rnorm(200)
x1 <- x2 + rnorm(200)
y2 <- rnorm(200) + x2
y1 <- y2 + rnorm(200)
z2 <- rnorm(200) + y2
z1 <- z2 + rnorm(200)
d <- data.frame(x1, x2, y1, y2, z1, z2)
## specify null and full models
#' Conditionally apply expressions on a data object
#'
#' @param .data Input data
#' @param condition A logical value to determine whether to use .if or .else
#' @param .if Formula or function to apply to intput data when condition is TRUE
#' @param .else Formula or function to apply to intput data when condition is FALSE
#' @return Output of appropriate .if/.else call
#' @export
#' @importFrom rlang as_closure
do_if_else <- function(.data, condition, .if, .else = identity) {
@mkearney
mkearney / impeachment-twitter-sentiment-plot.R
Last active October 12, 2020 20:52
Visualizing Sentiment Analysis of Tweets about the 2019 Impeachment Hearings
## install these if not already
pkgs <- c("remotes", "syuzhet", "tfse", "rtweet", "ggplot2", "dplyr", "tidyr")
if (length(pkgs <- pkgs[!pkgs %in% installed.packages("remotes")]) > 0) {
install.packages(pkgs)
}
## install from github
remotes::install_github("mkearney/dataviz")
remotes::install_github("mkearney/dict")
@mkearney
mkearney / nevada-caucus-tweets-2020.R
Created February 22, 2020 22:12
Collect (via Twitter's stream API) tweets from 2020 Nevada Caucus
## load rtweet
library(rtweet)
## store bounding box coordinates for state of nevada
nv <- c(-120.00647, 35.00186, -114.03965, 42.00221)
## initialize output vector and Midwestern 'ope' counter
s <- list()
ope <- 0L
@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 / keynote.R
Last active November 16, 2019 17:40 — forked from dkiesow/keynote.R
R script used with rtweet and Keynote rTweet AppleScript to automate tweets from Mac Keynote
#!/usr/bin/env Rscript
##keynote.R v1.0 Damon Kiesow @dkiesow
##Use with the Keynote rTweet AppleScript app to automate threaded tweeting during Keynote presentations
##
## load rtweet package
library(rtweet)
me <- rtweet:::home_user()
## Pull parameters from command line (first_status will be "yes" or "no" and provided from the AppleScript)
args <- commandArgs(trailingOnly = TRUE)
@mkearney
mkearney / get-congress-follows.R
Created October 31, 2019 21:53
Get all accounts followed by members of the U.S. Congress
## load rtweet and congress116
library(rtweet)
library(congress116)
## create long-version of congress116 data (and drop rows w/o screen names)
sns <- with(congress116, data.frame(
bioguide = c(bioguide, bioguide[!is.na(screen_name_personal)]),
handle = c(screen_name_official, screen_name_personal[!is.na(screen_name_personal)]),
stringsAsFactors = FALSE
))
@mkearney
mkearney / anyconnect.scpt
Created September 12, 2019 21:57 — forked from andrewh/anyconnect.scpt
Applescript to automate the Cisco AnyConnect SSL VPN client on OS X Mavericks
-- 1. Place in ~/Library/Scripts and enable the Applescript menu via the Applescript Editor
-- 2. Substitute "vpn.example.com" and "redacted" for your VPN server and password
-- 3. Open Security & Privacy System Preferences, go to Privacy, Accessibility
-- 4. Enable Applescript Editor and System UI Server
-- 5. Trigger script from the menu
-- 6. Enjoy being connected
tell application "Cisco AnyConnect Secure Mobility Client"
activate
end tell
@mkearney
mkearney / bytes2char.R
Created August 27, 2019 20:34
Convert bytes in string to appropriate char(s)
bytes2char <- function(x) {
m <- gregexpr("<[[:alnum:]]{2}>", x)
y <- regmatches(x, m)[[1]]
y <- gsub("^<|>$", "", y)
y <- strsplit(y, "[><]+")
regmatches(x, m) <- list(sapply(y, function(.x) rawToChar(as.raw(paste0("0x", .x)))))
x
}
## read in string with bytes