r 使用推文活动数据中最好的9条推文创建一个拼贴画
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r 使用推文活动数据中最好的9条推文创建一个拼贴画相关的知识,希望对你有一定的参考价值。
# code adapted from http://www.masalmon.eu/2017/12/30/best9of2017/
library(purrr)
library(webshot) #webshot::install_phantomjs()
library(magick)
library(dplyr)
library(glue)
# load all the CSV files in the data folder downloaded from https://analytics.twitter.com/user/<username>/tweets
my_tweets <- file.path("data", list.files("data", pattern = "^tweet_activity_metrics_")) %>% map_df(readr::read_csv)
# select the 9 best tweets
my_best_tweets <- my_tweets %>%
mutate(score = retweets + likes / 2 + replies) %>% # custom score, I don't only using the likes
arrange(desc(score)) %>%
slice(1:9)
# take screenchot of the best tweets and save them in temporary files
my_best_tweets_shots_paths <- my_best_tweets %>%
select(`Tweet id`, `Tweet permalink`) %>%
mutate(
webshot_filepath = webshot(
url = `Tweet permalink`,
file = rerun(9, tempfile(pattern = "shot_", fileext = ".png")) %>% unlist(),
cliprect = c(0, 150, 650, 650)
)
)
# collage is made column by column
# then the columns are assembled in one image
make_col <- function(path, color, geometry) {
tmp <- tempfile(fileext = ".png")
image_read(path) %>%
image_border(color = color, geometry = geometry) %>%
image_append(stack = TRUE) %>% # append pictures vertically
image_write(tmp)
return(tmp)
}
my_best_tweets_shots_paths %>%
mutate(column = rep(1:3, 3)) %>% # assign each tweet to a column
group_by(column) %>%
summarise(col_image = make_col(webshot_filepath, color = "#777777", geometry = "30x30")) %>%
pull(col_image) %>%
image_read() %>%
image_append(stack = FALSE) %>% # append column pictures horizontally
image_border(color = "#777777", geometry = "30x30") %>%
image_write("best9of2017.png")
以上是关于r 使用推文活动数据中最好的9条推文创建一个拼贴画的主要内容,如果未能解决你的问题,请参考以下文章
使用 TweetSharp 返回最近的 n 条推文
为啥每条推文都被删除
设置 Twitter API,获取最后几条推文
绘制 R 中分类变量随时间的出现情况
试图读取存储在csv文件中的R中的推文
如何将每条推文分成不同的引导卡