试试这个。它不是用阴谋,而是用同样的总体思路。实际上,它将数据过滤到画笔。双击将完全返回。
require(shiny)
library(dplyr)
library(ggplot2)
ui <- fluidPage(
titlePanel(title=h4("example", align="center")),
mainPanel(
plotOutput("plot", brush = brushOpts("brush_p")))
)
##server
server <- function(input,output){
dat<-reactive({
num<-c(1,2,3,4,5,6,7,8,9)
let<-c("A","B","C","D","E","F","G","H","I")
df<-data.frame(num,let)
df
})
dat2 <- reactive({
y_max <- input$brush_p$ymax
y_min <- input$brush_p$ymin
if(is.null(y_max)) return(dat())
dat() %>% filter(num > y_min & num < y_max)
})
output$plot<-renderPlot({
gg <- ggplot(dat2(),aes(x=let,y=num))+geom_point(aes(colour='red'))
gg
})
}
shinyApp(ui = ui, server = server)
reprex
package
(第0.2.0版)。