Created
June 20, 2019 20:13
-
-
Save fredbenenson/83149e7f320ac6eaf0e113554612dc57 to your computer and use it in GitHub Desktop.
Tweet Explore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install.packages("mongolite") | |
require("mongolite") | |
require("tidyverse") | |
require("lubridate") | |
library(DT) | |
connection <- mongo(collection = "tweets", db = "tweets", url = "mongodb://localhost") | |
query <- '{ | |
"$and": [ | |
{ "full_text": { "$not": { "$regex": "^RT ", "$options": "i" } } } | |
] | |
}' | |
tweets <- as_tibble(connection$find( | |
query = query, | |
fields = '{ | |
"_id": false, | |
"created_at": true, | |
"full_text": true, | |
"favorite_count": true, | |
"retweet_count": true | |
}' | |
)) %>% | |
filter(created_at >= as.Date('2010-01-01')) | |
tweets$year <- format(tweets$created_at, "%Y") | |
ui <- fluidPage( | |
mainPanel( | |
plotOutput("graph", | |
height = 600, | |
brush = brushOpts(id = "plot_brush") | |
), | |
DT::dataTableOutput("table"), | |
width = 12 | |
) | |
) | |
server <- function(input, output) { | |
output$table = DT::renderDataTable({ | |
t <- brushedPoints(tweets, input$plot_brush) | |
DT::datatable(t) | |
}) | |
output$graph <- renderPlot({ | |
ggplot(tweets, aes(x = retweet_count, y = favorite_count, color = year)) + | |
geom_jitter() + | |
geom_count() + | |
scale_x_log10() + | |
scale_y_log10() + | |
geom_abline(slope = 1) + | |
facet_wrap( ~ year) | |
}) | |
} | |
shinyApp(ui, server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment