Skip to content

Commit

Permalink
Rename selectBrush to brushedPoints
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed May 1, 2015
1 parent 5fefc48 commit 30d0bfb
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 31 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export(basicPage)
export(bootstrapPage)
export(br)
export(brushOpts)
export(brushedPoints)
export(checkboxGroupInput)
export(checkboxInput)
export(clickOpts)
Expand Down Expand Up @@ -143,7 +144,6 @@ export(runExample)
export(runGist)
export(runGitHub)
export(runUrl)
export(selectBrush)
export(selectInput)
export(selectizeInput)
export(serverInfo)
Expand Down
2 changes: 1 addition & 1 deletion R/bootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,7 @@ verbatimTextOutput <- function(outputId) {
#' res
#' })
#' output$plot_brushedpoints <- renderTable({
#' res <- selectBrush(data(), input$plot_brush, "speed", "dist")
#' res <- brushedPoints(data(), input$plot_brush, "speed", "dist")
#' if (nrow(res) == 0)
#' return()
#' res
Expand Down
28 changes: 16 additions & 12 deletions R/image-interact.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
#' 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
#' \code{plot(x=cars$speed, y=cars$dist)}, and your brush is named
#' \code{"cars_brush"}, then you would use \code{selectBrush(cars,
#' \code{"cars_brush"}, then you would use \code{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 \code{ggplot(cars, aes(x=speed, y=dist)) + geom_point()}, you
#' could use \code{selectBrush(cars, input$cars_brush)}. If, however, you use a
#' could use \code{brushedPoints(cars, input$cars_brush)}. If, however, you use a
#' computed column, like \code{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
Expand All @@ -28,13 +28,9 @@
#'
#' @param brush The data from a brush, such as \code{input$plot_brush}.
#' @param df A data frame from which to select rows.
#' @param xvar A string with the name of the variable on the x axis. This must
#' @param 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 \code{df}. If absent, then
#' \code{selectBrush} will try to infer the variable from the brush (only
#' works for ggplot2).
#' @param yvar A string with the name of the variable on the y axis. This must
#' also be the name of a column in \code{df}. If absent, then
#' \code{selectBrush} will try to infer the variable from the brush (only
#' this function will try to infer the variable from the brush (only
#' works for ggplot2).
#' @param 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
Expand All @@ -44,22 +40,26 @@
#'
#' @seealso \code{\link{plotOutput}} for example usage.
#' @export
selectBrush <- function(df, brush, xvar = NULL, yvar = NULL,
brushedPoints <- function(df, brush, xvar = NULL, yvar = NULL,
panelvar1 = NULL, panelvar2 = NULL) {
if (is.null(brush)) {
return(df[0, , drop = FALSE])
}

if (is.null(brush$xmin)) {
stop("brushedPoints requires a brush object with xmin, xmax, ymin, and ymax.")
}

# Try to extract vars from brush object
xvar <- xvar %OR% brush$mapping$x
yvar <- yvar %OR% brush$mapping$y
panelvar1 <- panelvar1 %OR% brush$mapping$panelvar1
panelvar2 <- panelvar2 %OR% brush$mapping$panelvar2

if (is.null(xvar))
stop("selectBrush: not able to automatically infer `xvar` from brush")
stop("brushedPoints: not able to automatically infer `xvar` from brush")
if (is.null(yvar))
stop("selectBrush: not able to automatically infer `yvar` from brush")
stop("brushedPoints: not able to automatically infer `yvar` from brush")

# Extract data values from the data frame
x <- asNumber(df[[xvar]])
Expand Down Expand Up @@ -91,7 +91,7 @@ selectBrush <- function(df, brush, xvar = NULL, yvar = NULL,
#' \code{"cars_click"}, then you would use \code{nearPoints(cars,
#' input$cars_brush, "speed", "dist")}.
#'
#' @inheritParams selectBrush
#' @inheritParams brushedPoints
#' @param coordinfo The data from a mouse event, such as \code{input$plot_click}.
#' @param threshold A maxmimum distance to the click point; rows in the data
#' frame where the distance to the click is less than \code{threshold} will be
Expand Down Expand Up @@ -123,6 +123,10 @@ nearPoints <- function(df, coordinfo, xvar = NULL, yvar = NULL,
return(df[0, , drop = FALSE])
}

if (is.null(coordinfo$x)) {
stop("nearPoints requires a click/hover/double-click object with x and y values.")
}

# Try to extract vars from coordinfo object
xvar <- xvar %OR% coordinfo$mapping$x
yvar <- yvar %OR% coordinfo$mapping$y
Expand Down
19 changes: 7 additions & 12 deletions man/selectBrush.Rd → man/brushedPoints.Rd
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Please edit documentation in R/image-interact.R
\name{selectBrush}
\alias{selectBrush}
\name{brushedPoints}
\alias{brushedPoints}
\title{Find rows of data that are selected by a brush}
\usage{
selectBrush(df, brush, xvar = NULL, yvar = NULL, panelvar1 = NULL,
brushedPoints(df, brush, xvar = NULL, yvar = NULL, panelvar1 = NULL,
panelvar2 = NULL)
}
\arguments{
\item{df}{A data frame from which to select rows.}

\item{brush}{The data from a brush, such as \code{input$plot_brush}.}

\item{xvar}{A string with the name of the variable on the x axis. This must
\item{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 \code{df}. If absent, then
\code{selectBrush} will try to infer the variable from the brush (only
works for ggplot2).}

\item{yvar}{A string with the name of the variable on the y axis. This must
also be the name of a column in \code{df}. If absent, then
\code{selectBrush} will try to infer the variable from the brush (only
this function will try to infer the variable from the brush (only
works for ggplot2).}

\item{panelvar1,panelvar2}{Each of these is a string with the name of a panel
Expand All @@ -37,14 +32,14 @@ The \code{xvar}, \code{yvar}, \code{panelvar1}, and \code{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
\code{plot(x=cars$speed, y=cars$dist)}, and your brush is named
\code{"cars_brush"}, then you would use \code{selectBrush(cars,
\code{"cars_brush"}, then you would use \code{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 \code{ggplot(cars, aes(x=speed, y=dist)) + geom_point()}, you
could use \code{selectBrush(cars, input$cars_brush)}. If, however, you use a
could use \code{brushedPoints(cars, input$cars_brush)}. If, however, you use a
computed column, like \code{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
Expand Down
2 changes: 1 addition & 1 deletion man/imageOutput.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ shinyApp(
res
})
output$plot_brushedpoints <- renderTable({
res <- selectBrush(data(), input$plot_brush, "speed", "dist")
res <- brushedPoints(data(), input$plot_brush, "speed", "dist")
if (nrow(res) == 0)
return()
res
Expand Down
8 changes: 4 additions & 4 deletions man/nearPoints.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ nearPoints(df, coordinfo, xvar = NULL, yvar = NULL, panelvar1 = NULL,

\item{coordinfo}{The data from a mouse event, such as \code{input$plot_click}.}

\item{xvar}{A string with the name of the variable on the x axis. This must
\item{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 \code{df}. If absent, then
\code{selectBrush} will try to infer the variable from the brush (only
this function will try to infer the variable from the brush (only
works for ggplot2).}

\item{yvar}{A string with the name of the variable on the y axis. This must
\item{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 \code{df}. If absent, then
\code{selectBrush} will try to infer the variable from the brush (only
this function will try to infer the variable from the brush (only
works for ggplot2).}

\item{panelvar1}{Each of these is a string with the name of a panel
Expand Down

0 comments on commit 30d0bfb

Please sign in to comment.