2

I've put together this Shiny app from tutorial and examples, and I've become stuck. My aim is to make the plot reactive, so that the data points in 'uval$df' are plotted, meaning that selected points will be removed from the graph, and it can't be selected twice. How do I do this? (I've got a feeling it's something lacking in my basic understanding)

Thanks!

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"))

1 Answer 1

1

Simple, as I thought.

plot_ly(uval$df, x = x, y = y, mode = "markers",source="B")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.