Skip to content

Instantly share code, notes, and snippets.

@withr
Last active October 24, 2018 14:05

Revisions

  1. @tianhuidong tianhuidong revised this gist Apr 8, 2015. 1 changed file with 114 additions and 28 deletions.
    142 changes: 114 additions & 28 deletions www\tracking.js
    Original file line number Diff line number Diff line change
    @@ -6,34 +6,120 @@ $(document).ready(function(){
    target.trigger("change");
    }, 'json');
    });
    // class is 'shiny-bound-input';
    $(document).on("change", ".shiny-bound-input:not([id='tracking'])", function(evt) {
    var e = $(evt.target);
    var tagName = e.context.tagName;
    // SELECT
    if (tagName == "SELECT") {
    var A = {};
    A['id'] = e.context.id;
    A['type'] = e.context.type;
    var so = e.context.selectedOptions;
    var S = [];
    for (var i=0; i<so.length; i++) {
    var s = {};
    s[so[i].innerHTML] = so[i].value
    S.push(s)
    }
    A['selected'] = S;
    console.log(A)
    var target = $("#tracking");
    target.val(JSON.stringify(A));
    target.trigger("change");
    }
    // INPUT
    if (tagName == "INPUT") {
    var A = {};
    var type = e.context.type;
    //radioButtons, numericInput, passwordInput;
    if (["radio", "numeric", "password"].indexOf(type) >= 0) {
    if (type == "radio") {A['name'] = e.context.name;}
    A['id'] = e.context.id;
    A['value'] = e.context.value;
    A['label'] = e.siblings()[0].innerHTML;
    }
    //fileInput;
    if (type == "file") {
    A['id'] = e.context.name;
    files = [];
    for (var i=0; i<e.context.files.length; i++) {
    files.push(JSON.stringify(e.context.files[i]));
    }
    A['files'] = files;
    }
    //checkboxInput, checkboxGroupInput;
    if (type == "checkbox") {
    A['id'] = e[0].id;
    A['value'] = e[0].value
    A['checked'] = e[0].checked
    A['label'] = e.siblings()[0].innerHTML;
    }
    console.log(A)
    var target = $("#tracking");
    target.val(JSON.stringify(A));
    target.trigger("change");
    }
    });


    // Who touched my Shiny-app?
    // For type equal 'text' INPUT;
    $(document).on("change", ".shiny-bound-input:not([id='tracking'])", function(evt) {
    var el = $(evt.target);
    var target = $("#tracking");
    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();
    console.log(el.prev("label"))
    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,"");
    target.val(label + "|" + value);
    target.trigger("change");
    var e = $(evt.target);
    var tagName = e.context.tagName;
    if (tagName == "INPUT") {
    var type = e.context.type;
    if (type == "text") {
    var v = e.context.value
    setTimeout(function(){
    var e = $(evt.target);
    if (e.context.value == v) {
    var A = {};
    A['value'] = e.context.value;
    var id = e.context.id;
    var label = e.siblings()[0].innerHTML;
    if (id == "") {
    var id = e.parent()[0].id;
    if (id == "") {
    var id = e.parent().parent()[0].id;
    var label = e.parent().siblings()[0].innerHTML
    if (e.siblings()[1].tagName == "SPAN") {
    A['dateType'] = "end"
    } else {
    A['dateType'] = "start"
    }
    }
    }
    A['id'] = id;
    A['label'] = label;
    console.log(A)
    var target = $("#tracking");
    target.val(JSON.stringify(A));
    target.trigger("change");
    }
    }, 500);
    }
    }
    });
    // actionButton;
    $(document).on("click", ".shiny-bound-input:not([id='tracking'])", function(evt) {
    var e = $(evt.target);
    var tagName = e.context.tagName;
    if (["A", "BUTTON"].indexOf(tagName) >= 0) {
    var A = {};
    A['id'] = e.context.id;
    A['label'] = e.context.innerText;
    console.log(A)
    var target = $("#tracking");
    target.val(JSON.stringify(A));
    target.trigger("change");
    }
    });
    // submitButton;
    $(':submit').on("click", function(evt) {
    var e = $(evt.target);
    if (e.context.tagName == "BUTTON") {
    var target = $("#tracking");
    target.val(JSON.stringify(e.context.innerText));
    target.trigger("change");
    }
    });
  2. @tianhuidong tianhuidong revised this gist Apr 7, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions www\tracking.js
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,9 @@
    $(document).ready(function(){
    var target = $("#tracking");
    $.get("http://ipinfo.io", function(response) {
    target.val("ipInfo|" + response.ip + "," + response.city + "," + response.loc);
    target.val(JSON.stringify(response));
    target.trigger("change");
    }, "jsonp");
    }, 'json');
    });


  3. @tianhuidong tianhuidong revised this gist Feb 13, 2014. 2 changed files with 6 additions and 26 deletions.
    6 changes: 4 additions & 2 deletions ui.R
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,9 @@ shinyUI(bootstrapPage(
    checkboxInput(inputId = "check",
    label = "Include NA?",
    value = TRUE),
    HTML("<textarea id='tracking'></textarea>")
    textInput(inputId = "tracking",
    label = "tracking:",
    value = "")
    ),

    div(class = "span8",
    @@ -26,4 +28,4 @@ shinyUI(bootstrapPage(
    tableOutput("view")
    )

    ))
    ))
    26 changes: 2 additions & 24 deletions www\tracking.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    // IP info;
    $(document).ready(function(){
    var target = $("textarea#tracking");
    var target = $("#tracking");
    $.get("http://ipinfo.io", function(response) {
    target.val("ipInfo|" + response.ip + "," + response.city + "," + response.loc);
    target.trigger("change");
    @@ -11,7 +11,7 @@ $(document).ready(function(){
    // Who touched my Shiny-app?
    $(document).on("change", ".shiny-bound-input:not([id='tracking'])", function(evt) {
    var el = $(evt.target);
    var target = $("textarea#tracking");
    var target = $("#tracking");
    if (el.prop("tagName").toLowerCase() === "select") {
    value = $("option:selected", el).map(function(){ return this.text }).get().join(", ");
    label = el.prev("label").text();
    @@ -37,25 +37,3 @@ $(document).on("change", ".shiny-bound-input:not([id='tracking'])", function(evt
    target.val(label + "|" + value);
    target.trigger("change");
    });

    var trackingBinding = new Shiny.InputBinding();
    $.extend(trackingBinding, {
    find: function(scope) {
    return $(scope).find(".tracking");
    },
    getValue: function(el) {
    return $(el).val();
    },
    setValue: function(el, value) {
    $(el).val(value);
    },
    subscribe: function(el, callback) {
    $(el).on("change.trackingBinding", function(e) {
    callback();
    });
    },
    unsubscribe: function(el) {
    $(el).off(".trackingBinding");
    }
    });
    Shiny.inputBindings.register(trackingBinding);
  4. @tianhuidong tianhuidong created this gist Feb 11, 2014.
    22 changes: 22 additions & 0 deletions server.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    shinyServer(function(input, output) {

    datasetInput <- reactive({
    switch(input$dataset,
    "rock" = rock,
    "pressure" = pressure,
    "cars" = cars)
    })

    output$caption <- renderText({
    input$caption
    })

    output$summary <- renderPrint({
    dataset <- datasetInput()
    summary(dataset)
    })

    output$view <- renderTable({
    head(datasetInput(), n = input$obs)
    })
    })
    29 changes: 29 additions & 0 deletions ui.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    shinyUI(bootstrapPage(
    # Add custom CSS
    tagList(tags$head(tags$script(type="text/javascript", src = "tracking.js"))),
    div(class = "span4",
    textInput(inputId = "caption",
    label = "Caption:",
    value = "Data Summary"),
    selectInput(inputId = "dataset",
    label = "Choose a dataset:",
    choices = c("rock", "pressure", "cars")),
    numericInput(inputId = "obs",
    label = "Number of observations to view:",
    value = 10),
    radioButtons(inputId = "radio",
    label = "Transform type:",
    choices = c("Log", "Exp")),
    checkboxInput(inputId = "check",
    label = "Include NA?",
    value = TRUE),
    HTML("<textarea id='tracking'></textarea>")
    ),

    div(class = "span8",
    h3(textOutput("caption")),
    verbatimTextOutput("summary"),
    tableOutput("view")
    )

    ))
    61 changes: 61 additions & 0 deletions www\tracking.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    // IP info;
    $(document).ready(function(){
    var target = $("textarea#tracking");
    $.get("http://ipinfo.io", function(response) {
    target.val("ipInfo|" + response.ip + "," + response.city + "," + response.loc);
    target.trigger("change");
    }, "jsonp");
    });


    // Who touched my Shiny-app?
    $(document).on("change", ".shiny-bound-input:not([id='tracking'])", function(evt) {
    var el = $(evt.target);
    var target = $("textarea#tracking");
    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();
    console.log(el.prev("label"))
    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,"");
    target.val(label + "|" + value);
    target.trigger("change");
    });

    var trackingBinding = new Shiny.InputBinding();
    $.extend(trackingBinding, {
    find: function(scope) {
    return $(scope).find(".tracking");
    },
    getValue: function(el) {
    return $(el).val();
    },
    setValue: function(el, value) {
    $(el).val(value);
    },
    subscribe: function(el, callback) {
    $(el).on("change.trackingBinding", function(e) {
    callback();
    });
    },
    unsubscribe: function(el) {
    $(el).off(".trackingBinding");
    }
    });
    Shiny.inputBindings.register(trackingBinding);