Plot with action button depending on 2 inputs updates immediately instead of waiting on button

Hi everyone,

I'm trying to make something which has the following characteristics:

  • It does not show anything at the start (no plot output), and
  • It only updates after clicking the button (not when changing the first or second var)

So far I've used observeEvent and eventReactive but both have not yet worked. For this question I'm using iris as an example. I've used many different techniques such as observeEvent & eventReactive, but I've no clue what I can do to get it to work. Can anyone explain or show how I could get this to work?

Code:

library(shiny)
library(datasets)
library(ggplot2)
library(plotly)
data(iris)

ui <- shinyUI(pageWithSidebar(
  headerPanel("plot example"),
  
  sidebarPanel(
    
    selectInput("var1", "First var",
                list("Sepal length" = "Sepal.Length",
                     "Sepal width"  = "Sepal.Width",
                     "Petal length" = "Petal.Length",
                     "Petal width"  = "Petal.Width")),
    
    selectInput("var2", "Second var",
                list("Petal length" = "Petal.Length",
                     "Petal width"  = "Petal.Width",
                     "Sepal length" = "Sepal.Length",
                     "Sepal width"  = "Sepal.Width")),
    actionButton("gobutton", "Go")
  ),
  
  mainPanel(
    plotlyOutput("plot"))
))


# Define server logic required to plot variables
server <- shinyServer(function(input, output, session){
  
  # I want the plot only to update if I press "Go"
  # observe event only does this the first time
  #  observeEvent(eventExpr = {
  #          input$gobutton
  #  },
  #          handlerExpr = {
  # output$plot <- renderPlotly({
  #         iris_plot <- ggplot(iris, aes_string(x=input$var1, 
  #                                      y=input$var2, 
  #                                      colour="Species")) + geom_point()
  #         ggplotly(iris_plot)
  # })
  #          })
  
  # this does not update it, it only works the first time
  inputVar <- eventReactive(input$gobutton, {
    return(iris)
  })
  
  output$plot <- renderPlotly({
    iris_plot <- ggplot(data=inputVar(), aes_string(x=input$var1, 
                                         y=input$var2,
                                         colour="Species")) + geom_point()
    ggplotly(iris_plot)
  })
  
  # this below is the regular code
  # output$plot <- renderPlotly({
  #                  iris_plot <- ggplot(iris, aes_string(x=input$var1,
  #                                               y=input$var2,
  #                                               colour="Species")) + geom_point()
  #                  ggplotly(iris_plot)
  # })
  
})

shinyApp(ui = ui, server = server)
server <- shinyServer(function(input, output, session){
  
iris_plot <- eventReactive(input$gobutton, {
  ggplot(data = iris, aes_string(
    x = req(input$var1),
    y = req(input$var2),
    colour = "Species"
  )) +
    geom_point()
})

output$plot <- renderPlotly({
  ggplotly(req(iris_plot()))
})
})

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.