Skip to content

Commit

Permalink
extend get_bunch_o_tweets to apply to all three datasets and ad an n_…
Browse files Browse the repository at this point in the history
…tweets == 'all' option
  • Loading branch information
aedobbyn committed Mar 11, 2019
1 parent 39781c3 commit a38f6ba
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 51 deletions.
26 changes: 13 additions & 13 deletions R/format_submissions.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
#'
#' @export
format_submissions <- function(subs, tips) {
maxid <- max(tips$id, na.rm = T)

maxid <- max(tips$id, na.rm = T)
x <- subs %>%
mutate(
id = seq(maxid + 1, maxid + nrow(subs)),
Tip = `Suggested Tweet`,
Author = `Your Name or Twitter Handle`,
Last.Sent = "",
Category = "Uncategorized"
) %>%
select(id, Tip, Author, Last.Sent, Category)

x <- subs %>%
mutate(id = seq(maxid+1, maxid+nrow(subs)),
Tip = `Suggested Tweet`,
Author = `Your Name or Twitter Handle`,
Last.Sent = "",
Category = "Uncategorized") %>%
select(id, Tip, Author, Last.Sent, Category)

colnames(x) <- NULL

readr::write_csv(x, file("clipboard"))

colnames(x) <- NULL

readr::write_csv(x, file("clipboard"))
}

