Error with plotly in R Shiny app

shiny
plotly

#1

I am using plotly r to plot a chart in a R shiny app. The following is the error :

No applicable method for 'ggplotly' applied to an object of class "NULL

The same thing works fine when I do plot it in R but not when in shiny. Below is the data which is being plotted and the code used in R shiny.

   Month_considered pct.x pct.y   pct
   <fct>            <dbl> <dbl> <dbl>
 1 Apr-17            79.0  18.4  2.61
 2 May-17            78.9  18.1  2.99
 3 Jun-17            77.9  18.7  3.42
 4 Jul-17            77.6  18.5  3.84
 5 Aug-17            78.0  18.3  3.70
 6 Sep-17            78.0  18.9  3.16
 7 Oct-17            77.6  18.9  3.49
 8 Nov-17            77.6  18.4  4.01
 9 Dec-17            78.5  18.0  3.46
10 Jan-18            79.3  18.4  2.31
11 2/1/18            78.9  19.6  1.48



output$plot2 <- renderPlotly({
    p <- plot_ly()%>%
      layout(title = "Trend Over Time",
             xaxis = list(title = ""),
             yaxis = list (title = "Monthly Count of Products Sold") )

    colNames <- colnames(delta_plot())
    ToAdd <- setdiff(colnames(delta_plot()),"Month_considered")

    for(i in ToAdd){
      p <- p %>% add_trace(x =delta_plot()$ Month_considered, y = delta_plot()[[i]], name = i,
                           type = 'scatter',
                           mode = 'markers',
                           line = list(color = 'rgb(205, 12, 24)', width = 4))
    }

  })

#2

Not sure if this is the problem, but you are assigning things to p but not actually outputting it. You may just need to put a single p by itself on a line after your loop block, so that plot becomes the thing that renderPlotly is trying to render.


#3

@brianstamper I was not outputting p as you said . Once i did do it it all worked.Thank you.