Skip to content

Instantly share code, notes, and snippets.

@rgrannell1
Created January 2, 2013 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgrannell1/4431698 to your computer and use it in GitHub Desktop.
Save rgrannell1/4431698 to your computer and use it in GitHub Desktop.
require(ggplot2)
require(grid)
shinyServer(function(input, output) {
parameters <- NULL
processs_follow <- function(input){
if(input$user_ids != ''){
paste0('follow=', gsub(' ', '', input$user_ids))
} else NULL
}
process_track <- function(input){
track_all <- if(input$track_terms_all != ''){
gsub(',', ' ', gsub(' ', '', input$track_terms_all))
}
track_any <- if(input$track_terms_any != ''){
gsub(' ', '', input$track_terms_any)
}
track_words <- NULL
if(length(track_any) > 0 && length(track_all) > 0){
track_words <- paste0(track_all, ',', track_any)
} else if(length(track_any) > 0){
track_words <- track_any}
else if(length(track_all) > 0) track_words <- track_all
if(length(track_any) + length(track_all) > 0){
paste0('track=', track_words)
}else NULL
}
process_locations <- function(input){
if(input$coordinates != ''){
paste0('locations=', gsub(' ', '', input$coordinates))
} else NULL
}
output$user_id <- reactiveText(function(){
require(twitteR)
tmp <- if(input$user_name != ''){
getUser(input$user_name)$id
}
if(!is.null(tmp)) tmp else 'user not found'
})
output$bounding_box <- reactiveText(function(){
paste0(c(input$lon_range[1], input$lat_range[1],
input$lon_range[2], input$lat_range[2]), collapse = ',')
})
output$query_string <- reactiveText(function(){
url <- switch(
input$endpoint,
'Advanced' = 'https://stream.twitter.com/1.1/statuses/filter.json?',
'Simple' = 'https://stream.twitter.com/1.1/statuses/sample.json?')
if(input$stall_warnings) parameters <- c(parameters, 'stall_warnings=true')
if(input$delimited) parameters <- c(parameters, 'delimited=length')
follow <- processs_follow(input)
track <- process_track(input)
locations <- process_locations(input)
url <- paste0(url, paste0(c(follow, track, locations, parameters), collapse = '&'))
return(url)
})
output$map <- reactivePlot(function(){
require(ggplot2)
map <- map_data('world')
map <- cbind(map,
c(map[,2] > input$lat_range[1] &
map[,2] < input$lat_range[2] &
map[,1] > input$lon_range[1] &
map[,1] < input$lon_range[2]))
names(map)[length(names(map))] <- 'colour'
plot(ggplot(data = map, legend=FALSE) + geom_polygon(
aes(x=long, y=lat, group=group, colour = colour),
size = 0.1, fill = '#A6A6A6') +
geom_line(data=
data.frame(x=c(-180, 180), y=rep(input$lat_range[1], 2)),
aes(x=x, y=y), linetype=2) +
geom_line(data=
data.frame(x=c(-180, 180), y=rep(input$lat_range[2], 2)),
aes(x=x, y=y), linetype=2) +
geom_line(data=
data.frame(x=rep(input$lon_range[1], 2), y=c(-90, 90)),
aes(x=x, y=y), linetype=2) +
geom_line(data=
data.frame(x=rep(input$lon_range[2], 2), y=c(-90, 90)),
aes(x=x, y=y), linetype=2) +
scale_colour_manual(
values = c('#B4CDCD', '#37FDFC')) +
theme(
axis.line = theme_blank(),
axis.text.x = theme_blank(),
axis.text.y = theme_blank(),
axis.ticks = theme_blank(),
axis.title.x = theme_blank(),
axis.title.y = theme_blank(),
panel.background = theme_blank(),
panel.border = theme_blank(),
panel.grid.major = theme_blank(),
panel.grid.minor = theme_blank(),
plot.background = theme_blank(),
legend.position = "none"
))
})
})
shinyUI(pageWithSidebar(
headerPanel('Streaming Search Tool'),
sidebarPanel(
(function(){
# three buttons to select the twitter endpoint
wellPanel(radioButtons(
"endpoint", "Streaming API Endpoint",
choices = c("Simple", "Advanced")))
})(),
conditionalPanel(
"input.endpoint == 'Advanced'",
wellPanel('Lookup User',
(function(){
textInput('user_name' , 'User Name', '')
})(),
(function(){
verbatimTextOutput('user_id')
})()
),
wellPanel('Bounding Box',
plotOutput('map'),
(function(){
# a slider to pick longitudes
sliderInput(
'lon_range', 'longitude', -180, 180, c(-180, 180), 0.1)
})(),
(function(){
# a slider to pick latitudes
sliderInput(
'lat_range', 'latitude', -90, 90, c(-90, 90), 0.1)
})(),
(function(){
verbatimTextOutput('bounding_box')
})()
))
),
mainPanel('Query',
(function(){
# print the query as it is being formed
verbatimTextOutput('query_string')
}
)(),
(function(){
conditionalPanel(
"input.endpoint == 'Advanced'",
wellPanel('Fields',
textInput('user_ids', "User Id's to Follow", ''),
textInput('track_terms_all', "All These Words", ''),
textInput('track_terms_any', "Any of These Words", ''),
(function(){
textInput('coordinates', "Any of These Bounding Boxes", '')
})()
))
})(),
(function(){
# adds the 'stall-warnings' option
checkboxInput(inputId='stall_warnings',
label='give warnings if client falls behind?', value=FALSE)
})(),
(function(){
# adds the 'delimited' option
checkboxInput(inputId='delimited',
label='add delimiters to the tweet stream?', value=FALSE)
})()
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment