Skip to content

Instantly share code, notes, and snippets.

@stla
Forked from withr/server.R
Created May 10, 2014 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stla/ee63e1ee525ce5041c55 to your computer and use it in GitHub Desktop.
Save stla/ee63e1ee525ce5041c55 to your computer and use it in GitHub Desktop.
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("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 = $("#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");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment