您(可能)正在寻找的是shiny-package 的plot_brush-functions(您可以在Shiny Gallery 找到一个示例)。
以下将提供 2 个应用程序,它们相互构建以回答您的 3 个问题:
1 用 plot_brush 画一个矩形
这可以通过以下代码实现:
library(shiny)
server <- function(input, output) {
# render the plot
output$plot1 <- renderPlot({
plot(mtcars$mpg, mtcars$disp)
})
# set the options for the brush technique
output$plotui <- renderUI({
plotOutput("plot1", height=300,
brush = brushOpts(id = "plot_brush")
)
})
}
ui <- fluidPage(
# render the plot
uiOutput("plotui")
)
# run the app
shinyApp(ui = ui, server = server)
2 & 3 识别点并打印数据表
使用和扩展第 1 部分,我们识别点并将它们加载到名为 res 的 data.frame 中,然后将它们加载到数据表中(使用“DT”包):
library(shiny)
library(DT)
server <- function(input, output) {
# render the plot
output$plot1 <- renderPlot({
plot(mtcars$mpg, mtcars$disp)
})
# set the options for the brush technique
output$plotui <- renderUI({
plotOutput("plot1", height=300,
brush = brushOpts(id = "plot_brush")
)
})
# for part 2 and 3
output$plot_brushed_points <- renderDataTable({
df <- mtcars
# this function gets the data for you
res <- brushedPoints(df, input$plot_brush, "mpg","disp")
# mpg = name of x variable, disp = name of y variable
# puts the results in a datatable format
datatable(res)
})
}
ui <- fluidPage(
# render the plot
uiOutput("plotui"),
# renders the datatable
dataTableOutput("plot_brushed_points")
)
# run the app
shinyApp(ui = ui, server = server)
这给出了这样的结果:
Stackoverflow 上的这个问题将为您指明鼠标悬停的正确方向。