Extract NEW Traces from Plotly

I'd like to:

  • render a plot to display

*allow the user to change features using something like plotlyProxy()

*extract those values for other calculations to potentially use.
This code doesn't do this job but this is the kind of thing I'm trying to do!

library(shiny)
library(plotly)

ui <- fluidPage(
  
  h4("addTraces", style = "text-decoration: underline;"),
  fluidRow(
    textInput("xvalue", NULL, "1,2,3,4"),
    textInput("yvalue", "Add more Y values" , "3,3,3,3"),
  actionButton("add", "Add", width = '210px'),

  plotlyOutput("p"),
  verbatimTextOutput("sum_ys")
))

server <- function(input, output, session) {
  
  
  #The plot output and send the data to global environment
  output$p <- renderPlotly({
   out <- plot_ly(
     type = 'scatter',
      mode = 'markers',
      x = c(1,2,3,4), 
      y = c(2,4,2,4)
    ) %>%
      layout(
        showlegend = T)
   plotly_get_data <<- plotly_build(out)
   return(out)
  })
  
  # plotly.addTraces. plotlyProxy takes an outputID not a plotly object so not clear 
#if can use this and then still extract the Y values for use by other functions
  observeEvent(input$add, {
    plotlyProxy("p", session) %>%
      plotlyProxyInvoke("addTraces", list(x = as.integer(unlist(strsplit(input$xvalue,","))), 
                                          y = as.integer(unlist(strsplit(input$yvalue, ","))),
                                          type = 'scatter',
                                          mode = 'markers'))
  })
  
  #view the data generated
  output$sum_ys <- renderText({
    paste("The y values on this graph are: ",
          list(plotly_get_data$x$data[[1]]$y))
  })
}

shinyApp(ui, server)

Any help would be MUCH appreciated!

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.