Julia has a few peculiarities in string indexing, an area generally blissfully devoid of such gotchas in most programming languages. Some of these can cause quite unpleasant results, and as such it’s helpful to have a good understanding of how string indices work. The other day, I have helped someone through a bit of code […]
Tag Archives: R
Vega.jl Rebooted – Now with 100% More Pie and Donut Charts!
Re-posted from: http://randyzwitch.com/vega-jl-julia/
Mmmmm, chartjunk!
Rebooting Vega.jl
Recently, I’ve found myself without a project to hack on, and I’ve always been interested in learning more about browser-based visualization. So I decided to revive the work that John Myles White had done in building Vega.jl nearly two years ago. And since I’ll be giving an analytics & visualization workshop at JuliaCon 2015, I figure I better study the topic in a bit more depth.
Back In Working Order!
The first thing I tackled here was to upgrade the syntax to target v0.4 of Julia. This is just my developer preference, to avoid using Compat.jl when there are so many more visualizations I’d like to support. So if you’re using v0.4, you shouldn’t see any deprecation errors; if you’re using v0.3, well, eventually you’ll use v0.4!
Additionally, I modified the package to recognize the traction that Jupyter Notebook has gained in the community. Whereas the original version of Vega.jl only displayed output in a tab in a browser, I’ve overloaded the writemime method to display :VegaVisualization inline for any environment that can display HTML. If you use Vega.jl from the REPL, you’ll still get the same default browser-opening behavior as existed before.
The First Visualization You Added Was A Pie Chart…
…And Followed With a Donut Chart?
Yup. I’m a troll like that. Besides, being loudly against pie charts is blowhardy (even if studies have shown that people are too stupid to evaluate them).
Adding these two charts (besides trolling) was a proof-of-concept that I understood the codebase sufficiently in order to extend the package. Now that the syntax is working for Julia v0.4, I understand how the package works (important!), and have improved the workflow by supporting Jupyter Notebook, I plan to create all of the visualizations featured in the Trifacta Vega Editor and other standard visualizations such as boxplots. If the community has requests for the order of implementation, I’ll try and accommodate them. Just add a feature request on Vega.jl GitHub issues.
Why Not Gadfly? You’re Not Starting A Language War, Are You?
No, I’m not that big of a troll. Besides, I don’t think we’ve squeezed all the juice (blood?!) out of the R vs. Python infographic yet, we don’t need another pointless debate.
My sole reason for not improving Gadfly is just that I plain don’t understand how the codebase works! There are many amazing computer scientists & developers in the Julia community, and I’m not really one of them. I do, however, understand how to generate JSON strings and in that sense, Vega is the perfect platform for me to contribute.
Collaborators Wanted!
If you’re interested in visualization, as well as learning Julia and/or contributing to a package, Vega.jl might be a good place to start. I’m always up for collaborating with people, and creating new visualizations isn’t that difficult (especially with the Trifacta examples). So hopefully some of you will be interested in enough to join me to adding one more great visualization library to the Julia community.
Julia style string literal interpolation in R
By: Francis Smart
Re-posted from: http://www.econometricsbysimulation.com/2014/10/julia-style-string-literal.html
I hate to say it but I feel that I have become somewhat infatuated with Julia. And infatuation is the right word. I have not yet committed the time to fully immerse myself in the language, yet everything I know about it makes me want to learn more. The language is well known for its mind-blowingly speed accomplished through just-in-time compiling. It also has many features which enhance the efficiency and readability of its code (see previous post, note the documentation has greatly improved since posting).
However, though I very much want to, I cannot entirely switch my coding needs from R into Julia. This is primarily due to my ongoing usage of packages such as RStudio’s “Shiny” and the University of Cambridge’s server side software for building adaptive tests, “Concerto”. And so with regret I will resign my Julia coding to probably a minor portion of my programming needs.
That does not mean however that I can’t make some small changes to make R work more like Julia. To this end I have programmed a small function p which will replace string literals identified as “Hello #(name), how are you?” with their values being evaluated. If there are nested parenthesizes then it is necessary to close the literal with “)#”, for example “c=#(b^(1+a))#”.
# Julia like text concaction function.
p <- function(..., sep="", esc="#") {
# Change escape characters by specifying esc.
# Break the input values into different strings cut at '#('
x <- paste(..., sep=sep)
x <- unlist(strsplit(x, paste0(esc,"("), fixed = TRUE))
# The first element is never evaluated.
out <- x[1]
# Check if x has been split.
if (length(x)>1) for (i in 2:length(x)) {
y <- unlist(strsplit(x[i], paste0(")",esc), fixed = TRUE))
if (x[i]==y[1])
y <- unlist(regmatches(x[i], regexpr(")", x[i]),
invert = TRUE))
out <- paste0(out, eval(parse(text=y[1])), y[-1])
}
out
}
name="Bob"
height=72
weight=230
# Let's see it in action
p(sep=" ", "Hello #(name).",
"My record indicates you are #(height) inches tall and weigh #(weight) pounds.",
"Your body mass index is #(round(703*weight/height^2,1))#")
# [1] "Hello Bob. My record indicates you are 72 inches tall and weigh 230 pounds.
# Your body mass index is 31.2"
# The other nice thing about the p function is that it can be used to concat
# strings as a shortcut for paste0.
p("QRS","TUV")
# [1] "QRSTUV"
Created by Pretty R at inside-R.org
Thank you SO community for your help.