Skip to content

Instantly share code, notes, and snippets.

@amsantac

amsantac/app.R Secret

Last active March 13, 2019 09:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save amsantac/9e25680ccbbccfa5a25adced04dc9e04 to your computer and use it in GitHub Desktop.
Save amsantac/9e25680ccbbccfa5a25adced04dc9e04 to your computer and use it in GitHub Desktop.
Shiny web app for retrieving geocoded tweets and displaying them in a leaflet map
library(leaflet)
library(twitteR)
shinyApp(
ui = fluidPage(
fluidRow(
column(4, textInput("searchkw", label = "search:", value = "#dinner")),
column(4, textInput("lat", label = "latitude:", value = 40.75)),
column(4, textInput("long", label = "longitude:", value = -74)),
column(8, leafletOutput("myMap")),
column(12, tableOutput('table'))
)
),
server = function(input, output) {
# OAuth authentication
consumer_key <- readLines("tokens.txt")[1]
consumer_secret <- readLines("tokens.txt")[2]
access_token <- readLines("tokens.txt")[3]
access_secret <- readLines("tokens.txt")[4]
options(httr_oauth_cache = TRUE) # enable using a local file to cache OAuth access credentials between R sessions
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
# Issue search query to Twitter
dataInput <- reactive({
tweets <- twListToDF(searchTwitter(input$searchkw, n = 100,
geocode = paste0(input$lat, ",", input$long, ",10km")))
tweets$created <- as.character(tweets$created)
tweets <- tweets[!is.na(tweets[, "longitude"]), ]
})
# Create a reactive leaflet map
mapTweets <- reactive({
map = leaflet() %>% addTiles() %>%
addMarkers(as.numeric(dataInput()$longitude), as.numeric(dataInput()$latitude), popup = dataInput()$screenName) %>%
setView(input$long, input$lat, zoom = 11)
})
output$myMap = renderLeaflet(mapTweets())
# Create a reactive table
output$table <- renderTable(
dataInput()[, c("text", "screenName", "longitude", "latitude", "created")]
)
}
)
@duguigis
Copy link

Excelente, gracias por compartir, estoy siguiendo los pasos de tu artículo http://amsantac.co/blog/es/2016/05/28/twitter-r-es.html, saludos desde Caracas - Venezuela.

@waltergkaturuza
Copy link

thank you sir for the code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment