Skip to content

Instantly share code, notes, and snippets.

View AdamSpannbauer's full-sized avatar
👀
¯\_(ツ)_/¯

Adam Spannbauer AdamSpannbauer

👀
¯\_(ツ)_/¯
View GitHub Profile
@AdamSpannbauer
AdamSpannbauer / get_twitter_time_dataframe.R
Created March 10, 2017 02:58
Get tweets from a user's timeline
get_timeline_df <- function(user, n_tweets=200, oauth_sig) {
i <- 0
n_left <- n_tweets
timeline_df <- NULL
#loop until n_tweets are all got
while (n_left > 0) {
n_to_get <- min(200, n_left)
i <- i+1
#incorporae max id in get_url (so as not to download same 200 tweets repeatedly)
if (i==1) {
##################################################################
# ADDRESSING https://github.com/AdamSpannbauer/lexRankr/issues/8
##################################################################
# GET EXAMPLE DATA
#----------------------------------------------------------
library(xml2)
library(rvest)
options(stringsAsFactors = FALSE)
##################################################################
# ADDRESSING https://github.com/AdamSpannbauer/lexRankr/issues/8
##################################################################
# Monduiz'S EXAMPLE CODE
##################################################################
library(rvest)
library(tidyverse)
library(stringr)
library(purrr)
library(data.table)
library(gganimate)
library(ggplot2)
#toy example data
dt = data.table(time=1:10, x=round(runif(10, 50, 100), 0))
#number of frames per bar
n_frames_per_bar = 20
#create sequence per time (to imitate the bar growing from ground)
#load needed packages
library(xml2)
library(rvest)
library(lexRankr)
#url to scrape
my_url = "http://www.freepatentsonline.com/y2014/0278285.html"
#read page html
page = xml2::read_html(my_url)
@AdamSpannbauer
AdamSpannbauer / wordcloud2_clickeven_with_reset.R
Created January 12, 2018 19:15
wordcloud2 example of click event and input resetting
#current preferred version of wc2
devtools::install_github("Lchiffon/wordcloud2#32")
#function to give wordcloud2 click interactivity
wc2ClickedWord = function(cloudOutputId, inputId) {
#ARGUMENTS
# - cloudOutputId: string; outputId of wordcloud2 obj being rendered (should be identical to the value passed to wordcloud2Output)
# - inputId: string; inputId of word clicked on (ie you will reference in server the word by input$inputId)
#OUPUT
# - referencing input in server will return a string of form word:freq (same as hover info shown in wordcloud; ie 'super:32')
@AdamSpannbauer
AdamSpannbauer / pytube_download_func.py
Created January 19, 2018 16:48
example pytube function for downloading youtube video
import os
import re
from pytube import YouTube
def downloadYtMp4(ytURL, dlDir=os.getcwd()):
#find youtube video
yt = YouTube(ytURL)
#
#filter to only mp4 files and take last in list (sorted lowest to highest quality)
hqMp4 = yt.filter('mp4')[-1]
@AdamSpannbauer
AdamSpannbauer / query_chunker_function_factory.R
Last active June 11, 2018 13:43
a function factory for creating a generator function for chunking large sql queries (written for use with SQL Server)
create_query_chunker = function(query,
dbi_connection,
post_process_func=NULL,
chunk_size=5000L,
debug_print=FALSE) {
#' @title Create a chunked query fetcher
#'
#' @description Creates a generator function that can be repeatedly
#' called to return the next n rows of query (where n = \code{chunk_size})
#'
@AdamSpannbauer
AdamSpannbauer / query_chunker_R6_class.R
Created June 11, 2018 19:58
an R6 class for chunking large sql queries (written for use with SQL Server). this is a translation of https://gist.github.com/AdamSpannbauer/b04c1f6243ce07a5d2e0c9eb78502a55
library(R6)
QueryChunker = R6Class('QueryChunker',
public = list(
query = NULL,
chunk_query = NULL,
connection = NULL,
post_process_func = NULL,
chunk_size = 1000L,
offset_size = 0,
@AdamSpannbauer
AdamSpannbauer / wordcloud2_url_click.R
Last active June 13, 2018 12:08
wordcloud2 browseURL click event
## install PR of wordcloud2 that adds click events
# devtools::install_github("Lchiffon/wordcloud2#35")
library(shiny)
library(shinyjs)
library(wordcloud2)
# define dummy data to use in example
wordcloud_df = data.frame(word = c('bing', 'google'),
freq = c(1, 2),