utils::globalVariables(c("Suggested Tweet", "Your Name or Twitter Handle", "id", "Tip", "Author", "Last.Sent", "Category"))
11 changes: 7 additions & 4 deletions R/get_tweets.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ save_tweet_number <- function(val) {

#' Get Tweets
#'
#' @param n_tweets_to_grab Number of tweets to grab, or "all"
#' @param save_number Whether to save the numbers of the tweets or not
#' @export
get_tweets <- function(save_number = TRUE) {
n_tweets_to_grab <-
get_tweet_number()
get_tweets <- function(save_number = TRUE, n_tweets_to_grab = "all") {
if (n_tweets_to_grab == "all") {
n_tweets_to_grab <-
get_tweet_number()
}

tbl <- rtweet::get_timeline("RLangTip", n = n_tweets_to_grab)

if (save_number) {
if (save_number && n_tweets_to_grab == "all") {
n_tweets <- nrow(tbl)
save_tweet_number(n_tweets)
}
Expand Down
29 changes: 14 additions & 15 deletions R/rtip.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,29 @@ read_tips <- function() {
#' @param cowsay if TRUE, a random cowsay animal will present the tip
#' @param color if TRUE, use a colorful cowsay display. Ignored if cowsay=FALSE
#' @param excluded categories excluded for random tip selection
#' @param keyword character vector keywords contained in tips to search for.
#' @param keyword character vector keywords contained in tips to search for.
#'
#' @return the tip, as a 3-element vector (tip number and category; tip text; author and date)
#' @export
rtip <- function(id, cowsay = TRUE, color = FALSE,
rtip <- function(id, cowsay = TRUE, color = FALSE,
excluded = c("deprecated", "Uncategorized"),
keyword = NULL) {
## Print a random tweet from tips.csv
tips <- read_tips()

if (!is.null(keyword)) {
if (!is.character(keyword)) stop("keyword must be of class character.")
keyword <- keyword %>%
unique() %>%

keyword <- keyword %>%
unique() %>%
tolower()

keyword <- stringr::str_c(keyword, collapse = "|")
tips <- tips %>%

tips <- tips %>%
dplyr::filter(str_detect(tolower(Tip), keyword))
}

N <- NROW(tips)
if (missing(id)) {
candidates <- (1:N)[!(tips$Category %in% excluded)]
Expand All @@ -56,7 +56,7 @@ rtip <- function(id, cowsay = TRUE, color = FALSE,

### Word-wrap the tip to fit the terminal
wrappedTip <- strwrap(tiprow$Tip)

display <- c(
paste0("Tip #", tiprow$id, " in category ", tiprow$Category, sep = ""),
wrappedTip,
Expand All @@ -79,18 +79,17 @@ rtip <- function(id, cowsay = TRUE, color = FALSE,

who <- sample(who_pool, 1)

display_cat <- display %>% paste(collapse = "\n")
display <- display %>% paste(collapse = "\n")

if (color) {
cowsay::say(display_cat, by_color = "rainbow", by = who, type = "string") %>%
cowsay::say(display, by_color = "rainbow", by = who, type = "string") %>%
cat()
} else {
cowsay::say(display_cat, by = who, type = "string") %>%
cowsay::say(display, by = who, type = "string") %>%
cat()
}
} else {
cat(display, sep = "\n")

}

invisible(display)
Expand Down
44 changes: 34 additions & 10 deletions R/run_pipleine.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,44 @@ save_tweets <- function(tbl) {
#' @export
run_tweet_pipeline <- function() {
get_tweets() %>%
join_tips() %>%
save_tweets()
join_tips() %>%
save_tweets()
}


#' Get a bunch of tweets
#'
#' @param n_tweets Number of tweets to choose
#' @param weighted Should tweets with higher scores be chosen more frequently?
#' @param from Which data source should be used. "canonical" for tips CSV, "twitter" for full Twitter data, "joined" for canonical joined on Twitter data and deduped
#' @param n_tweets Number of tweets to choose, or "all"
#' @param weighted Should tweets with higher scores be chosen more frequently? Will be false if from is "canonical"
#' @export
#' @importFrom dplyr sample_n
get_bunch_o_tweets <- function(n_tweets = 5, weighted = TRUE) {
joined <- readr::read_csv(here::here("data-raw", "joined.csv"))

joined %>%
dplyr::sample_n(n_tweets, weight = score)
}
get_bunch_o_tweets <- function(from = "joined", n_tweets = "all", weighted = TRUE) {
stopifnot(from %in% c("canonical", "twitter", "joined"))
stopifnot(is.numeric(n_tweets) || n_tweets == "all")

suppressMessages({
if (from == "twitter") {
tbl <- get_tweets(save_number = FALSE, n_tweets_to_grab = n_tweets) %>%
score_tweets()
} else if (from == "joined") {
tbl <- readr::read_csv(joined_path)
} else if (from == "canonical") {
tbl <- readr::read_csv(tips_path)
}
})

if (n_tweets == "all") {
return(tbl)
}

if (from %in% c("joined", "twitter") && weighted) {
tbl <- tbl %>%
dplyr::sample_n(n_tweets, weight = score)
} else if (from %in% c("joined", "twitter") && !weighted || from == "canonical") {
tbl <- tbl %>%
dplyr::sample_n(n_tweets)
}

tbl
}
3 changes: 2 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
n_tweets_path <- here::here("n_tweets.txt")
tips_path <- here::here("tips.csv")
tips_path <- here::here("inst", "extdata", "tips.csv")
joined_path <- here::here("data-raw", "joined.csv")

utils::globalVariables(c("consumer_key", "consumer_secret", "access_token", "access_secret"))
save_twitter_token <- function() {
Expand Down
9 changes: 6 additions & 3 deletions man/get_bunch_o_tweets.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/get_tweets.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions tests/testthat/test-sample_tweet.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ testthat::test_that("rtip produces string", {

testthat::test_that("rtip produces expected string selected by id", {
tip <- rtip(id = 1, cowsay = FALSE)
expect_equal(tip,
c("Tip #1 in category statistics",
expect_equal(
tip,
c(
"Tip #1 in category statistics",
"Use as.dendrogram() to prepare hierarchical clusters or trees for plotting #rstats http://bit.ly/1sshY0I",
" -- @RStudioJoe, 2010-06-09")
" -- @RStudioJoe, 2010-06-09"
)
)
})

Expand All @@ -22,5 +25,5 @@ testthat::test_that("rtip produces an error if id doesn't exist", {
})

testthat::test_that("rtip is expected length", {
expect_length(rtip(), 3)
expect_length(rtip(), 3)
})

0 comments on commit a38f6ba

Please sign in to comment.