diff --git a/DESCRIPTION b/DESCRIPTION index 5290d6f9..7e3f2c36 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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), diff --git a/NAMESPACE b/NAMESPACE index 1f202586..ee142f3d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) @@ -96,4 +96,3 @@ export(users_data) export(users_with_tweets) export(write_as_csv) import(rlang) -importFrom(magrittr,"%>%") diff --git a/NEWS.md b/NEWS.md index cf563420..0fa5fc36 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/R/as_screenname.R b/R/as_screenname.R index ba349715..d06bfeaf 100644 --- a/R/as_screenname.R +++ b/R/as_screenname.R @@ -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")) diff --git a/R/extractors.R b/R/extractors.R index cd1e276b..f4782d70 100644 --- a/R/extractors.R +++ b/R/extractors.R @@ -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)) #' #' } #' diff --git a/R/followers.R b/R/followers.R index d3717f12..e649fcf4 100644 --- a/R/followers.R +++ b/R/followers.R @@ -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 #' #' } #' diff --git a/R/friends.R b/R/friends.R index 1427c431..d9d56ddf 100644 --- a/R/friends.R +++ b/R/friends.R @@ -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"))) #' #' } @@ -74,6 +74,7 @@ get_friends <- function(users, parse = TRUE, verbose = TRUE, token = NULL) { + stopifnot(is.vector(users), is_n(n)) diff --git a/R/imports.R b/R/imports.R deleted file mode 100644 index ce0b79ed..00000000 --- a/R/imports.R +++ /dev/null @@ -1,3 +0,0 @@ -#' @importFrom magrittr %>% -#' @export -magrittr::`%>%` diff --git a/R/lists_statuses.R b/R/lists_statuses.R index 163b13bb..a978efe6 100644 --- a/R/lists_statuses.R +++ b/R/lists_statuses.R @@ -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, diff --git a/R/lists_subscriptions.R b/R/lists_subscriptions.R index 49ad5f42..81574fc1 100644 --- a/R/lists_subscriptions.R +++ b/R/lists_subscriptions.R @@ -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 #' ) #' diff --git a/R/post-favorite.R b/R/post-favorite.R index 249fc666..c01db3cf 100644 --- a/R/post-favorite.R +++ b/R/post-favorite.R @@ -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 diff --git a/R/post-list.R b/R/post-list.R index 32aa70c2..b16e8b77 100644 --- a/R/post-list.R +++ b/R/post-list.R @@ -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") @@ -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", diff --git a/R/post-tweet.R b/R/post-tweet.R index e4b023e5..51e178dd 100644 --- a/R/post-tweet.R +++ b/R/post-tweet.R @@ -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 @@ -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]]) @@ -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") { @@ -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) + } +} diff --git a/R/post_destroy.R b/R/post_destroy.R new file mode 100644 index 00000000..51b8a7ec --- /dev/null +++ b/R/post_destroy.R @@ -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)) +} diff --git a/R/ts_plot.R b/R/ts_plot.R index c9da7861..9d05ddfb 100644 --- a/R/ts_plot.R +++ b/R/ts_plot.R @@ -19,7 +19,6 @@ #' @examples #' #' \dontrun{ -#' #' ## search for tweets containing "rstats" #' rt <- search_tweets("rstats", n = 10000) #' @@ -27,9 +26,7 @@ #' 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( @@ -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 @@ -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 diff --git a/R/tweet_threading.R b/R/tweet_threading.R index cd360d02..2b23fe9e 100644 --- a/R/tweet_threading.R +++ b/R/tweet_threading.R @@ -15,7 +15,7 @@ #' @examples #' \dontrun{ #' tw <- lookup_statuses('1084143184664510470') -#' tw_thread <- tw%>%tweet_threading() +#' tw_thread <- tweet_threading(tw) #' tw_thread #' } #' @seealso @@ -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 diff --git a/R/varnames.R b/R/varnames.R index e7b72bcc..32668c10 100644 --- a/R/varnames.R +++ b/R/varnames.R @@ -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) { @@ -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") } - diff --git a/man/as_screenname.Rd b/man/as_screenname.Rd index bd02a2c4..6cb96717 100644 --- a/man/as_screenname.Rd +++ b/man/as_screenname.Rd @@ -30,7 +30,7 @@ get_followers. \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")) diff --git a/man/get_followers.Rd b/man/get_followers.Rd index 4802ca42..0cd6f709 100644 --- a/man/get_followers.Rd +++ b/man/get_followers.Rd @@ -74,21 +74,7 @@ rate limit maximum), set "retryonratelimit" to TRUE. ## 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 } diff --git a/man/get_friends.Rd b/man/get_friends.Rd index 02b9df38..d4132183 100644 --- a/man/get_friends.Rd +++ b/man/get_friends.Rd @@ -93,9 +93,9 @@ friends lists for a user over time. \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"))) } diff --git a/man/lists_statuses.Rd b/man/lists_statuses.Rd index 41363dfe..f8996447 100644 --- a/man/lists_statuses.Rd +++ b/man/lists_statuses.Rd @@ -62,6 +62,11 @@ data \description{ Get a timeline of tweets authored by members of a specified list. } +\examples{ +\dontrun{ +(ls_senators <- lists_statuses(slug = "senators", owner_user = "cspan")) +} +} \seealso{ Other lists: \code{\link{lists_members}()}, diff --git a/man/lists_subscriptions.Rd b/man/lists_subscriptions.Rd index d2b1d5b0..1515c2f1 100644 --- a/man/lists_subscriptions.Rd +++ b/man/lists_subscriptions.Rd @@ -40,9 +40,9 @@ Get list subscriptions of a given user. \dontrun{ -## get subscriptions of new york times politics list +## get kearneymw subscriptions rstats <- lists_subscriptions( - slug = "new-york-times-politics", + user = "kearneymw", n = 1000 ) diff --git a/man/post_destroy.Rd b/man/post_destroy.Rd new file mode 100644 index 00000000..fbf487bc --- /dev/null +++ b/man/post_destroy.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/post_destroy.R +\name{post_destroy} +\alias{post_destroy} +\title{Delete status of user's Twitter account} +\usage{ +post_destroy(destroy_id, token = NULL) +} +\arguments{ +\item{destroy_id}{To delete a status, supply the single status ID here. If a +character string is supplied, overriding the default (NULL), then a destroy +request is made (and the status text and media attachments) are irrelevant.} + +\item{token}{An OAuth token object. The default, \code{NULL}, will retrieve +a default token with \code{\link[=get_token]{get_token()}}. You should only need to +use this argument if you are wrapping rtweet in a package. + +See \code{vignette("auth")} for more details.} +} +\description{ +Deletes a status of user's profile. +} +\examples{ +\dontrun{ +pt <- post_tweet() +crt <- httr::content(pt) +post_destroy(crt$id_str) +} +} diff --git a/man/post_favorite.Rd b/man/post_favorite.Rd index 6ea60b2a..228d70a8 100644 --- a/man/post_favorite.Rd +++ b/man/post_favorite.Rd @@ -33,8 +33,8 @@ Favorites target status id. } \examples{ \dontrun{ -rt <- search_tweets("rstats") -r <- lapply(rt$user_id, post_favorite) +rt <- search_tweets("#rstats", n = 1) +post_favorite(rt$status_id) } } \seealso{ diff --git a/man/post_list.Rd b/man/post_list.Rd index 793ad157..2d85a8b7 100644 --- a/man/post_list.Rd +++ b/man/post_list.Rd @@ -65,10 +65,9 @@ browseURL(sprintf("https://twitter.com/\%s/lists/cnn-accounts", 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") @@ -78,9 +77,7 @@ browseURL(sprintf("https://twitter.com/\%s/lists/cnn-accounts", 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", diff --git a/man/reexports.Rd b/man/reexports.Rd deleted file mode 100644 index ba445cdc..00000000 --- a/man/reexports.Rd +++ /dev/null @@ -1,16 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/imports.R -\docType{import} -\name{reexports} -\alias{reexports} -\alias{\%>\%} -\title{Objects exported from other packages} -\keyword{internal} -\description{ -These objects are imported from other packages. Follow the links -below to see their documentation. - -\describe{ - \item{magrittr}{\code{\link[magrittr:pipe]{\%>\%}}} -}} - diff --git a/man/ts_data.Rd b/man/ts_data.Rd index a9342abc..58cd803e 100644 --- a/man/ts_data.Rd +++ b/man/ts_data.Rd @@ -43,10 +43,7 @@ ts_data(sens) 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") } } diff --git a/man/ts_plot.Rd b/man/ts_plot.Rd index 0306d115..8fd86b32 100644 --- a/man/ts_plot.Rd +++ b/man/ts_plot.Rd @@ -34,7 +34,6 @@ interval of time. \examples{ \dontrun{ - ## search for tweets containing "rstats" rt <- search_tweets("rstats", n = 10000) @@ -42,9 +41,7 @@ rt <- search_tweets("rstats", n = 10000) 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( @@ -59,9 +56,7 @@ ts_plot(tmls, "weeks") 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") } } diff --git a/man/tweet_threading.Rd b/man/tweet_threading.Rd index 784d652d..c68a1265 100644 --- a/man/tweet_threading.Rd +++ b/man/tweet_threading.Rd @@ -34,7 +34,7 @@ the origin status. \examples{ \dontrun{ tw <- lookup_statuses('1084143184664510470') -tw_thread <- tw\%>\%tweet_threading() +tw_thread <- tweet_threading(tw) tw_thread } } diff --git a/man/users_data.Rd b/man/users_data.Rd index 23ca97da..1a70f3fc 100644 --- a/man/users_data.Rd +++ b/man/users_data.Rd @@ -25,13 +25,13 @@ Extracts users data from tweets data object. \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)) } diff --git a/tests/testthat/test-direct_messages.R b/tests/testthat/test-direct_messages.R new file mode 100644 index 00000000..f9dd687e --- /dev/null +++ b/tests/testthat/test-direct_messages.R @@ -0,0 +1,10 @@ +test_that("direct_messages works", { + dm <- direct_messages() + expect_type(dm, "list") + expect_length(dm, 2L) +}) + +test_that("old functions give informative errors", { + expect_error(direct_messages_received(), "no longer exists") + expect_error(direct_messages_sent(), "no longer exists") +}) diff --git a/tests/testthat/test-followers.R b/tests/testthat/test-followers.R index 4daa55de..0be0abee 100644 --- a/tests/testthat/test-followers.R +++ b/tests/testthat/test-followers.R @@ -2,9 +2,9 @@ test_that("get_followers returns data frame with user_id", { skip_on_cran() skip_if_offline() - f <- get_followers("HillaryClinton", n = 10000) + f <- get_followers("HillaryClinton", n = 100) expect_s3_class(f, "tbl_df") expect_named(f, "user_id") - expect_gt(NROW(f), 9999) + expect_gt(NROW(f), 99) }) diff --git a/tests/testthat/test-get_followers.R b/tests/testthat/test-get_followers.R new file mode 100644 index 00000000..17a31c0e --- /dev/null +++ b/tests/testthat/test-get_followers.R @@ -0,0 +1,5 @@ +test_that("get_followers works", { + kfc <- get_followers("KFC") + expect_s3_class(kfc, "tbl") + expect_equal(nrow(kfc), 5000) +}) diff --git a/tests/testthat/test-get_friends.R b/tests/testthat/test-get_friends.R new file mode 100644 index 00000000..42c44aa6 --- /dev/null +++ b/tests/testthat/test-get_friends.R @@ -0,0 +1,4 @@ +test_that("get_friends works", { + djt <- get_friends("ropensci") + expect_s3_class(djt, "tbl_df") +}) diff --git a/tests/testthat/test-get_my_timeline.R b/tests/testthat/test-get_my_timeline.R new file mode 100644 index 00000000..5ea80a03 --- /dev/null +++ b/tests/testthat/test-get_my_timeline.R @@ -0,0 +1,5 @@ +test_that("get_my_timeline works", { + gmt <- get_my_timeline() + expect_s3_class(gmt, "tbl_df") + expect_equal(nrow(gmt), 100) +}) diff --git a/tests/testthat/test-lookup_friendships.R b/tests/testthat/test-lookup_friendships.R new file mode 100644 index 00000000..b5f47757 --- /dev/null +++ b/tests/testthat/test-lookup_friendships.R @@ -0,0 +1,4 @@ +test_that("lookup_friendships works", { + lf <- lookup_friendships("hadley", "Lluis_Revilla") + expect_s3_class(lf, "data.frame") +}) diff --git a/tests/testthat/test-lookup_users.R b/tests/testthat/test-lookup_users.R new file mode 100644 index 00000000..212adc0c --- /dev/null +++ b/tests/testthat/test-lookup_users.R @@ -0,0 +1,10 @@ +test_that("lookup_users works", { + users <- c( + "potus", "hillaryclinton", "realdonaldtrump", + "fivethirtyeight", "cnn", "espn", "twitter" + ) + usr_df <- lookup_users(users) + expect_s3_class(usr_df, "tbl") + expect_equal(nrow(usr_df), 6) + expect_equal(ncol(usr_df), 91) +}) diff --git a/tests/testthat/test-my_friendships.R b/tests/testthat/test-my_friendships.R new file mode 100644 index 00000000..f910784d --- /dev/null +++ b/tests/testthat/test-my_friendships.R @@ -0,0 +1,4 @@ +test_that("my_friendships works", { + mf <- my_friendships("hadley") + expect_s3_class(mf, "data.frame") +}) diff --git a/tests/testthat/test-network_data.R b/tests/testthat/test-network_data.R new file mode 100644 index 00000000..f8e3f96b --- /dev/null +++ b/tests/testthat/test-network_data.R @@ -0,0 +1,16 @@ +test_that("network_data works", { + rstats <- search_tweets("#rstats", n = 20) + ## create from-to data frame representing retweet/mention/reply connections + rstats_net <- network_data(rstats, "retweet,mention,reply") + expect_s3_class(rstats_net, "data.frame") + expect_equal(colnames(rstats_net), c("from", "to", "type")) +}) + + +test_that("network_graph works", { + rstats <- search_tweets("#rstats", n = 20) + + ## create from-to data frame representing retweet/mention/reply connections + rstats_net <- network_graph(rstats) + expect_s3_class(rstats_net, "igraph") +}) diff --git a/tests/testthat/test-post.R b/tests/testthat/test-post.R index 2e80ca81..ee5d699e 100644 --- a/tests/testthat/test-post.R +++ b/tests/testthat/test-post.R @@ -24,3 +24,14 @@ test_that("can set alt text", { id <- upload_media_to_twitter(test_path("tweet.gif"), alt_text = "A bird tweeting") expect_type(id, "character") }) + + +test_that("post_tweet works", { + skip_if_offline() + skip_on_cran() + msg <- paste("test", Sys.time()) # To avoid having duplicated status + rt <- expect_message(post_tweet(msg), "your tweet has been posted!") + crt <- httr::content(rt) + rt <- expect_message(post_destroy(crt$id_str), "your tweet has been deleted!") + expect_equal(httr::status_code(rt), 200L) +}) diff --git a/tests/testthat/test-post_favorite.R b/tests/testthat/test-post_favorite.R new file mode 100644 index 00000000..0429674d --- /dev/null +++ b/tests/testthat/test-post_favorite.R @@ -0,0 +1,5 @@ +test_that("post_favorite works", { + rt <- search_tweets("#rstats", n = 1) + expect_equal(httr::status_code(post_favorite(rt$status_id)), 200) + expect_equal(httr::status_code(post_favorite(rt$status_id, destroy = TRUE)), 200) +}) diff --git a/tests/testthat/test-post_follow.R b/tests/testthat/test-post_follow.R new file mode 100644 index 00000000..f93a7730 --- /dev/null +++ b/tests/testthat/test-post_follow.R @@ -0,0 +1,16 @@ +test_that("post_follow works", { + pf <- post_follow("BarackObama") + expect_equal(httr::status_code(pf), 200L) +}) + +test_that("post_destroy works", { + pf <- post_follow("BarackObama", destroy = TRUE) + expect_equal(httr::status_code(pf), 200L) +}) + +test_that("post_follow works with notifications", { + pf <- post_follow("BarackObama", notify = TRUE) + expect_equal(httr::status_code(pf), 200L) + pf <- post_follow("BarackObama", destroy = TRUE) + expect_equal(httr::status_code(pf), 200L) +}) diff --git a/tests/testthat/test-post_message.R b/tests/testthat/test-post_message.R new file mode 100644 index 00000000..dde12017 --- /dev/null +++ b/tests/testthat/test-post_message.R @@ -0,0 +1,7 @@ +test_that("post_message works", { + rt <- expect_message( + post_message(paste("Testing", Sys.time()), user = "Lluis_Revilla"), + "your DM has been posted!" + ) + expect_equal(httr::status_code(rt), 200L) +}) diff --git a/tests/testthat/test-rate_limit.R b/tests/testthat/test-rate_limit.R new file mode 100644 index 00000000..95866f5f --- /dev/null +++ b/tests/testthat/test-rate_limit.R @@ -0,0 +1,19 @@ +test_that("rate_limit works", { + rl <- rate_limit() + expect_s3_class(rl, "tbl_df") +}) +test_that("rate_limit works", { + rl <- rate_limit("application/rate_limit_status") + expect_s3_class(rl, "tbl_df") + expect_equal(nrow(rl), 1) +}) + + +test_that("rate_limit_reset works", { + reset <- rate_limit_reset("application/rate_limit_status") + expect_s3_class(reset, "POSIXct") +}) +test_that("rate_limit_wait works", { + wait <- rate_limit_wait("application/rate_limit_status") + expect_null(wait) +}) diff --git a/tests/testthat/test-search_tweets.R b/tests/testthat/test-search_tweets.R index 34af8959..4885c752 100644 --- a/tests/testthat/test-search_tweets.R +++ b/tests/testthat/test-search_tweets.R @@ -1,7 +1,4 @@ test_that("search_tweets returns tweets data and latlng", { - skip_on_cran() - skip_if_offline() - df <- search_tweets("#rstats", n = 50) expect_s3_class(df, "tbl_df") expect_true(nrow(df) > 25) # should almost always be true @@ -15,3 +12,9 @@ test_that("gives useful errors", { expect_snapshot(search_tweets(c(1:10), verbose = FALSE), error = TRUE) expect_snapshot(search_tweets("stats", type = "all"), error = TRUE) }) + +test_that("search_tweets2 can search for multiple queries", { + df <- search_tweets2(c("#rstats", "open science"), n = 50) + expect_s3_class(df, "tbl_df") + expect_true(nrow(df) > 25) # should almost always be true +}) diff --git a/tests/testthat/test-tweet_threading.R b/tests/testthat/test-tweet_threading.R new file mode 100644 index 00000000..2af57ffa --- /dev/null +++ b/tests/testthat/test-tweet_threading.R @@ -0,0 +1,5 @@ +test_that("tweet_threading works", { + tw <- lookup_statuses('1084143184664510470') + tw_thread <- tweet_threading(tw) + expect_s3_class(tw_thread, "tbl_df") +}) diff --git a/tests/testthat/test-tweets_data.R b/tests/testthat/test-tweets_data.R new file mode 100644 index 00000000..455c0143 --- /dev/null +++ b/tests/testthat/test-tweets_data.R @@ -0,0 +1,8 @@ +test_that("tweets_data works", { + jack <- lookup_users("jack") + + ## get data on most recent tweet from user(s) + tweets <- tweets_data(jack) + expect_s3_class(tweets, "tbl") + expect_equal(colnames(tweets), tweets_names()) +}) diff --git a/tests/testthat/test-users_data.R b/tests/testthat/test-users_data.R new file mode 100644 index 00000000..3b446416 --- /dev/null +++ b/tests/testthat/test-users_data.R @@ -0,0 +1,5 @@ +test_that("users_data works", { + tweets <- search_tweets("r") + users <- users_data(tweets) + expect_s3_class(users, "tbl") +})