2

I'm new to Shiny and have been developing a web app that displays a plotly chart; I'm still developing the app on my local machine. When I run the app for the first time after opening RGui, the web app runs but does not render the chart, even after I have clicked the "Draw" button. I have to refresh the webpage once or twice and then the chart will render. Once the chart has rendered, the problem is gone for the duration of that R session. Refreshing the page or restarting the shiny program will continue to render the chart, until RGui is closed. The problem reliably recurs the next time I open RGui and run the app.

All the existing questions and answers I have searched on shiny and failed rendering have not answered my question.

After several frustrated attempts at trying to find what was wrong in my program, I boiled it down to this code that looks (to me) like it should be functional, but still presents the issue:

library(shiny)
library(plotly)

ui = fluidPage(
  plotlyOutput("Plot"),
  actionButton("drawPlotButton", "Draw")
)

server = function(input, output)
{
  output$Plot = renderPlotly({
    input$drawPlotButton
    return(plot_ly(mtcars, x = ~hp, y = ~mpg, type = "scatter", mode = "markers"))
  })
}

shinyApp(ui = ui, server = server)

I can't tell if I am missing something simple or what. All help is appreciated.

params
  • 66
  • 6
  • This code works ok on my machine, using `Rstudio Version 1.2.1114, R version 3.5.0 (I should update soon..), shiny_1.2.0 & plotly_4.9.0`. What versions are you running? You can run `sessionInfo()` to find out – Jonny Phelps Jul 12 '19 at 15:47
  • @JonnyPhelps I'm running `R version 3.5.3`, `plotly_4.9.0`, and `shiny_1.3.2` Seeing as @RK1 in his answer doesn't have an issue either, I wonder if my setup is somehow malfunctional... – params Jul 12 '19 at 16:06
  • 1
    It might just be a problem with the gui. In Rstudio you can ask to open it in the browser instead. Maybe explore if it's an issue if you try changing what you use to view the app – Jonny Phelps Jul 12 '19 at 16:37

1 Answers1

0

Generally runs fine for me as in the plot renders at initialization. However, if your objective is to plot the graph only when the Draw button is clicked then:

You need to add a eventReactive method

See r Shiny action button and data table output

library(shiny)
library(plotly)

ui = fluidPage(
  plotlyOutput("Plot"),
  actionButton("drawPlotButton", "Draw")
)

server = function(input, output)
{
  plot_d <- eventReactive(input$drawPlotButton, {
    plot_ly(mtcars, x = ~hp, y = ~mpg, type = "scatter", mode = "markers")
  })

  output$Plot = renderPlotly({
    plot_d()
  })
}

shinyApp(ui = ui, server = server)
RK1
  • 2,384
  • 1
  • 19
  • 36
  • While `eventReactive` is something I didn't know about, the main issue here is simply that the plot doesn't render at initialization. Since you aren't experiencing this issue, I have to wonder why my setup might be broken. Thanks! – params Jul 12 '19 at 16:08
  • Cool, @JonnyPhelps makes a valid point, worth viewing the app in your browser and seeing if the error still occurs – RK1 Jul 12 '19 at 21:01