Skip to content

Instantly share code, notes, and snippets.

@fredbenenson
Created June 20, 2019 20:13
Show Gist options
  • Save fredbenenson/83149e7f320ac6eaf0e113554612dc57 to your computer and use it in GitHub Desktop.
Save fredbenenson/83149e7f320ac6eaf0e113554612dc57 to your computer and use it in GitHub Desktop.
Tweet Explore
# 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