Sorted HTML Tables and Javascript Libraries
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
A few days ago StatsInTheWild asked the following question
In the R function “sortable.html.table”, it's sorting 9.2 higher than 30.5 because of the leading digit. How do I fix this?
— SITW (@StatsInTheWild) July 23, 2014
So we had a few exchanges where I thought you could use sprintf
and be done but it didn't seem to work:
@StrictlyStat It still sorts them based on the first number. I think it's a problem in the function sortable.html.table.
— SITW (@StatsInTheWild) July 23, 2014
After a bit more discourse, StatsInTheWild shared some data with me:
@StrictlyStat Data link https://t.co/sW6II3d19A
— SITW (@StatsInTheWild) July 23, 2014
and I went down the rabbit hole of trying to find out what was going on. Here is the code to make the table:
require(SortableHTMLTables) myfile = "openWAR.csv" if (!file.exists(myfile)) { download.file("https://dl.dropboxusercontent.com/u/35094868/openWAR.csv", myfile, method="wget") } openWAR<-read.csv(myfile, stringsAsFactors = FALSE); sortable.html.table(openWAR,"openWAR2014.html")
And as you can see in the output table here the column RAA.pitch
does not sort correctly.
Attempts at, and then finding, a Solution
I tried a few things such as changing numeric to string, seeing if missing data was a problem, trying some things where I make the numbers all positive, but the problem persisted.
As StackOverflow usually does, it had insight into an answer. Essentially, prior to version 2.0.5, jquery.tablesorter.js didn't sort numbers exactly correctly. The problem is that SortableHTMLTables
ships with version 2.0.3:
head(readLines(system.file("assets/jquery.tablesorter.js", package = "SortableHTMLTables"))) [1] "/*" [2] " * " [3] " * TableSorter 2.0 - Client-side table sorting with ease!" [4] " * Version 2.0.3" [5] " * @requires jQuery v1.2.3" [6] " * "
and uses this version for the table output. Now, if you wanted to fix this, you'd have some css to your file or some other route. Or, you can just update jquery.tablesorter.js. I went to http://tablesorter.com/docs/, downloaded the new js plugin.
But I want this automatic!
If you're using R
and don't want to play around with JavaScript, that's the whole point of these functions. Saying you have to edit css or something of the like defeats that purpose. But for this fix to be automatically'' done, you either have to 1) copy the .js file every time you run the
sortable.html.table command as it re-copies the files over, 2) wait for the maintainer to update (out of your control), 3) change your css, or 4) copy a new .js file with different name and edit the html file after running to make sure it uses your new js file. I'll implement 4).
require(SortableHTMLTables) outfile = "openWAR2014_fixed.html" sortable.html.table(openWAR,outfile) change_js = function(f, newjs = "jquery.tablesorter_v2.0.5.js"){ x = readLines(f) x = gsub("jquery.tablesorter.js", newjs, x, fixed=TRUE) writeLines(x, con = f) } change_js(outfile)
(I named my file jquery.tablesorter_v2.0.5.js
). Now, you see here, the table works! Hope this helps.
Maintainer
Note, I contacted the maintainer and I'm sure he'll fix this in the next update (he does a LOT of awesome work and development).
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.