Shiny by RStudio

Find rows of data that are selected by a brush

brushedPoints(df, brush, xvar = NULL, yvar = NULL, panelvar1 = NULL,
  panelvar2 = NULL, allRows = FALSE)

Arguments

df A data frame from which to select rows.
brush The data from a brush, such as input$plot_brush.
xvar, 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, 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.
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 brush (TRUE) or outside the brush (FALSE).

Description

This function returns rows from a data frame which are under a brush used with plotOutput.

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.

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 brush is named "cars_brush", then you would use brushedPoints(cars, input$cars_brush, "speed", "dist").

For plots created with ggplot2, it should not be necessary to specify the column names; that information will already be contained in the brush, provided that variables are in the original data, and not computed. For example, with ggplot(cars, aes(x=speed, y=dist)) + geom_point(), you could use brushedPoints(cars, input$cars_brush). If, however, you use a computed column, like ggplot(cars, aes(x=speed/2, y=dist)) + geom_point(), then it will not be able to automatically extract column names and filter on them. If you want to use this function to filter data, it is recommended that you not use computed columns; instead, modify the data first, and then make the plot with "raw" columns in the modified data.

If a specified x or y column is a factor, then it will be coerced to an integer vector. If it is a character vector, then it will be coerced to a factor and then integer vector. This means that the brush will be considered to cover a given character/factor value when it covers the center value.

If the brush is operating in just the x or y directions (e.g., with brushOpts(direction = "x"), then this function will filter out points using just the x or y variable, whichever is appropriate.

See also

plotOutput for example usage.