Who Touched My Shiny-app?

February 11, 2014

(This article was first published on Category: R | Huidong Tian's Blog, and kindly contributed to R-bloggers)

When we created a Shiny-app, deployed it on a server and open it to public, we must have interesting of who visited our app, and if possible, where they from and what they have done. To achieve this, we need a user behavior tracking feature if we are not using the commercial version: Shiny-server pro. I wrote a JavaScript code to conduct such operation, and would like to share with you.

The idea of the JavaScript code is:

  1. Trigger action when user change inputs.
  2. Catch the changed object, get its value and label, and put it in a Shiny input.
  3. Export the value stored in above input to local file.

So, we need some JavaScript to catch user’s behavior and store the value and label to the new created Shiny input.

“` ruby
// Who touched my Shiny-app?
$(document).on(“change”, “.shiny-bound-input:not([id=’tracking’])”, function(evt) {
var el = $(evt.target);
if (el.prop(“tagName”).toLowerCase() === “select”) {
value = $(“option:selected”, el).map(function(){ return this.text }).get().join(“, “);
label = el.prev(“label”).text();
} else if (el.attr(“type”) === “checkbox”) {
value = el.attr(“checked”);
label = el.parent().text();
} else if (el.attr(“type”) === “radio”) {
value = el.next().text();
label = $(“.control-label”, el.parent().parent()).text();
} else if (el.attr(“type”) === “text”) {
value = el.val();
label = el.prev(“label”).text();
if (el.attr(“class”) === “input-small”) {
label = label + el.index()
} else if (el.attr(“type”) === “number”) {
value = el.val();
label = el.prev(“label”).text();
label = label.replace(/\n|\r/gm,””);
label = label.replace(/^ *| *$/gm,””);
var target = $(“#tracking”);
target.val(label + “|” + value);


And, we need users’ location and geographic coordinates. we can get them from many IP query websites, like ipinfo.io.

// IP info;
var target = $("#tracking");
$.get("http://ipinfo.io", function(response) {
target.val("ipInfo|" + response.ip + "," + response.city + "," + response.loc);
}, "jsonp");

A demo can be found here, and its source codes are stored here.

To leave a comment for the author, please follow the link and comment on their blog: Category: R | Huidong Tian's Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)