Skip to content

Instantly share code, notes, and snippets.

@sckott
Created June 10, 2013 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sckott/5751445 to your computer and use it in GitHub Desktop.
Save sckott/5751445 to your computer and use it in GitHub Desktop.
require(shiny)
shinyServer(function(input, output){
theData <- reactive( {
require(rbison)
print("enterData")
print(input$common)
print(input$species)
if (is.null(input$species)) return(NULL)
# if (input$common=="") return(NULL)
print(input$species)
df.init <- bison(species = input$species, type = "scientific_name", start = 0, count = 20) # big numbers cause errors
df <- bison(species = input$species, type = "scientific_name", start = 0, count = df.init$georeferenced)
# print("df")
# print(head(df))
print("extractgeo")
geo <- bison_data(df, "data_df")
# print("geo")
# print(geo)
geo$latitude <- as.numeric(as.character(geo$latitude))
geo$longitude <- as.numeric(as.character(geo$longitude))
geo$latlong <- paste0(geo$latitude,":",geo$longitude)
#print("looks good")
# print(geo)
# state info
print(df$counties$total)
if (df$counties$total >0) {
states <- bison_data(df, datatype = "counties")
# print(states)
states$total <- as.integer(states$total)
# there are ties for max so cannot do ddply on matching - transform and then take first for
# each on ordered
states.summary <- ddply(states,"state", transform, tot=sum(total),max=max(total))
states.summary <- arrange(states.summary,desc(total))
temp <- ddply(states.summary,"state", function(x) c(county_name=x$county_name[1]) )
#prob a simpler way but can then do merge
df.states <- merge(temp,states.summary,by=c("state","county_name"),all.x=TRUE)[,c(1,5,2,4)]
df.states <- arrange(df.states,desc(tot))
# print(df.states)
colnames(df.states) <- c('State','Total','Top','County')
} else {
df.states <- data.frame(State="No Data",Total=0,Top=0,County="")
}
print("dfstates")
print(df.states)
## sighting methods get rid of centroid
method <- bison_data(df)
method["centroid"] <- NULL
print(method)
#plotHeight=200
info=list(df=df,geo=geo,df.states=df.states,method=method) #plotHeight=plotHeight
return(info)
print("info returned")
})
output$locationPlot <- renderPlot( {
if (is.null(input$species)) return(NULL)
if (theData()$df$georeferenced==0) return(NULL)
# print(theData()$df)
bisonmap(theData()$df)
})
})
require(shiny)
shinyUI(pageWithSidebar(
headerPanel(""),
sidebarPanel(
# tags$head(
# tags$link(rel = 'stylesheet', type = 'text/css', href = 'styles.css'),
# tags$link(rel = 'stylesheet', type = 'text/css', href = 'appStyles.css') # yet to get working
#
# ),
p("The", a('rOpenSci initiative', href='http://ropensci.org/', id="link")," have developed an easy method to access
", a('US Geological Survey', href='http://bison.usgs.ornl.gov/', id="link")," records for
more than 70,000 species of flora and fauna in the USA"),
p("Enter a common name, press the button, select from the
options provided and re-press"),
p("Be as precise as possible e.g 'Black bear' gives 4
alternatives, 'bear' more than 800"),
textInput("common", "Enter Common Name (can be slow response)"),
uiOutput("selection"),
submitButton("Go"),
# actionButton("goButton","Go"),
p(),
htmlOutput("image"),
uiOutput("imageText")
),
mainPanel(
tabsetPanel(
tabPanel("State Map",
h4(textOutput("stateCaption")),
plotOutput("statePlot"),
h5(textOutput("stateInfo")),
value = 1),
tabPanel("Tables",
tableOutput("method"),
tableOutput("summary"),
value=2),
tabPanel("County Map (be patient!)",
h4(textOutput("countyCaption")),
plotOutput("countyPlot"),
h5(textOutput("countyInfo")),
value = 3),
tabPanel("Location (inc. Alaska)",
h4(textOutput("locationCaption")),
plotOutput("locationPlot"),
h5(textOutput("locationInfo")),
value = 4),
tabPanel("Google Map (inc. Alaska)",
h4(textOutput("gvisCaption")),
htmlOutput("gvisPlot"),
h5(textOutput("gvisInfo")),
value = 5),
tabPanel("Notes",
# img(src="blankMap.png"), testing and oes work
HTML("<h5>Definitions</h5
<ul>
<li>Fossil: Petrified evidence of a species occurrence in geological time </li>
<li>Specimen: The species or a part of it has been collected from this location and preserved in a formal collection</li>
<li>Germplasm: Living tissue from which new organisms can be grown </li>
<li>Literature: Assertion in a scientific publication of an occurrence </li>
<li>Living: Organism kept in captivity at the given location </li>
<li>Observation: A free-living species occurrence that does not produce a specimen or germplasm </li>
</ul>
<br>
Data may have been collected over many decades
Scientific studies in a particular region may bias results
<br><br>
R packages used: shiny, rbison, taxize_, rjson, XML, googleVis, plyr, stringr
<br><br>
Special thanks to Scott Campbell of OpenSci
"),
value = 6),
id="tabs1")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment