This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lite.as.feather <- function(path){ | |
data <- read.csv(path) | |
newpath <- paste0(sub(".csv", "", path), ".feather") | |
feather::write_feather(data, newpath) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> # 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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 | |
#' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:')]") %>% |
OlderNewer