Skip to content

Instantly share code, notes, and snippets.

@withr
Last active October 24, 2018 14:05
Show Gist options
  • Save withr/8934848 to your computer and use it in GitHub Desktop.
Save withr/8934848 to your computer and use it in GitHub Desktop.
Who touched my shiny-app?
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)
})
})
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),
textInput(inputId = "tracking",
label = "tracking:",
value = "")
),
div(class = "span8",
h3(textOutput("caption")),
verbatimTextOutput("summary"),
tableOutput("view")
)
))
// IP info;
$(document).ready(function(){
var target = $("#tracking");
$.get("http://ipinfo.io", function(response) {
target.val(JSON.stringify(response));
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");
}
});
// For type equal 'text' INPUT;
$(document).on("change", ".shiny-bound-input:not([id='tracking'])", function(evt) {
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");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment