inst/solutions/chapter5/interactive2.R

library("shiny")
ui = basicPage(
  plotOutput("scatter",
             click = "plot_click",
             dblclick = "plot_dblclick",
             hover = "plot_hover",
             brush = "plot_brush"
  ),
  verbatimTextOutput("info")
)

## Simulate data
x = signif(rnorm(10), 3); y = signif(rnorm(10), 3);

server = function(input, output) {
  output$scatter = renderPlot({
    plot(x, y)
  })
  output$info = renderText({
    xy_str = function(e) {
      if (is.null(e)) return("NULL\n")
      paste0("x=", round(e$x, 1), " y=", round(e$y, 1), "\n")
    }

    xy_range_str = function(e) {
      if (is.null(e)) return("NULL\n")
      paste0("xmin=", round(e$xmin, 1), " xmax=", round(e$xmax, 1),
             " ymin=", round(e$ymin, 1), " ymax=", round(e$ymax, 1))
    }

    paste0(
      "click: ", xy_str(input$plot_click),
      "dblclick: ", xy_str(input$plot_dblclick),
      "hover: ", xy_str(input$plot_hover),
      "brush: ", xy_range_str(input$plot_brush)
    )
  })
}

shinyApp(ui, server)
jr-packages/jrShiny documentation built on Feb. 16, 2020, 9:13 p.m.