1

Is there any way to maintain my view (zoom level/ extents), in shiny plotly in R, as a plot re-renders?

I've a plot from which selected values are removed, after which the plot re-renders (as no delete traces in R?). I then have to navigate back to the area of interest on the plot. This slows the process a great deal, is there any way around this?

Thanks.

library(shiny)
library(plotly)
library(dplyr)

ui <- fluidPage(
  fluidRow(
    column(12,plotlyOutput("plot"),
           actionButton("addButton", "Save Edits"),
           verbatimTextOutput("txtout1"),
           verbatimTextOutput("txtout2"),
           verbatimTextOutput("txtout3"))
  )
)

server <- function(input, output, session) {
x<-c(1,2,3,4,5,6,7)   
y<-c(10,10,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(),{
  if(!vector.is.empty(K())){
    new0 <- isolate(M())
    isolate(values$df <- rbind(values$df, new0))
 }
})

uval <- reactiveValues()
uval$df <- df
newEntry1 <- observeEvent({values$df},{
  if(!vector.is.empty(K())){
  new1 <- isolate(data.frame(values$df))
  fnew1 <- filter(df, !(df$x %in% new1$x))
  isolate(uval$df <-  fnew1)
  }
})

output$plot <- renderPlotly({
  plot_ly(uval$df, x = x, y = y, mode = "markers",source="B") %>%
    layout(title = "Original Plot", font=list(size=10))
})

addData <- observe({
  if(input$addButton > 0) {
     save_data <<- isolate(uval$df)
  }
})

output$txtout1 <- renderPrint({
  if(vector.is.empty(K())) "Click and drag across points" else M()
 #nrow(df)
})

output$txtout2 <- renderPrint({
  #nrow(uval$df)
  uval$df
})

output$txtout3 <- renderPrint({
  #nrow(values$df)
  values$df
})

}

shinyApp(ui, server, options = list(display.mode = "showcase"))
Mirza Sisic
  • 2,401
  • 4
  • 24
  • 38
Pidgeon
  • 41
  • 6
  • Would the same rerendering issue be the case, if I were to null the selected data? – Pidgeon Sep 08 '16 at 08:47
  • I'm still a bit consfused, your example works fine with zooming and dubble click for zooming back out. – Axeman Sep 08 '16 at 08:51
  • When you select a point with box/lasso it's removed from the plot, and the view resets back to include the extents of the data. Is there any way to stop this and to stay at the zoom/extents when the data was selected? – Pidgeon Sep 08 '16 at 10:28

0 Answers0