[rtweet] Collecter des données sur Twitter

En cours…

Introduction

Le but de ce document est de présenter comment collecter des informations sur Twitter. Pour pouvoir faire cela, il est nécessaire d’avoir un compte et d’avoir activé un compte développeur pour utiliser l’API [Plus d’info dans le tuto 3 et le tuto 2.

Librairie

Nous allons utiliser la librairie Rtweet.

library(rtweet)
ls("package:rtweet")
##  [1] "%>%"                      "as_screenname"           
##  [3] "as_userid"                "bearer_token"            
##  [5] "create_token"             "direct_messages"         
##  [7] "direct_messages_received" "direct_messages_sent"    
##  [9] "do_call_rbind"            "emojis"                  
## [11] "flatten"                  "get_collections"         
## [13] "get_favorites"            "get_followers"           
## [15] "get_friends"              "get_mentions"            
## [17] "get_my_timeline"          "get_retweeters"          
## [19] "get_retweets"             "get_timeline"            
## [21] "get_timelines"            "get_token"               
## [23] "get_tokens"               "get_trends"              
## [25] "invalidate_bearer"        "langs"                   
## [27] "lat_lng"                  "lists_members"           
## [29] "lists_memberships"        "lists_statuses"          
## [31] "lists_subscribers"        "lists_subscriptions"     
## [33] "lists_users"              "lookup_collections"      
## [35] "lookup_coords"            "lookup_friendships"      
## [37] "lookup_statuses"          "lookup_tweets"           
## [39] "lookup_users"             "max_id"                  
## [41] "my_friendships"           "network_data"            
## [43] "network_graph"            "next_cursor"             
## [45] "parse_stream"             "plain_tweets"            
## [47] "post_favorite"            "post_follow"             
## [49] "post_friendship"          "post_list"               
## [51] "post_message"             "post_mute"               
## [53] "post_tweet"               "post_unfollow_user"      
## [55] "previous_cursor"          "rate_limit"              
## [57] "rate_limits"              "read_twitter_csv"        
## [59] "round_time"               "save_as_csv"             
## [61] "search_30day"             "search_fullarchive"      
## [63] "search_tweets"            "search_tweets2"          
## [65] "search_users"             "since_id"                
## [67] "stopwordslangs"           "stream_tweets"           
## [69] "stream_tweets2"           "suggested_slugs"         
## [71] "suggested_users"          "suggested_users_all"     
## [73] "trends_available"         "ts_data"                 
## [75] "ts_plot"                  "tweet_shot"              
## [77] "tweets_data"              "tweets_with_users"       
## [79] "unflatten"                "users_data"              
## [81] "users_with_tweets"        "write_as_csv"

La librairie possède 82 fonctions pris de plusieurs autres librairies pour une partie, mais nous utiliserons principalement les 13 fonctions suivantes:

FonctionUtilité
%>%Passer le résultat de gauche dans la fonction de droite.
twitter_token()Permet de générer un “token” qui permet de collecter des données et contrôler le compte.
get_trends()Voire les tendances
search_tweets()Chercher des tweets
get_timeline()Voir les derniers tweets d’un compte
get_favorites()Voir les derniers tweets aimés par un compte
get_friends()Voir les compte suivit pas un compte
get_followers()Voir les comptes qui suivent un compte
get_retweeters()Voir les compte qui ont retweeté un tweet
ts_plot()Afficher une ligne temporelle d’un nombre d’éléments.
network_graph()Créer un graphe de réseau
network_data()Créer un tableau de données au format igraph

Obtenir un token

Nous devons créer un token pour notre session afin de pouvoir faire des requêtes. Toutes les informations sont trouvables dans votre application en ligne.

appname <- "XXXXX"
api_key <- "XXXXX"
api_secret <- "XXXXX"
token_key <- "XXXXX"
token_secret <- "XXXXX"

Note: Ici les valeurs sont censurées

Puis nous pouvons lancer la fonction suivante:

twitter_token <- create_token(
  app = appname,
  consumer_key = api_key,
  consumer_secret = api_secret,
  access_token = token_key,
  access_secret = token_secret)

Fonction

search_tweets()

Permet de rechercher des tweets selon plusieurs modalités (nombre, position géographique, retweets, etc.), dans cet exemple nous recherchons le terme “statistic” en incluant les retweets.

twitter_1 <- search_tweets("statistic", n = 1000,
                           include_rts = TRUE)
dplyr::glimpse(twitter_1)
## Rows: 1,000
## Columns: 90
## $ user_id                 <chr> "42390674", "1230523115140272134", "1335006418~
## $ status_id               <chr> "1406680311438757898", "1406680264743567360", ~
## $ created_at              <dttm> 2021-06-20 18:28:15, 2021-06-20 18:28:04, 202~
## $ screen_name             <chr> "jridz", "ProficientW3", "CovidXmas2021", "Phr~
## $ text                    <chr> "If you fell in love with a monster, always re~
## $ source                  <chr> "Twitter for iPhone", "Twitter for Android", "~
## $ display_text_width      <dbl> 140, 234, 140, 100, 140, 219, 233, 140, 100, 1~
## $ reply_to_status_id      <chr> NA, NA, NA, NA, NA, NA, "1406622410871169026",~
## $ reply_to_user_id        <chr> NA, NA, NA, NA, NA, NA, "86626845", NA, NA, NA~
## $ reply_to_screen_name    <chr> NA, NA, NA, NA, NA, NA, "EricTopol", NA, NA, N~
## $ is_quote                <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE~
## $ is_retweet              <lgl> TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, T~
## $ favorite_count          <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0~
## $ retweet_count           <int> 4, 0, 15, 89, 135, 0, 0, 1, 89, 89, 127, 0, 16~
## $ quote_count             <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ reply_count             <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ hashtags                <list> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ symbols                 <list> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ urls_url                <list> NA, NA, NA, NA, NA, "twitter.com/MensHealthUK~
## $ urls_t.co               <list> NA, NA, NA, NA, NA, "https://t.co/CD5CaaNVds"~
## $ urls_expanded_url       <list> NA, NA, NA, NA, NA, "https://twitter.com/Mens~
## $ media_url               <list> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ media_t.co              <list> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ media_expanded_url      <list> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ media_type              <list> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ ext_media_url           <list> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ ext_media_t.co          <list> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ ext_media_expanded_url  <list> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ ext_media_type          <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ mentions_user_id        <list> "28109485", NA, "1145702015374438401", "85074~
## $ mentions_screen_name    <list> "ShiryuGL", NA, "VDAREJamesK", "ltarsenal", "~
## $ lang                    <chr> "en", "en", "en", "en", "en", "en", "en", "en"~
## $ quoted_status_id        <chr> NA, NA, NA, NA, NA, "1405817119581876225", NA,~
## $ quoted_text             <chr> NA, NA, NA, NA, NA, "That 25 per cent of pregn~
## $ quoted_created_at       <dttm> NA, NA, NA, NA, NA, 2021-06-18 09:18:14, NA, ~
## $ quoted_source           <chr> NA, NA, NA, NA, NA, "Twitter Web App", NA, NA,~
## $ quoted_favorite_count   <int> NA, NA, NA, NA, NA, 377, NA, NA, NA, NA, NA, N~
## $ quoted_retweet_count    <int> NA, NA, NA, NA, NA, 127, NA, NA, NA, NA, NA, N~
## $ quoted_user_id          <chr> NA, NA, NA, NA, NA, "21093744", NA, NA, NA, NA~
## $ quoted_screen_name      <chr> NA, NA, NA, NA, NA, "MensHealthUK", NA, NA, NA~
## $ quoted_name             <chr> NA, NA, NA, NA, NA, "Men's Health UK", NA, NA,~
## $ quoted_followers_count  <int> NA, NA, NA, NA, NA, 307904, NA, NA, NA, NA, NA~
## $ quoted_friends_count    <int> NA, NA, NA, NA, NA, 740, NA, NA, NA, NA, NA, N~
## $ quoted_statuses_count   <int> NA, NA, NA, NA, NA, 103514, NA, NA, NA, NA, NA~
## $ quoted_location         <chr> NA, NA, NA, NA, NA, "London, UK", NA, NA, NA, ~
## $ quoted_description      <chr> NA, NA, NA, NA, NA, "The only magazine that im~
## $ quoted_verified         <lgl> NA, NA, NA, NA, NA, TRUE, NA, NA, NA, NA, NA, ~
## $ retweet_status_id       <chr> "1403712057514541058", NA, "140664500013025280~
## $ retweet_text            <chr> "If you fell in love with a monster, always re~
## $ retweet_created_at      <dttm> 2021-06-12 13:53:28, NA, 2021-06-20 16:07:56,~
## $ retweet_source          <chr> "Twitter Web App", NA, "Twitter Web App", "Twi~
## $ retweet_favorite_count  <int> 16, NA, 144, 1697, 455, NA, NA, 1, 1697, 1697,~
## $ retweet_retweet_count   <int> 4, NA, 15, 89, 135, NA, NA, 1, 89, 89, 127, NA~
## $ retweet_user_id         <chr> "28109485", NA, "1145702015374438401", "850747~
## $ retweet_screen_name     <chr> "ShiryuGL", NA, "VDAREJamesK", "ltarsenal", "a~
## $ retweet_name            <chr> "Shiryu", NA, "James Kirkpatrick", "LTArsenal™~
## $ retweet_followers_count <int> 1165, NA, 8528, 186182, 164208, NA, NA, 436, 1~
## $ retweet_friends_count   <int> 2280, NA, 1428, 1489, 7776, NA, NA, 377, 1489,~
## $ retweet_statuses_count  <int> 74921, NA, 19116, 41872, 296359, NA, NA, 14341~
## $ retweet_location        <chr> "Lisbon, Portugal, Europe", NA, "Occupied Virg~
## $ retweet_description     <chr> "\"No Surrender, No Retreat.\"\n\nMusic Produc~
## $ retweet_verified        <lgl> FALSE, NA, FALSE, FALSE, TRUE, NA, NA, FALSE, ~
## $ place_url               <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ place_name              <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ place_full_name         <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ place_type              <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ country                 <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ country_code            <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ geo_coords              <list> <NA, NA>, <NA, NA>, <NA, NA>, <NA, NA>, <NA, ~
## $ coords_coords           <list> <NA, NA>, <NA, NA>, <NA, NA>, <NA, NA>, <NA, ~
## $ bbox_coords             <list> <NA, NA, NA, NA, NA, NA, NA, NA>, <NA, NA, NA~
## $ status_url              <chr> "https://twitter.com/jridz/status/140668031143~
## $ name                    <chr> "Jarid 6<U+00013153>", "Proficient Writers", "Covid Chris~
## $ location                <chr> "", "Los Angeles, CA", "", "Accra, Ghana", "La~
## $ description             <chr> "", "Highly qualified,reliable & proficient  w~
## $ url                     <chr> NA, NA, NA, NA, "https://t.co/KGxVmnpGOO", "ht~
## $ protected               <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALS~
## $ followers_count         <int> 482, 492, 14, 1694, 7646, 1817, 744, 436, 153,~
## $ friends_count           <int> 480, 872, 22, 2329, 94, 1406, 1013, 377, 675, ~
## $ listed_count            <int> 1, 0, 0, 3, 32, 32, 36, 1, 0, 0, 1, 1, 3, 37, ~
## $ statuses_count          <int> 34785, 23374, 3100, 3665, 2825, 3906, 17831, 1~
## $ favourites_count        <int> 4996, 468, 14028, 4505, 171, 6912, 313401, 591~
## $ account_created_at      <dttm> 2009-05-25 11:36:14, 2020-02-20 16:03:10, 202~
## $ verified                <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALS~
## $ profile_url             <chr> NA, NA, NA, NA, "https://t.co/KGxVmnpGOO", "ht~
## $ profile_expanded_url    <chr> NA, NA, NA, NA, "http://www.wildfusions.com", ~
## $ account_lang            <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ profile_banner_url      <chr> "https://pbs.twimg.com/profile_banners/4239067~
## $ profile_background_url  <chr> "http://abs.twimg.com/images/themes/theme14/bg~
## $ profile_image_url       <chr> "http://pbs.twimg.com/profile_images/140601532~

get_timeline()

Cette fonction permet de collecter les dernier tweets d’un ou plusieurs compte, dans cet exemple nous collectons les 100 derniers tweets du compte @rstudiotips:

twitter_2 <- get_timeline("rstudiotips", n = 100)
dplyr::glimpse(twitter_2)
## Rows: 100
## Columns: 90
## $ user_id                 <chr> "2613859267", "2613859267", "2613859267", "261~
## $ status_id               <chr> "1405562538029293572", "1402660786162388997", ~
## $ created_at              <dttm> 2021-06-17 16:26:37, 2021-06-09 16:16:06, 202~
## $ screen_name             <chr> "rstudiotips", "rstudiotips", "rstudiotips", "~
## $ text                    <chr> "OMG updating @rstudio and now have a memory t~
## $ source                  <chr> "Twitter Web App", "Twitter Web App", "Twitter~
## $ display_text_width      <dbl> 133, 209, 140, 2, 140, 140, 58, 3, 224, 140, 1~
## $ reply_to_status_id      <chr> NA, NA, NA, "1400815785300344833", NA, NA, "13~
## $ reply_to_user_id        <chr> NA, NA, NA, "56458354", NA, NA, "392770190", "~
## $ reply_to_screen_name    <chr> NA, NA, NA, "d_olivaw", NA, NA, "drserdarbalci~
## $ is_quote                <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALS~
## $ is_retweet              <lgl> TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE, F~
## $ favorite_count          <int> 0, 137, 0, 68, 0, 0, 2, 24, 171, 0, 0, 0, 0, 8~
## $ retweet_count           <int> 49, 42, 3, 3, 16, 62, 2, 0, 70, 34, 170, 231, ~
## $ quote_count             <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ reply_count             <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ hashtags                <list> NA, "rstats", NA, NA, NA, NA, NA, NA, "rstats~
## $ symbols                 <list> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ urls_url                <list> NA, "blog.rstudio.com/2021/06/09/rst…", NA, N~
## $ urls_t.co               <list> NA, "https://t.co/9hsOvMwsoZ", NA, NA, NA, NA~
## $ urls_expanded_url       <list> NA, "https://blog.rstudio.com/2021/06/09/rstu~
## $ media_url               <list> "http://pbs.twimg.com/media/E3svvVOVUAEKNIg.p~
## $ media_t.co              <list> "https://t.co/ehmmS7i61D", NA, NA, NA, NA, NA~
## $ media_expanded_url      <list> "https://twitter.com/skyetetra/status/1403768~
## $ media_type              <list> "photo", NA, NA, NA, NA, NA, NA, NA, NA, NA, ~
## $ ext_media_url           <list> "http://pbs.twimg.com/media/E3svvVOVUAEKNIg.p~
## $ ext_media_t.co          <list> "https://t.co/ehmmS7i61D", NA, NA, NA, NA, NA~
## $ ext_media_expanded_url  <list> "https://twitter.com/skyetetra/status/1403768~
## $ ext_media_type          <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ mentions_user_id        <list> <"14225080", "235261861">, "235261861", <"100~
## $ mentions_screen_name    <list> <"skyetetra", "rstudio">, "rstudio", <"mdvige~
## $ lang                    <chr> "en", "en", "en", "und", "en", "en", "en", "un~
## $ quoted_status_id        <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_text             <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_created_at       <dttm> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ quoted_source           <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_favorite_count   <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_retweet_count    <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_user_id          <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_screen_name      <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_name             <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_followers_count  <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_friends_count    <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_statuses_count   <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_location         <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_description      <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_verified         <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ retweet_status_id       <chr> "1403768328087236608", NA, "140216725248564428~
## $ retweet_text            <chr> "OMG updating @rstudio and now have a memory t~
## $ retweet_created_at      <dttm> 2021-06-12 17:37:04, NA, 2021-06-08 07:34:58,~
## $ retweet_source          <chr> "Twitter Web App", NA, "Twitter Web App", NA, ~
## $ retweet_favorite_count  <int> 866, NA, 17, NA, 169, 378, NA, NA, NA, 226, 10~
## $ retweet_retweet_count   <int> 49, NA, 3, NA, 16, 62, NA, NA, NA, 34, 170, 23~
## $ retweet_user_id         <chr> "14225080", NA, "1000270608545050625", NA, "41~
## $ retweet_screen_name     <chr> "skyetetra", NA, "mdvigeland", NA, "sharlagelf~
## $ retweet_name            <chr> "Dr. Jacqueline Nolis", NA, "Magnus Dehli Vige~
## $ retweet_followers_count <int> 11792, NA, 15, NA, 8482, 4124, NA, NA, NA, 201~
## $ retweet_friends_count   <int> 535, NA, 105, NA, 323, 399, NA, NA, NA, 915, 8~
## $ retweet_statuses_count  <int> 12096, NA, 9, NA, 10478, 4579, NA, NA, NA, 170~
## $ retweet_location        <chr> "Seattle, WA", NA, "Oslo, Norway", NA, "Toront~
## $ retweet_description     <chr> "Head of data science @saturn_cloud. (co-)wrot~
## $ retweet_verified        <lgl> FALSE, NA, FALSE, NA, FALSE, FALSE, NA, NA, NA~
## $ place_url               <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ place_name              <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ place_full_name         <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ place_type              <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ country                 <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ country_code            <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ geo_coords              <list> <NA, NA>, <NA, NA>, <NA, NA>, <NA, NA>, <NA, ~
## $ coords_coords           <list> <NA, NA>, <NA, NA>, <NA, NA>, <NA, NA>, <NA, ~
## $ bbox_coords             <list> <NA, NA, NA, NA, NA, NA, NA, NA>, <NA, NA, NA~
## $ status_url              <chr> "https://twitter.com/rstudiotips/status/140556~
## $ name                    <chr> "RStudio Tips", "RStudio Tips", "RStudio Tips"~
## $ location                <chr> "Seattle, WA", "Seattle, WA", "Seattle, WA", "~
## $ description             <chr> "Useful features, shortcuts, and ideas for wor~
## $ url                     <chr> "http://t.co/OkBGsvjQ0u", "http://t.co/OkBGsvj~
## $ protected               <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALS~
## $ followers_count         <int> 78722, 78722, 78722, 78722, 78722, 78722, 7872~
## $ friends_count           <int> 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50~
## $ listed_count            <int> 1270, 1270, 1270, 1270, 1270, 1270, 1270, 1270~
## $ statuses_count          <int> 476, 476, 476, 476, 476, 476, 476, 476, 476, 4~
## $ favourites_count        <int> 182, 182, 182, 182, 182, 182, 182, 182, 182, 1~
## $ account_created_at      <dttm> 2014-07-09 16:33:00, 2014-07-09 16:33:00, 201~
## $ verified                <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALS~
## $ profile_url             <chr> "http://t.co/OkBGsvjQ0u", "http://t.co/OkBGsvj~
## $ profile_expanded_url    <chr> "http://www.rstudio.com/", "http://www.rstudio~
## $ account_lang            <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ profile_banner_url      <chr> "https://pbs.twimg.com/profile_banners/2613859~
## $ profile_background_url  <chr> "http://abs.twimg.com/images/themes/theme1/bg.~
## $ profile_image_url       <chr> "http://pbs.twimg.com/profile_images/111148646~

get_favorites()

Cette fonction permet de collecter les derniers tweets aimés par un compte. Dans cet exemple nous collectons les derniers tweets aimés par le compte @rstudio:

twitter_3 <- get_favorites("rstudio")

get_friends()

Cette fonction permet d’obtenir la liste des comptes suivis par le compte spécifié (base 5000). Dans cet exemple, nous cherchons les personnes qui sont suivies par le compte @hadleywickham :

twitter_4 <- get_friends("hadleywickham")
dplyr::glimpse(twitter_4)
## Rows: 273
## Columns: 2
## $ user    <chr> "hadleywickham", "hadleywickham", "hadleywickham", "hadleywick~
## $ user_id <chr> "3959153969", "43875304", "2423861950", "793171723772395521", ~

get_followers()

Cette fonction permet d’obtenir la liste des comptes suivant un compte spécifié (base 5000). Dans cet exemple, nous cherchons les comptes qui suivent le compte @juliasilge :

twitter_5 <- get_followers("juliasilge")
dplyr::glimpse(twitter_5)
## Rows: 5,000
## Columns: 1
## $ user_id <chr> "149145639", "96912396", "1011901140", "14270698", "1345573175~

get_retweeters()

Cette fonction permet d’obtenir une liste des personnes ayant retweeté un tweet (normalement 100). Dans cet exemple, nous cherchons les retweeters d’un tweet spécifique.

twitter_6 <- get_retweeters("1400550230920024065")
dplyr::glimpse(twitter_6)
## Rows: 8
## Columns: 90
## $ user_id                 <chr> "1179447535087190017", "1891739941", "98347019~
## $ status_id               <chr> "1402191414168014849", "1401704358983737345", ~
## $ created_at              <dttm> 2021-06-08 09:10:59, 2021-06-07 00:55:36, 202~
## $ screen_name             <chr> "rashamadj", "nutseba", "R4DScommunity", "ayir~
## $ text                    <chr> "No #TidyTuesday screencast from me this week,~
## $ source                  <chr> "Twitter for Android", "Twitter Web App", "Twi~
## $ display_text_width      <int> NA, NA, NA, NA, NA, NA, NA, NA
## $ reply_to_status_id      <lgl> NA, NA, NA, NA, NA, NA, NA, NA
## $ reply_to_user_id        <lgl> NA, NA, NA, NA, NA, NA, NA, NA
## $ reply_to_screen_name    <lgl> NA, NA, NA, NA, NA, NA, NA, NA
## $ is_quote                <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALS~
## $ is_retweet              <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE
## $ favorite_count          <int> 0, 0, 0, 0, 0, 0, 0, 0
## $ retweet_count           <int> 8, 8, 8, 8, 8, 8, 8, 8
## $ quote_count             <int> NA, NA, NA, NA, NA, NA, NA, NA
## $ reply_count             <int> NA, NA, NA, NA, NA, NA, NA, NA
## $ hashtags                <list> "TidyTuesday", "TidyTuesday", "TidyTuesday", "~
## $ symbols                 <list> NA, NA, NA, NA, NA, NA, NA, NA
## $ urls_url                <list> NA, NA, NA, NA, NA, NA, NA, NA
## $ urls_t.co               <list> NA, NA, NA, NA, NA, NA, NA, NA
## $ urls_expanded_url       <list> NA, NA, NA, NA, NA, NA, NA, NA
## $ media_url               <list> NA, NA, NA, NA, NA, NA, NA, NA
## $ media_t.co              <list> NA, NA, NA, NA, NA, NA, NA, NA
## $ media_expanded_url      <list> NA, NA, NA, NA, NA, NA, NA, NA
## $ media_type              <list> NA, NA, NA, NA, NA, NA, NA, NA
## $ ext_media_url           <list> NA, NA, NA, NA, NA, NA, NA, NA
## $ ext_media_t.co          <list> NA, NA, NA, NA, NA, NA, NA, NA
## $ ext_media_expanded_url  <list> NA, NA, NA, NA, NA, NA, NA, NA
## $ ext_media_type          <chr> NA, NA, NA, NA, NA, NA, NA, NA
## $ mentions_user_id        <list> "13074042", "13074042", "13074042", "13074042"~
## $ mentions_screen_name    <list> "juliasilge", "juliasilge", "juliasilge", "jul~
## $ lang                    <chr> "en", "en", "en", "en", "en", "en", "en", "en"
## $ quoted_status_id        <chr> NA, NA, NA, NA, NA, NA, NA, NA
## $ quoted_text             <chr> NA, NA, NA, NA, NA, NA, NA, NA
## $ quoted_created_at       <dttm> NA, NA, NA, NA, NA, NA, NA, NA
## $ quoted_source           <chr> NA, NA, NA, NA, NA, NA, NA, NA
## $ quoted_favorite_count   <int> NA, NA, NA, NA, NA, NA, NA, NA
## $ quoted_retweet_count    <int> NA, NA, NA, NA, NA, NA, NA, NA
## $ quoted_user_id          <chr> NA, NA, NA, NA, NA, NA, NA, NA
## $ quoted_screen_name      <chr> NA, NA, NA, NA, NA, NA, NA, NA
## $ quoted_name             <chr> NA, NA, NA, NA, NA, NA, NA, NA
## $ quoted_followers_count  <int> NA, NA, NA, NA, NA, NA, NA, NA
## $ quoted_friends_count    <int> NA, NA, NA, NA, NA, NA, NA, NA
## $ quoted_statuses_count   <int> NA, NA, NA, NA, NA, NA, NA, NA
## $ quoted_location         <chr> NA, NA, NA, NA, NA, NA, NA, NA
## $ quoted_description      <chr> NA, NA, NA, NA, NA, NA, NA, NA
## $ quoted_verified         <lgl> NA, NA, NA, NA, NA, NA, NA, NA
## $ retweet_status_id       <chr> "1400550230920024065", "1400550230920024065", ~
## $ retweet_text            <chr> "No #TidyTuesday screencast from me this week,~
## $ retweet_created_at      <dttm> 2021-06-03 20:29:30, 2021-06-03 20:29:30, 2021~
## $ retweet_source          <chr> "Twitter Web App", "Twitter Web App", "Twitter~
## $ retweet_favorite_count  <int> 60, 60, 60, 60, 60, 60, 60, 60
## $ retweet_retweet_count   <int> 8, 8, 8, 8, 8, 8, 8, 8
## $ retweet_user_id         <chr> "13074042", "13074042", "13074042", "13074042"~
## $ retweet_screen_name     <chr> "juliasilge", "juliasilge", "juliasilge", "jul~
## $ retweet_name            <chr> "Julia Silge", "Julia Silge", "Julia Silge", ~
## $ retweet_followers_count <int> 39534, 39534, 39534, 39534, 39534, 39534, 395~
## $ retweet_friends_count   <int> 711, 711, 711, 711, 711, 711, 711, 711
## $ retweet_statuses_count  <int> 22555, 22555, 22555, 22555, 22555, 22555, 225~
## $ retweet_location        <chr> "Salt Lake City, UT", "Salt Lake City, UT", "~
## $ retweet_description     <chr> "Data science and modeling at @rstudio, #rsta~
## $ retweet_verified        <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FAL~
## $ place_url               <chr> NA, NA, NA, NA, NA, NA, NA, NA
## $ place_name              <chr> NA, NA, NA, NA, NA, NA, NA, NA
## $ place_full_name         <chr> NA, NA, NA, NA, NA, NA, NA, NA
## $ place_type              <chr> NA, NA, NA, NA, NA, NA, NA, NA
## $ country                 <chr> NA, NA, NA, NA, NA, NA, NA, NA
## $ country_code            <chr> NA, NA, NA, NA, NA, NA, NA, NA
## $ geo_coords              <list> <NA, NA>, <NA, NA>, <NA, NA>, <NA, NA>, <NA, ~
## $ coords_coords           <list> <NA, NA>, <NA, NA>, <NA, NA>, <NA, NA>, <NA, ~
## $ bbox_coords             <list> <NA, NA, NA, NA, NA, NA, NA, NA>, <NA, NA, NA~
## $ status_url              <chr> "https://twitter.com/rashamadj/status/1402191~
## $ name                    <chr> "almohandis", "Sebastián Pizarro", "R4DS onli~
## $ location                <chr> "Mombasa, Zanzibar", "SCL", "", "San Francisc~
## $ description             <chr> "Reckless and Restless.", "Adventurer, nutriti~
## $ url                     <chr> NA, "http://t.co/KfqOOgTQvG", "https://t.co/Y~
## $ protected               <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FAL~
## $ followers_count         <int> 303, 477, 15939, 2860, 3, 166, 24, 1207
## $ friends_count           <int> 4989, 3385, 1630, 4998, 53, 365, 362, 2
## $ listed_count            <int> 7, 15, 299, 1233, 0, 1, 0, 7
## $ statuses_count          <int> 21570, 20051, 13941, 279676, 334, 281, 132, 15~
## $ favourites_count        <int> 20122, 3027, 13566, 252717, 953, 926, 318, 295
## $ account_created_at      <dttm> 2019-10-02 17:26:32, 2013-09-21 22:41:05, 2018~
## $ verified                <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALS~
## $ profile_url             <chr> NA, "http://t.co/KfqOOgTQvG", "https://t.co/Y~
## $ profile_expanded_url    <chr> NA, "http://about.me/sebastianpizarrocortes", ~
## $ account_lang            <lgl> NA, NA, NA, NA, NA, NA, NA, NA
## $ profile_banner_url      <chr> "https://pbs.twimg.com/profile_banners/1179447~
## $ profile_background_url  <chr> NA, "http://abs.twimg.com/images/themes/theme1~
## $ profile_image_url       <chr> "http://pbs.twimg.com/profile_images/133745525~

stream_tweets()

Cette fonction permet de collecter les tweets en direct sans limites de quantité ou de temps. Si rien n’est spécifié, cette fonction collecte alors 1% des tweets au monde aléatoirement pendant 30 secondes. Il est possible de spécifier des termes de recherche, la langue, une zone géographique ou le temps de collecte (en seconde). Dans notre exemple, nous collectons 1% des tweets au monde pendant 5 secondes:

twitter_7 <- stream_tweets(timeout = 5)
?stream_tweets
dplyr::glimpse(twitter_7)
## Rows: 206
## Columns: 90
## $ user_id                 <chr> "988948602671288323", "633916791", "9327386248~
## $ status_id               <chr> "1406682865341960193", "1406682865371430913", ~
## $ created_at              <dttm> 2021-06-20 18:38:24, 2021-06-20 18:38:24, 202~
## $ screen_name             <chr> "jewelle_royal", "raissagk", "ilikeziggy1", "D~
## $ text                    <chr> "Here comes the think pieces...", "cansada de ~
## $ source                  <chr> "Twitter for Android", "Twitter for iPhone", "~
## $ display_text_width      <dbl> NA, NA, NA, NA, NA, NA, NA, 6, NA, 10, NA, NA,~
## $ reply_to_status_id      <chr> NA, NA, NA, NA, NA, NA, NA, "14066822761430261~
## $ reply_to_user_id        <chr> NA, NA, NA, NA, NA, NA, NA, "11383380532720680~
## $ reply_to_screen_name    <chr> NA, NA, NA, NA, NA, NA, NA, "lwah_maMzi", NA, ~
## $ is_quote                <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALS~
## $ is_retweet              <lgl> FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, F~
## $ favorite_count          <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0~
## $ retweet_count           <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0~
## $ quote_count             <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0~
## $ reply_count             <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0~
## $ hashtags                <list> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ symbols                 <list> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ urls_url                <list> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ urls_t.co               <list> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ urls_expanded_url       <list> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ media_url               <list> NA, NA, NA, NA, NA, NA, "http://pbs.twimg.com~
## $ media_t.co              <list> NA, NA, NA, NA, NA, NA, "https://t.co/Tb7PnO6~
## $ media_expanded_url      <list> NA, NA, NA, NA, NA, NA, "https://twitter.com/~
## $ media_type              <list> NA, NA, NA, NA, NA, NA, "photo", NA, NA, NA, ~
## $ ext_media_url           <list> NA, NA, NA, NA, NA, NA, "http://pbs.twimg.com~
## $ ext_media_t.co          <list> NA, NA, NA, NA, NA, NA, "https://t.co/Tb7PnO6~
## $ ext_media_expanded_url  <list> NA, NA, NA, NA, NA, NA, "https://twitter.com/~
## $ ext_media_type          <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ mentions_user_id        <list> NA, NA, "119363202", "1265658280271122433", <~
## $ mentions_screen_name    <list> NA, NA, "ISOKO_MOCHIZUKI", "DolffR", <"KirenR~
## $ lang                    <chr> "en", "pt", "ja", "pt", "hi", "pt", "und", "un~
## $ quoted_status_id        <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_text             <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_created_at       <dttm> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N~
## $ quoted_source           <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_favorite_count   <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_retweet_count    <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_user_id          <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_screen_name      <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_name             <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_followers_count  <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_friends_count    <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_statuses_count   <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_location         <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_description      <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ quoted_verified         <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ retweet_status_id       <chr> NA, NA, "1406515824035196936", "14066826895712~
## $ retweet_text            <chr> NA, NA, "<U+30E9><U+30B0><U+30D3><U+30FC><U+5354><U+4F1A><U+65B0><U+6E96><U+5099><U+5BA4><U+9577><U+3092><U+89E3><U+4EFB><U+3055><U+308C><U+305F><U+8C37>~
## $ retweet_created_at      <dttm> NA, NA, 2021-06-20 07:34:38, 2021-06-20 18:37~
## $ retweet_source          <chr> NA, NA, "Twitter for iPhone", "Twitter for And~
## $ retweet_favorite_count  <int> NA, NA, 3417, 1, 100, NA, 3198, NA, 11, NA, NA~
## $ retweet_retweet_count   <int> NA, NA, 1383, 2, 25, NA, 137, NA, 5, NA, NA, 1~
## $ retweet_user_id         <chr> NA, NA, "119363202", "1265658280271122433", "3~
## $ retweet_screen_name     <chr> NA, NA, "ISOKO_MOCHIZUKI", "DolffR", "KirenRij~
## $ retweet_name            <chr> NA, NA, "<U+671B><U+6708><U+8863><U+5851><U+5B50>", "Pinscher do Rodolffo Ma~
## $ retweet_followers_count <int> NA, NA, 237800, 1316, 1007484, NA, 19264, NA, ~
## $ retweet_friends_count   <int> NA, NA, 1420, 734, 437, NA, 9, NA, 822, NA, NA~
## $ retweet_statuses_count  <int> NA, NA, 54533, 4057, 21918, NA, 24, NA, 28801,~
## $ retweet_location        <chr> NA, NA, NA, NA, "New Delhi, India", NA, "Campi~
## $ retweet_description     <chr> NA, NA, "<U+65B0><U+805E><U+8A18><U+8005><U+3002><U+5343><U+8449><U+3001><U+57FC><U+7389><U+306A><U+3069><U+5404><U+770C><U+8B66><U+3001><U+6771><U+4EAC>~
## $ retweet_verified        <lgl> NA, NA, FALSE, FALSE, TRUE, NA, FALSE, NA, FAL~
## $ place_url               <chr> NA, "https://api.twitter.com/1.1/geo/id/5b8fdf~
## $ place_name              <chr> NA, "Toledo", NA, NA, NA, NA, NA, "Umlazi", NA~
## $ place_full_name         <chr> NA, "Toledo, Brasil", NA, NA, NA, NA, NA, "Uml~
## $ place_type              <chr> NA, "city", NA, NA, NA, NA, NA, "city", NA, NA~
## $ country                 <chr> NA, "Brasil", NA, NA, NA, NA, NA, "South Afric~
## $ country_code            <chr> NA, "BR", NA, NA, NA, NA, NA, "ZA", NA, NA, NA~
## $ geo_coords              <list> <NA, NA>, <NA, NA>, <NA, NA>, <NA, NA>, <NA, ~
## $ coords_coords           <list> <NA, NA>, <NA, NA>, <NA, NA>, <NA, NA>, <NA, ~
## $ bbox_coords             <list> <NA, NA, NA, NA, NA, NA, NA, NA>, <-54.04585,~
## $ status_url              <chr> "https://twitter.com/jewelle_royal/status/1406~
## $ name                    <chr> "Exhausted Listener", "rairai", "ilikeziggy", ~
## $ location                <chr> "Jamaica", "Toledo, Brasil", NA, NA, "<U+0001F332>Garden~
## $ description             <chr> "Island Girl <U+0001F334>. Virgo <U+264D>. UWI <U+0001F4D2>.\nManchester~
## $ url                     <chr> NA, NA, NA, NA, NA, NA, NA, "https://www.insta~
## $ protected               <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALS~
## $ followers_count         <int> 6680, 2302, 401, 855, 379, 1968, 3, 35692, 205~
## $ friends_count           <int> 3842, 1127, 1057, 1054, 613, 1853, 238, 8859, ~
## $ listed_count            <int> 1, 1, 4, 0, 0, 1, 0, 4, 6, 62, 0, 87, 10, 0, 0~
## $ statuses_count          <int> 43200, 89034, 61099, 9956, 8001, 94182, 61, 31~
## $ favourites_count        <int> 212397, 54588, 58835, 9911, 17499, 35117, 33, ~
## $ account_created_at      <dttm> 2018-04-25 01:11:35, 2012-07-12 15:55:34, 201~
## $ verified                <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALS~
## $ profile_url             <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ profile_expanded_url    <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ account_lang            <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA~
## $ profile_banner_url      <chr> "https://pbs.twimg.com/profile_banners/9889486~
## $ profile_background_url  <chr> "", "http://abs.twimg.com/images/themes/theme1~
## $ profile_image_url       <chr> "http://pbs.twimg.com/profile_images/124117422~

ts_plot()

Cette fonction permet de transformer un tableau de données en graph temporelle facilement utilisable. Ici nous affichons le nombre de tweets par heure du tableau de données twitter_1:

ts_plot(twitter_1, by = "hours")

network_graph()

Cette fonction permet de générer un graphique en réseau d’un tableau de données twitter. Il est possible de spécifier si on s’intéresse aux mentions, au retweets, aux réponses, aux citations ou à plusieurs de ces éléments à la fois:

network_graph(twitter_2) %>% 
  plot()

Note: Il est possible de transformer le tableau de données twitter en tableau de données utile pour l’analyse de réseaux en utilisant cette fois-ci la fonction network_data().