nearPoints {shiny} | R Documentation

## Find rows of data that are near a click/hover/double-click

### Description

This function returns rows from a data frame which are near a click, hover, or double-click, when used with `plotOutput`. The rows will be sorted by their distance to the mouse event.

### Usage

    nearPoints(df, coordinfo, xvar = NULL, yvar = NULL, panelvar1 = NULL,
      panelvar2 = NULL, threshold = 5, maxpoints = NULL, addDist = FALSE,
      allRows = FALSE)

### Arguments

`df` | A data frame from which to select rows.
`coordinfo` | The data from a mouse event, such as `input$plot_click`.
`xvar` | A string with the name of the variable on the x or y axis. This must also be the name of a column in `df`. If absent, then this function will try to infer the variable from the brush (only works for ggplot2).
`yvar` | A string with the name of the variable on the x or y axis. This must also be the name of a column in `df`. If absent, then this function will try to infer the variable from the brush (only works for ggplot2).
`panelvar1` | Each of these is a string with the name of a panel variable. For example, if with ggplot2, you facet on a variable called `cyl`, then you can use `"cyl"` here. However, specifying the panel variable should not be necessary with ggplot2; Shiny should be able to auto-detect the panel variable.
`panelvar2` | Each of these is a string with the name of a panel variable. For example, if with ggplot2, you facet on a variable called `cyl`, then you can use `"cyl"` here. However, specifying the panel variable should not be necessary with ggplot2; Shiny should be able to auto-detect the panel variable.
`threshold` | A maxmimum distance to the click point; rows in the data frame where the distance to the click is less than `threshold` will be returned.
`maxpoints` | Maximum number of rows to return. If NULL (the default), return all rows that are within the threshold distance.
`addDist` | If TRUE, add a column named `dist_` that contains the distance from the coordinate to the point, in pixels. When no mouse event has yet occured, the value of `dist_` will be `NA`.
`allRows` | If `FALSE` (the default) return a data frame containing the selected rows. If `TRUE`, the input data frame will have a new column, `selected_`, which indicates whether the row was inside the selected by the mouse event (`TRUE`) or not (`FALSE`).

### Details

It is also possible for this function to return all rows from the input data frame, but with an additional column `selected_`, which indicates which rows of the input data frame are selected by the brush (`TRUE` for selected, `FALSE` for not-selected). This is enabled by setting `allRows=TRUE` option. If this is used, the resulting data frame will not be sorted by distance to the mouse event.

The `xvar`, `yvar`, `panelvar1`, and `panelvar2` arguments specify which columns in the data correspond to the x variable, y variable, and panel variables of the plot. For example, if your plot is `plot(x=cars$speed, y=cars$dist)`, and your click variable is named `"cars_click"`, then you would use `nearPoints(cars, input$cars_brush, "speed", "dist")`.

### See Also

`plotOutput` for more examples.

### Examples

    ## Not run:
    # Note that in practice, these examples would need to go in reactives
    # or observers.

    # This would select all points within 5 pixels of the click
    nearPoints(mtcars, input$plot_click)

    # Select just the nearest point within 10 pixels of the click
    nearPoints(mtcars, input$plot_click, threshold = 10, maxpoints = 1)

    ## End(Not run)

[Package shiny version 1.0.3 Index]