What if I made an R command center for tweeting and tooting?

social
twitter
mastodon
Messing around with stuff in R on a friday Morning
Author

Matt Crump

Published

October 28, 2022

Modified

June 26, 2023

I am multivalent about social media. And, I’m not going to hash out those valences right now.

Instead, in addition to twitter (where I have gone through a few account deletion cycles, @MattCrumpLab), I just joined Mastodon @MattCrumpLab@fosstodon.org. I’m not sure for how long, for either place.

I remember feeling happy and sad when deleting twitter accounts. I did not take precautions to save or archive some of the content that I deleted, and I wish I did. If I had sent all those tweets from a personal central command center, then I maybe I would still have them.

Anyway, let’s get on with business. My purpose here is to nerd-out and use R to send tweets or toots, or both. This way I’m thinking about a content curation strategy, and thinking about what deploying the content on various platforms means etc.

Can I send a tweet with R?

Looks like tweeting from R can be done with https://github.com/ropensci/rtweet.

This worked, and it was a bit of pain to wade through the twitter developer authentication process.

library(rtweet)

auth <- rtweet_bot()

rtweet::post_tweet(status = "Testing out rtweet::post_tweet to see if I can post from Rstudio.", token=auth)

Some notes to self about what I did. The {rtweet} documentation was helpful.

  1. Make a twitter developer account
  2. Register an app
  3. Request elevated status
  4. Give the app access to my account
  5. Save the API key and secret, and bearer token
  6. Generate token and secret for user account access.

All this took about an hour of messing around. I was using the wrong authentication methods for trying to post a tweet. rtweet_bot() triggers text input prompts to enter all of the keys and secrets, and these can be saved for use in future sessions.

rtweet::auth_as(auth)
my_timeline <- get_my_timeline(token=auth)

reply_id <- my_timeline[10,]$id_str

library(tidyverse)
library(ggrepel)

tmp <- starwars %>%
  filter(mass < 1000) %>%
ggplot(aes(x=height,y=mass,label=name))+
  geom_text_repel()+
  geom_text()

ggsave("starwars.png",plot=tmp)

post_tweet("proof of ggplot for @vuorre", 
           media = "starwars.png", 
           media_alt_text = "A plot of star wars characters by height and mass.",
           in_reply_to_status_id = reply_id,
           token = auth)

post_tweet("rtweet works, took a while to set up. \n Requiring alt_text for media is a nice nudge", token=auth)

Interesting. Mission accomplished. Still have to figure out more complicated stuff like posting threads, etc. Moving on to tooting at Mastodon.

Can I toot with R?

I will try to do this with mastodon client for R.

library(mastodon)

token <- login('https://fosstodon.org/', my_email, my_password)

post_status(token, 'This is a test post from an R console, using the mastodon client for R.')

That was easy.

What’s next

Time to go outside and do other stuff. Leaving these breadcrumbs here for later.

Also, I could just use this cross-posting service https://crossposter.masto.donte.com.br