## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----eval = FALSE-------------------------------------------------------------
# shiny::runApp(system.file("examples", "shiny-brush", package = "glyph"))

## ----echo = FALSE, out.width = "100%", fig.alt = "The example app: a scatterplot of car weight against miles per gallon, with a grey brush rectangle over nine light, efficient cars. Below it, the text '9 cars selected.' and a table of those nine cars."----
knitr::include_graphics("shiny-app.png")

## ----eval = FALSE, code = readLines(system.file("examples", "shiny-brush", "app.R", package = "glyph"))----
# # Brush points in the scatterplot to list the cars in a table.
# #
# # Run it with:
# #   shiny::runApp(system.file("examples", "shiny-brush", package = "glyph"))
# 
# library(shiny)
# library(glyph)
# 
# cars <- mtcars[, c("mpg", "cyl", "wt", "hp")]
# cars$cyl <- as.integer(cars$cyl)
# cars$hp <- as.integer(cars$hp)
# 
# ui <- fluidPage(
#   titlePanel("Brushing cars"),
#   p("Drag across the plot to select cars. Press Escape, or click",
#     "Clear selection, to start again."),
#   glyphOutput("plot", height = "400px"),
#   textOutput("count"),
#   tableOutput("rows")
# )
# 
# server <- function(input, output, session) {
#   output$plot <- renderGlyph(
#     glyph(cars, x = wt, y = mpg) |>
#       mark_point(color = factor(cyl)) |>
#       interact(brush = TRUE, tooltip = TRUE)
#   )
# 
#   # Row numbers of `cars`, or NULL when nothing is selected.
#   selected <- reactive(input$plot_selected)
# 
#   output$count <- renderText({
#     n <- length(selected())
#     if (is.null(selected())) "No cars selected."
#     else if (n == 1) "1 car selected."
#     else paste(n, "cars selected.")
#   })
# 
#   output$rows <- renderTable({
#     req(selected())
#     cars[selected(), ]
#   }, rownames = TRUE)
# }
# 
# shinyApp(ui, server)

## ----eval = FALSE-------------------------------------------------------------
# output$plot <- renderGlyph(
#   glyph(cars, x = wt, y = mpg) |>
#     mark_point(color = factor(cyl)) |>
#     interact(brush = TRUE, tooltip = TRUE)
# )

## ----eval = FALSE-------------------------------------------------------------
# df <- data.frame(x = c(1, NA, 3, 4), y = c(2, 5, 1, 3))
# glyph(df, x = x, y = y) |> mark_point() |> interact(brush = TRUE)
# # Brushing every point gives c(1L, 3L, 4L).

## ----eval = FALSE-------------------------------------------------------------
# output$plots <- renderGlyph(
#   compose(
#     cars = glyph(mtcars, x = wt, y = mpg) |>
#       mark_point() |>
#       interact(brush = TRUE),
#     flowers = glyph(iris, x = Sepal.Length, y = Petal.Length) |>
#       mark_point() |>
#       interact(brush = TRUE)
#   )
# )

