Plot
#| standalone: true
#| components: [viewer]
#| viewerHeight: 200
library(shiny)
library(bslib)
library(ggplot2)
ui <- page_fluid(
layout_column_wrap(
plotOutput(
"plot",
click = "plot_click",
height = "200px"
),
textOutput("coords")
)
)
server <- function(input, output) {
output$plot <- renderPlot({
mpg |>
ggplot(aes(hwy, cty)) +
geom_point(size = 1) +
labs(title = "Click anywhere on the plot")
})
output$coords <- renderText({
req(input$plot_click) # Ensure values available before proceeding
x <- round(input$plot_click$x, 2)
y <- round(input$plot_click$y, 2)
glue::glue("({x}, {y})")
})
}
shinyApp(ui, server)
library(shiny)
library(bslib)
library(ggplot2)
ui <- page_fluid(
plotOutput("plot", click = "plot_click"),
textOutput("coords")
)
server <- function(input, output) {
output$plot <- renderPlot({
mpg |>
ggplot(aes(hwy, cty)) +
geom_point() +
labs(title = "Click anywhere on the plot")
})
output$coords <- renderText({
req(input$plot_click) # Ensure values are available before proceeding
x <- round(input$plot_click$x, 2)
y <- round(input$plot_click$y, 2)
glue::glue("({x}, {y})")
})
}
shinyApp(ui, server)
Relevant Functions
-
brushedPoints
brushedPoints(df, brush, xvar = NULL, yvar = NULL, panelvar1 = NULL, panelvar2 = NULL, allRows = FALSE)
-
nearPoints
nearPoints(df, coordinfo, xvar = NULL, yvar = NULL, panelvar1 = NULL, panelvar2 = NULL, threshold = 5, maxpoints = NULL, addDist = FALSE, allRows = FALSE)
-
plotOutput
plotOutput(outputId, width = "100%", height = "400px", click = NULL, dblclick = NULL, hover = NULL, brush = NULL, inline = FALSE, fill = !inline)
-
renderPlot
renderPlot(expr, width = "auto", height = "auto", res = 72, ..., alt = NA, env = parent.frame(), quoted = FALSE, execOnResize = FALSE, outputArgs = list())
Details
Plots can be used as inputs that respond to mouse events. To create a plot that responds to mouse events, you’ll first need to create a plot output:
Add
plotOutput()
in the UI of your app to create a div in which to display the plot. Where you call this function within the UI functions will determine where the plot will appear within the layout of the app. Set theoutputId
argument ofplotOutput()
to a unique value.Provide
plotOutput()
’sclick
,dblclick
,hover
, and/orbrush
arguments with unique strings to control what happens when the user click, double-clicks, hovers, or brushes the plot. For example,click = "plot_click"
will allow you to control what happens when the user clicks the plot by accessinginput$plot_click
in the server function.Within the server function, call
renderPlot()
and save its output as an element of theoutput
list. Name the element after theoutputId
used above. For example,output$plot <- renderPlot()
.plotOutput()
will display the value of theoutput
element whose name matches itsoutputId
.Pass
renderPlot()
a block of code, surrounded with{}
. The code should return a plot (e.g., a ggplot2 object).
Next, create another output that will display information after the user interacts with the plot.
Add another output to the UI of your app (e.g.,
textOutput()
). This output will display information after the user interacts with the plot. Provide this output with a uniqueoutputId
.Within the server function, use the corresponding
render_()
function (e.g.,renderText()
) and save its output as an element of theoutput
list. Name the element after theoutputId
used above. For example,output$coords <- renderText()
.
Finally, connect the second output to the plot input.
- Pass the second
render_()
function (e.g.,renderText()
) a block of code, surrounded by{}
. This code should use the plot inputs to display the relevant type of output. Access the plot inputs using the strings provided toplotOutput()
’sclick
,dblclick
,hover
, and/orbrush
arguments (e.g.,input$plot_click
).
The server value of click
, dblclick
, and hover
inputs are named lists with x
and y
elements indicating the mouse position. The server value of the brush
input is a named list indicating the brush area (see the Plot brushing variation below for more details).
Variations
Find nearby points
Use the function nearPoints()
to create a data frame containing rows near the click.
#| standalone: true
#| components: [viewer]
#| viewerHeight: 300
library(shiny)
library(bslib)
library(ggplot2)
ui <- page_fluid(
layout_column_wrap(
plotOutput("plot", click = "plot_click", height = "200px"),
tableOutput("data")
)
)
server <- function(input, output) {
output$plot <- renderPlot({
mpg |>
ggplot(aes(hwy, cty)) +
geom_point(size = 1) +
labs(title = "Click on a point on the plot")
})
output$data <- renderTable({
nearPoints(mpg, input$plot_click)
})
}
shinyApp(ui, server)
library(shiny)
library(bslib)
library(ggplot2)
ui <- page_fluid(
plotOutput("plot", click = "plot_click"),
tableOutput("data")
)
server <- function(input, output) {
output$plot <- renderPlot({
mpg |>
ggplot(aes(hwy, cty)) +
geom_point() +
labs(title = "Click on a point on the plot")
})
output$data <- renderTable({
nearPoints(mpg, input$plot_click)
})
}
shinyApp(ui, server)
Plot brushing
Pass a string to the brush
argument of plotOutput()
to allow the user to “brush” the plotting area. You can then use that string to access the server value of the brushed area (e.g., input$plot_brush
), which will be a named list with xmin
, xmax
, ymin
, and ymax
elements indicating the brush area. Use the function brushedPoints()
to access the rows from the data frame that fall within the brushed area.
#| standalone: true
#| components: [viewer]
#| viewerHeight: 300
library(shiny)
library(bslib)
library(ggplot2)
ui <- page_fluid(
layout_column_wrap(
plotOutput("plot", brush = "plot_brush", height = "200px"),
tableOutput("data")
)
)
server <- function(input, output) {
output$plot <- renderPlot({
mpg |>
ggplot(aes(hwy, cty)) +
geom_point(size = 1) +
labs(title = "Select an area on the plot")
})
output$data <- renderTable({
brushedPoints(mpg, input$plot_brush)
})
}
shinyApp(ui, server)
library(shiny)
library(bslib)
library(ggplot2)
ui <- page_fluid(
plotOutput("plot", brush = "plot_brush"),
tableOutput("data")
)
server <- function(input, output) {
output$plot <- renderPlot({
mpg |>
ggplot(aes(hwy, cty)) +
geom_point() +
labs(title = "Select an area on the plot")
})
output$data <- renderTable({
brushedPoints(mpg, input$plot_brush)
})
}
shinyApp(ui, server)