Shiny plot updates with inputs OR by using plotly_click

I've created a Shiny application where by clicking on a point in the first tabpanel you are rerouted to the second panel and a plot automatically generates using plotly's plotly_click function.

I was wondering if it's possible to create logic where the user can override this automatically generated plot and create their own plot on the second pane using input$species and input$sep_length

library(shiny)
library(plotly)
library(tidyverse)

# ui with two panes
# when you click on the outlier in the first plot
# you are routed to the second "point explorer" page
ui <- navbarPage("Plotly On Click Switch Pane", id = "navbarID",
                 tabPanel("Page_1",
                          fluidRow(
                            column(6, selectInput("x_iris", "X:", choices= c(unique(colnames(iris))))),
                            column(6, selectInput("y_iris", "Y:", choices = c(unique(colnames(iris)))))
                          ),
                          mainPanel(plotlyOutput("plot"),
                                    tableOutput("text"))),
                 tabPanel("Page_2",
                          mainPanel(
                            fluidRow(
                              # these inputs are not yet being called in the server because the ind_plot is being generated using the first pane
                              column(6, selectInput("Species", "Speies:", choices= c(unique(colnames(iris))))),
                              column(6, sliderInput("sep_length", "Sepal Length:", min = min(iris$Sepal.Length), max = max(iris$Sepal.Length), value = 3))
                            ),
                            plotlyOutput("ind_plot"))
                 ))


server <- function(input, output, session) {
  
  # plot on first page
  output$plot <- renderPlotly({
    ggplotly(source = "sub_iris",
      ggplot(iris, aes_string(x = input$x_iris, y = input$y_iris)) + 
      geom_point()
    ) %>%
      event_register("plotly_click")
  })
  
  # create reactive for subset plot on second tab
  s <- reactive({
    event_data("plotly_click", source = "sub_iris")
  })
  
  observeEvent(s(), {
    updateNavbarPage(session, inputId = "navbarID", selected = "Page_2")
  })
  
  # plot text on first page (test)
  output$text <- renderTable(req(s()))
  
  # this is the correct plot
  # but I want the user to be able to input their own data and override this
  output$ind_plot <- renderPlotly({
   # something like if s() draw this plot
   # else ggplot(iris, aes(x = input$species, y = input$sep_length).....

    req(s())
    iris_ind <- subset(iris)[subset(s(), curveNumber == 0)$pointNumber + 1,]
    ggplotly(
      ggplot(iris_ind, aes(x = Species, y = Sepal.Length)) +
        geom_bar(stat = "identity")
    )
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

Any help appreciated!!!

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