This is an example app of how to make use of the new feature - click and hover on static plots - in Shiny.
For more information, see: rstudio/shiny#183.
Other examples:
This is an example app of how to make use of the new feature - click and hover on static plots - in Shiny.
For more information, see: rstudio/shiny#183.
Other examples:
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") | |
)) |