r语言 - Shiny plot反应性数据图



我已经把这个闪亮的应用程序从教程和例子,我已经卡住了。我的目标是使绘图具有反应性,以便绘制'uval$df'中的数据点,这意味着选中的点将从图中删除,并且不能选择两次。我该怎么做呢?(我有一种感觉,我的基本理解有些欠缺。)

谢谢!

library(shiny)
library(plotly)
library(dplyr)
ui <- fluidPage(
  fluidRow(
    column(12,plotlyOutput("plot"),
           verbatimTextOutput("txtout1"),
           verbatimTextOutput("txtout2"),
           verbatimTextOutput("txtout3"))
  )
)
server <- function(input, output, session) {
x<-c(1,2,34,2,1,23,24)   
y<-c(10,20,30,40,50,60,70)
df<-data.frame(x,y)
vector.is.empty <- function(x) return(length(x) ==0 )
K <-reactive({
  event_data("plotly_selected",source = "B")
})
M<-reactive({
  K()[,c("x","y")]
})
values <- reactiveValues()
values$df <- data.frame(x = numeric(0), y = numeric(0))
newEntry <- observeEvent(K(),priority = 1,{
    new0 <- isolate(M())
    isolate(values$df <- rbind(values$df, new0))
})
uval <- reactiveValues()
uval$df <- df
newEntry1 <- observeEvent({values$df},priority = 2,{
  new1 <- isolate(data.frame(values$df))
  isolate(uval$df <- setdiff(df,new1))
})
output$plot <- renderPlotly({
  plot_ly(x = df$x, y = df$y, mode = "markers",source="B") %>%
    layout(dragmode =  "select", title = "Original Plot", font=list(size=10))
})
output$txtout1 <- renderPrint({
  if(vector.is.empty(K())) "Click and drag across points" else M()
})
output$txtout2 <- renderPrint({
  uval$df
})
output$txtout3 <- renderPrint({
  values$df
})
}
shinyApp(ui, server, options = list(display.mode = "showcase"))

和我想的一样简单。

plot_ly(uval$df, x = x, y = y, mode = "markers",source="B")

最新更新