Skip to content

Instantly share code, notes, and snippets.

@reinholdsson
Created August 25, 2013 09:38
Show Gist options
  • Save reinholdsson/6332998 to your computer and use it in GitHub Desktop.
Save reinholdsson/6332998 to your computer and use it in GitHub Desktop.
Chart comments shiny app
shinyServer(function(input, output, session) {
click_points_data <- data.frame(
date = character(),
x = numeric(),
y = numeric(),
category = factor(),
comment = character()
)
observe({
output$plot <- renderPlot({
plot(sin, -pi, 2*pi) # example
data <- click_points_data
if (nrow(data) > 0) {
points(data$x, data$y, cex = 4, pch = 1, col = as.integer(data$category))
text(
data$x, data$y, rownames(data),
col = as.integer(data$category), cex = 1.5
)
categories <- levels(data$category)
legend("topright", categories, fill = 1:length(categories))
}
abline(v=input$click$x, h=input$click$y)
})
if (input$save == 0)
return()
isolate({
newpoint <- input$click
if (!is.null(newpoint)) {
click_points_data <<- rbind(
click_points_data,
data.frame(
date = as.character(Sys.time()),
x = newpoint$x,
y = newpoint$y,
category = input$category,
comment = input$comment
)
)
}
})
output$mytable <- renderTable({
if (nrow(click_points_data) > 0) {
click_points_data[, c("date", "category", "comment")]
}
})
})
})
shinyUI(bootstrapPage(
h3("Chart Comments"),
p("Click on the chart, write a comment and select a category - save!"),
plotOutput("plot", clickId = "click"),
textInput("comment", "Comment:", ""),
radioButtons("category", "Category:", c("Category A", "Category B", "Category C")),
actionButton("save", "Save"),
br(), br(),
tableOutput("mytable")
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment