Skip to content
This repository has been archived by the owner on Nov 10, 2024. It is now read-only.

Commit

Permalink
Drop magrittr
Browse files Browse the repository at this point in the history
Fixes #522
  • Loading branch information
llrs authored and hadley committed Mar 8, 2021
1 parent 031910b commit 18375af
Show file tree
Hide file tree
Showing 48 changed files with 291 additions and 126 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Imports:
httr (>= 1.3.0),
jsonlite (>= 0.9.22),
lifecycle (>= 1.0.0),
magrittr (>= 1.5),
progress,
rappdirs,
tibble (>= 1.3.4),
Expand Down
3 changes: 1 addition & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ S3method(print,screen_name)
S3method(print,user_id)
S3method(round_time,Date)
S3method(round_time,POSIXt)
export("%>%")
export(as_screenname)
export(as_userid)
export(bearer_token)
Expand Down Expand Up @@ -60,6 +59,7 @@ export(network_graph)
export(next_cursor)
export(parse_stream)
export(plain_tweets)
export(post_destroy)
export(post_favorite)
export(post_follow)
export(post_friendship)
Expand Down Expand Up @@ -96,4 +96,3 @@ export(users_data)
export(users_with_tweets)
export(write_as_csv)
import(rlang)
importFrom(magrittr,"%>%")
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# rtweet ...

- rtweet no longer re-exports the pipe; if you want to continue using it, you'll
need to `library(magrittr)` or `library(dplyr)` (#522).
- `stream_tweets()` has been overhauled to only write valid data. This obsoletes
all previous strategy to cleen up bad data after the fact (#350, #356).
Expand Down
2 changes: 1 addition & 1 deletion R/as_screenname.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#' @examples
#' \dontrun{
#' ## get friends list for user with the handle "1234"
#' get_friends(as_screenname("1234"))
#' get_friends(as_screenname("12345"))
#'
#' ## as_screenname coerces all elements to class "screen_name"
#' sns <- as_screenname(c("kearneymw", "1234", "jack"))
Expand Down
6 changes: 3 additions & 3 deletions R/extractors.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
#' \dontrun{
#'
#' ## search for 100 tweets containing the letter r
#' r <- search_tweets("r")
#' tweets <- search_tweets("r")
#'
#' ## print tweets data (only first 10 rows are shown)
#' head(r, 10)
#' head(tweets, 10)
#'
#' ## extract users data
#' head(users_data(r))
#' head(users_data(tweets))
#'
#' }
#'
Expand Down
16 changes: 1 addition & 15 deletions R/followers.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,7 @@
#'
#' ## get max number [per fresh token] of POTUS follower IDs
#' (pres <- get_followers("potus", n = 75000))
#'
#' ## resume data collection (warning: rate limits reset every 15 minutes)
#' pres2 <- get_followers("potus", n = 75000, page = next_cursor(pres))
#'
#' ## store next cursor in object before merging data
#' nextpage <- next_cursor(pres2)
#'
#' ## merge data frames
#' pres <- rbind(pres, pres2)
#'
#' ## store next cursor as an attribute in the merged data frame
#' attr(pres, "next_cursor") <- next_page
#'
#' ## view merged ddata
#' pres
#' ## You might use retryonratelimit = TRUE to obtain exactly 750000
#'
#' }
#'
Expand Down
5 changes: 3 additions & 2 deletions R/friends.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
#' \dontrun{
#'
#' ## get user ids of accounts followed by Donald Trump
#' (djt <- get_friends("realDonaldTrump"))
#' (djt <- get_friends("ropensci"))
#'
#' ## get user ids of accounts followed by (friends) KFC, Trump, and Nate Silver.
#' ## get user ids of accounts followed by (friends) KFC, jack, and Nate Silver.
#' (fds <- get_friends(c("kfc", "jack", "NateSilver538")))
#'
#' }
Expand Down Expand Up @@ -74,6 +74,7 @@ get_friends <- function(users,
parse = TRUE,
verbose = TRUE,
token = NULL) {


stopifnot(is.vector(users), is_n(n))

Expand Down
3 changes: 0 additions & 3 deletions R/imports.R

This file was deleted.

4 changes: 4 additions & 0 deletions R/lists_statuses.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#' @family lists
#' @family tweets
#' @return data
#' @examples
#' \dontrun{
#' (ls_senators <- lists_statuses(slug = "senators", owner_user = "cspan"))
#' }
#' @export
lists_statuses <- function(list_id = NULL,
slug = NULL,
Expand Down
4 changes: 2 additions & 2 deletions R/lists_subscriptions.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
#'
#' \dontrun{
#'
#' ## get subscriptions of new york times politics list
#' ## get kearneymw subscriptions
#' rstats <- lists_subscriptions(
#' slug = "new-york-times-politics",
#' user = "kearneymw",
#' n = 1000
#' )
#'
Expand Down
4 changes: 2 additions & 2 deletions R/post-favorite.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#' @aliases post_favourite favorite_tweet
#' @examples
#' \dontrun{
#' rt <- search_tweets("rstats")
#' r <- lapply(rt$user_id, post_favorite)
#' rt <- search_tweets("#rstats", n = 1)
#' post_favorite(rt$status_id)
#' }
#' @family post
#' @export
Expand Down
11 changes: 4 additions & 7 deletions R/post-list.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@
#' cnn_users <- search_users("CNN", n = 200)
#'
#' ## filter and select more users to add to list
#' more_users <- cnn_users %>%
#' subset(verified & !tolower(screen_name) %in% tolower(users)) %>%
#' .$screen_name %>%
#' grep("cnn", ., ignore.case = TRUE, value = TRUE)
#' more_users <- cnn_users$screen_name[cnn_users$verified]
#' more_users <- setdiff(more_users, users)
#' more_users <- more_users[grepl("cnn", more_users, ignore.case = TRUE)]
#'
#' ## add more users to list- note: can only add up to 100 at a time
#' post_list(users = more_users, slug = "cnn-accounts")
Expand All @@ -47,9 +46,7 @@
#' rtweet:::api_screen_name()))
#'
#' ## select users on list without "cnn" in their name field
#' drop_users <- cnn_users %>%
#' subset(screen_name %in% more_users & !grepl("cnn", name, ignore.case = TRUE)) %>%
#' .$screen_name
#' drop_users <- cnn_users$screen_name[!grepl("cnn", cnn_users$screen_name, ignore.case = TRUE)]
#'
#' ## drop these users from the cnn list
#' post_list(users = drop_users, slug = "cnn-accounts",
Expand Down
33 changes: 26 additions & 7 deletions R/post-tweet.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,8 @@ post_tweet <- function(status = "my first rtweet #rstats",

## if delete
if (!is.null(destroy_id)) {
stopifnot(is.character(destroy_id) && length(destroy_id) == 1)

query <- sprintf("statuses/destroy/%s", destroy_id)
r <- TWIT_post(token, query)
message("your tweet has been deleted!")
return(invisible(r))
lifecycle::deprecate_warn("1.0.0", "post_tweet(destroy_id)", "post_destroy()")
return(post_destroy(destroy_id))
}

## if retweet
Expand All @@ -95,9 +91,9 @@ post_tweet <- function(status = "my first rtweet #rstats",
}

stopifnot(is.character(status), length(status) == 1)

## media if provided
if (!is.null(media)) {
check_media(media, media_alt_text)
media_id_string <- character(length(media))
for (i in seq_along(media)) {
media_id_string[[i]] <- upload_media_to_twitter(media[[i]], token, media_alt_text[[i]])
Expand Down Expand Up @@ -142,6 +138,7 @@ upload_media_to_twitter <- function(media,
mp4 = "video/mp4",
stop("Unsupported file extension", call. = FALSE)
)

file_size <- file.size(media)

if (file_size <= chunk_size && media_type != "video/mp4") {
Expand Down Expand Up @@ -227,3 +224,25 @@ wait_for_chunked_media <- function(resp, media_id, token = NULL) {

invisible()
}

check_media <- function(media, alt_text) {
media_type <- tools::file_ext(media)
if (length(media) > 4) {
stop("At most 4 images per plot can be uploaded.", call. = FALSE)
}

if (media_type %in% c("gif", "mp4") && length(media) > 1) {
stop("Cannot upload more than one gif or video per tweet.", call. = TRUE)
}

if (!is.null(alt_text) && length(alt_text) != length(media)) {
stop("Alt text for media isn't provided for each image.", call. = TRUE)
}
if (!any(media_type %in% c("jpg", "jpeg", "png", "gif", "mp4"))) {
stop("Media type format not recognized.", call. = TRUE)
}

if (any(nchar(alt_text) > 1000)) {
stop("Alt text cannot be bigger than 1000 characters.", call. = TRUE)
}
}
19 changes: 19 additions & 0 deletions R/post_destroy.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#' Delete status of user's Twitter account
#'
#' Deletes a status of user's profile.
#' @inheritParams post_tweet
#' @export
#' @examples
#' \dontrun{
#' pt <- post_tweet()
#' crt <- httr::content(pt)
#' post_destroy(crt$id_str)
#' }
post_destroy <- function(destroy_id, token = NULL) {
stopifnot(is.character(destroy_id) && length(destroy_id) == 1)

query <- sprintf("statuses/destroy/%s", destroy_id)
r <- TWIT_post(token, query)
message("your tweet has been deleted!")
return(invisible(r))
}
14 changes: 3 additions & 11 deletions R/ts_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@
#' @examples
#'
#' \dontrun{
#'
#' ## search for tweets containing "rstats"
#' rt <- search_tweets("rstats", n = 10000)
#'
#' ## plot frequency in 1 min intervals
#' ts_plot(rt, "mins")
#'
#' ## plot multiple time series--retweets vs non-retweets
#' rt %>%
#' dplyr::group_by(is_retweet) %>%
#' ts_plot("hours")
#' ts_plot(dplyr::group_by(tmls, is_retweet), "hours")
#'
#' ## compare account activity for some important US political figures
#' tmls <- get_timeline(
Expand All @@ -44,9 +41,7 @@
#' ts_plot(dplyr::group_by(tmls, screen_name), "weeks")
#'
#' ## group by screen name and is_retweet
#' tmls %>%
#' dplyr::group_by(tmls, screen_name, is_retweet) %>%
#' ts_plot("months")
#' ts_plot(dplyr::group_by(tmls, screen_name, is_retweet), "months")
#'
#' }
#' @family ts_data
Expand Down Expand Up @@ -110,10 +105,7 @@ ts_plot_ <- function(data, by = "days", trim = 0L, tz ="UTC", ...) {
#' ts_data(sens, "weeks")
#'
#' ## group by screen name and then use weekly intervals
#' sens %>%
#' dplyr::group_by(screen_name) %>%
#' ts_plot("weeks")
#'
#' ts_plot(dplyr::group_by(sens, screen_name), "weeks")
#' }
#'
#' @export
Expand Down
4 changes: 2 additions & 2 deletions R/tweet_threading.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' @examples
#' \dontrun{
#' tw <- lookup_statuses('1084143184664510470')
#' tw_thread <- tw%>%tweet_threading()
#' tw_thread <- tweet_threading(tw)
#' tw_thread
#' }
#' @seealso
Expand All @@ -36,7 +36,7 @@ tweet_threading <- function(tw, traverse = c('backwards','forwards'), n = 10, ve
for(i in traverse){
cat('\n')
.f <- get(sprintf('tweet_threading_%s',i),envir = asNamespace('rtweet'))
tw <- tw%>%.f(n,verbose)
tw <- .f(tw, n,verbose)
}

tw
Expand Down
25 changes: 22 additions & 3 deletions R/varnames.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@


users_names <- function() {
c("user_id", "screen_name", "name", "location", "description", "url", "protected", "followers_count", "friends_count", "listed_count", "statuses_count", "favourites_count", "account_created_at", "verified", "profile_url", "profile_expanded_url", "account_lang", "profile_banner_url", "profile_background_url", "profile_image_url")
c("user_id", "screen_name", "name", "location", "description", "url",
"protected", "followers_count", "friends_count", "listed_count",
"statuses_count", "favourites_count", "account_created_at", "verified",
"profile_url", "profile_expanded_url", "account_lang", "profile_banner_url",
"profile_background_url", "profile_image_url")
}

names_in_users <- function(x) {
Expand All @@ -17,6 +21,21 @@ names_in_tweets <- function(x) {
}

tweets_names <- function() {
c("user_id", "status_id", "created_at", "screen_name", "text", "source", "display_text_width", "reply_to_status_id", "reply_to_user_id", "reply_to_screen_name", "is_quote", "is_retweet", "favorite_count", "retweet_count", "hashtags", "symbols", "urls_url", "urls_t.co", "urls_expanded_url", "media_url", "media_t.co", "media_expanded_url", "media_type", "ext_media_url", "ext_media_t.co", "ext_media_expanded_url", "ext_media_type", "mentions_user_id", "mentions_screen_name", "lang", "quoted_status_id", "quoted_text", "quoted_created_at", "quoted_source", "quoted_favorite_count", "quoted_retweet_count", "quoted_user_id", "quoted_screen_name", "quoted_name", "quoted_followers_count", "quoted_friends_count", "quoted_statuses_count", "quoted_location", "quoted_description", "quoted_verified", "retweet_status_id", "retweet_text", "retweet_created_at", "retweet_source", "retweet_favorite_count", "retweet_user_id", "retweet_screen_name", "retweet_name", "retweet_followers_count", "retweet_friends_count", "retweet_statuses_count", "retweet_location", "retweet_description", "retweet_verified", "place_url", "place_name", "place_full_name", "place_type", "country", "country_code", "geo_coords", "coords_coords", "bbox_coords")
c("user_id", "status_id", "created_at", "screen_name", "text", "source",
"display_text_width", "reply_to_status_id", "reply_to_user_id",
"reply_to_screen_name", "is_quote", "is_retweet", "favorite_count",
"retweet_count", "hashtags", "symbols", "urls_url", "urls_t.co",
"urls_expanded_url", "media_url", "media_t.co", "media_expanded_url",
"media_type", "ext_media_url", "ext_media_t.co", "ext_media_expanded_url",
"ext_media_type", "mentions_user_id", "mentions_screen_name", "lang",
"quoted_status_id", "quoted_text", "quoted_created_at", "quoted_source",
"quoted_favorite_count", "quoted_retweet_count", "quoted_user_id",
"quoted_screen_name", "quoted_name", "quoted_followers_count", "quoted_friends_count",
"quoted_statuses_count", "quoted_location", "quoted_description",
"quoted_verified", "retweet_status_id", "retweet_text", "retweet_created_at",
"retweet_source", "retweet_favorite_count", "retweet_user_id", "retweet_screen_name",
"retweet_name", "retweet_followers_count", "retweet_friends_count", "retweet_statuses_count",
"retweet_location", "retweet_description", "retweet_verified", "place_url",
"place_name", "place_full_name", "place_type", "country", "country_code",
"geo_coords", "coords_coords", "bbox_coords")
}

2 changes: 1 addition & 1 deletion man/as_screenname.Rd

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

16 changes: 1 addition & 15 deletions man/get_followers.Rd

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

4 changes: 2 additions & 2 deletions man/get_friends.Rd

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

Loading

0 comments on commit 18375af

Please sign in to comment.