Skip to content

Instantly share code, notes, and snippets.

@eliocamp
Last active October 10, 2022 16:02
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliocamp/e22bfd3912196d2403af6c558168a1a4 to your computer and use it in GitHub Desktop.
Save eliocamp/e22bfd3912196d2403af6c558168a1a4 to your computer and use it in GitHub Desktop.
Example of posting a twitter thread from R
---
title: "Tweet thread"
author: "Elio Campitelli"
output: github_document
---
```{r}
knitr::opts_chunk$set(dev = "png",
tweet_this = TRUE)
prev_status_id <- NULL
tweet_this <- function(before, options, envir) {
if (isFALSE(before) & isTRUE(options$tweet_this)) {
figure <- paste(options$fig.path, options$label, "-1.png", sep = '')
if (!file.exists(figure)) {
figure <- NULL
}
rtweet::post_tweet(status = options$tw_status,
media = figure,
in_reply_to_status_id = get("prev_status_id", envir = .GlobalEnv),
auto_populate_reply_metadata = TRUE)
my_timeline <- rtweet::get_timeline(rtweet:::home_user())
assign("prev_status_id", my_timeline$status_id[1], envir = .GlobalEnv)
}
}
knitr::knit_hooks$set(tweet_this = tweet_this)
```
```{r, tw_status = "Hey, #rstats tweeps! I'm tweeting this, not only from R, but from a markdown document! It's kind of a hack, but allows you to tweet out threads (with plots!). Read on!"}
knitr::include_graphics("test_tw_files/figure-gfm/screencap.png")
```
```{r, tw_status = "My idea is to streamline #tidytuesday / #datosdemiercoles work and to create a tweet thread with your results as you are rendering your analysis."}
```
```{r, tweet_this = FALSE}
library(data.table)
library(ggplot2)
library(magrittr)
buenos_aires <- fread("https://raw.githubusercontent.com/cienciadedatos/datos-de-miercoles/master/datos/2019/2019-06-05/buenos_aires.csv")
cdmx <- fread("https://raw.githubusercontent.com/cienciadedatos/datos-de-miercoles/master/datos/2019/2019-06-05/cdmx.csv")
rio <- fread("https://raw.githubusercontent.com/cienciadedatos/datos-de-miercoles/master/datos/2019/2019-06-05/rio.csv")
santiago <- fread("https://raw.githubusercontent.com/cienciadedatos/datos-de-miercoles/master/datos/2019/2019-06-05/santiago.csv")
airbnb <- rbindlist(
list(ba = buenos_aires,
cdmx = cdmx,
rio = rio,
santiago = santiago),
idcol = "ciudad" )
ppp <- fread("02 - AirBnB/ppp_rates.csv")
paises <- c("ARG", "CHL", "BRA", "MEX")
ppp <- ppp[LOCATION %in% paises] %>%
.[, .(tasa = Value[TIME == max(TIME)]), by = .(pais = LOCATION)] %>%
.[, ciudad := c("cdmx", "rio", "santiago", "ba")]
ppp[]
airbnb[, precio_ppp := precio/ppp[ciudad == .BY$ciudad, tasa], by = ciudad]
```
```{r, tw_status = "For example, you could be analysing the latest #datosdemiercoles dataset and created this plot. If you want to share it, just add a small text on the same knitr chunk and the text you want to tweet (e.g. this one)."}
airbnb %>% ggplot(aes(precio_ppp)) +
geom_density() +
scale_x_log10("Precio de AirBnB") +
labs(title = "Price distribution of AirBnB prices (PPP dolars)",
subtitle = "in Buenos Aires, Santiago, Mexico City and Río de Janeiro")
```
```{r, tw_status = "The sourcecode for this thread can be found in this gist: https://gist.github.com/eliocamp/e22bfd3912196d2403af6c558168a1a4. \n #rstats"}
```
```{r, tw_status = "PS: Due to a small bug in the rtweet package, this won't work until this PR is merged! https://gist.github.com/eliocamp/e22bfd3912196d2403af6c558168a1a4 :( "}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment