Skip to content

Instantly share code, notes, and snippets.

@atbradley
Created May 2, 2013 20:56
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 atbradley/5505405 to your computer and use it in GitHub Desktop.
Save atbradley/5505405 to your computer and use it in GitHub Desktop.
Generates the HTML necessary to create a map using Stately (http://intridea.github.io/stately/). stately.csv contains state names and abbreviations with the correct letter for the Stately font.
#States is a data.frame with at least the following columns:
# $color: Any string identifiable as a color by CSS (It's included in a `style` attribute.
# $letter: The letter corresponding to this state in the Stately font.
#
# states$id and states$class will be used as the `id` and `class`
# attributes of the list elements for the states. All other columns
# will be inserted as data-<column-name> attributes (in case you want
# to use them with JavaScript).
statelify <- function(states) {
#TODO: color palette handling should be here (maybe).
sp <- "\n"
req.cols <- c('letter', 'color')
outp <- '<ul class="stately">'
#Ever get the feeling you're being really stupid and there's a much easier way?
extraAtts <- colnames(states)[!(colnames(states) %in% req.cols)]
for ( x in 1:length(states[,1]) ) {
ln <- sprintf('<li style="color:%s;"', states$color[x])
if ( length(extraAtts) > 0 ) {
for( att in extraAtts ) {
attName <- gsub('\\.', '-', att)
if ( !(attName %in% c('class', 'id')))
attName <- paste('data-', attName, sep='')
ln <- sprintf('%s %s="%s"', ln, attName, states[x, att])
}
}
ln <- sprintf('%s>%s</li>', ln, states$letter[x])
outp <- paste(outp, ln, sep=sp)
}
paste(outp, '</ul>', sep=sp)
}
state abbr letter
alabama al A
alaska ak B
arkansas ar C
arizona az D
california ca E
colorado co F
connecticut ct G
delaware de H
district of columbia dc I
florida fl J
georgia ga K
hawaii hi L
idaho id M
illinois il N
indiana in O
iowa ia P
kansas ks Q
kentucky ky R
louisiana la S
maine me T
maryland md U
massachusetts ma V
michigan mi W
minnesota mn X
mississippi ms Y
missouri mo Z
montana mt a
nebraska ne b
nevada nv c
new hampshire nh d
new jersey nj e
new mexico nm f
new york ny g
north carolina nc h
north dakota nd i
ohio oh j
oklahoma ok k
oregon or l
pennsylvania pa m
rhode island ri n
south carolina sc o
south dakota sd p
tennessee tn q
texas tx r
utah ut s
virginia va t
vermont vt u
washington wa v
west virginia wv w
wisconsin wi x
wyoming wy y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment