What if I made an R command center for tweeting and tooting?
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)
<- rtweet_bot()
auth
::post_tweet(status = "Testing out rtweet::post_tweet to see if I can post from Rstudio.", token=auth) rtweet
Some notes to self about what I did. The {rtweet} documentation was helpful.
- Make a twitter developer account
- Register an app
- Request elevated status
- Give the app access to my account
- Save the API key and secret, and bearer token
- 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.
::auth_as(auth)
rtweet<- get_my_timeline(token=auth)
my_timeline
<- my_timeline[10,]$id_str
reply_id
library(tidyverse)
library(ggrepel)
<- starwars %>%
tmp 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)
<- login('https://fosstodon.org/', my_email, my_password)
token
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