7
votes

I've created a Shiny app, which contains some Plotly objects. All works fine when previewing in RStudio. Though, when I deploy the app to Shiny server (on-premise) and open the app, the plots are not shown. When looking at the developer console in the browser (Chrome or Safari) I see the following error Can't find variable: Plotly which points to line 141 of plotly.js, which is var plot = Plotly.plot(graphDiv, x) in the following code:

  //if no plot exists yet, create one with a particular configuration

  if (!instance.plotly) {

    var plot = Plotly.plot(graphDiv, x);
    instance.plotly = true;
    instance.autosize = x.layout.autosize || true;
    instance.width = x.layout.width;
    instance.height = x.layout.height;

I tested this in Chrome and Safari, and it happens in both browsers, though when I refresh the page in Chrome the plots sometimes work.

So to test this further, I took this code from the Plotly website, and deployed it to our Shiny server environment to see if the same behaviour occurs, which is the case. As such I don't think I made a coding mistake.

I'm using the following package versions:

  • plotly 4.7.1
  • shiny 1.0.5
  • htmlwidgets 1.0
  • Shiny Server v1.5.3.838

Does anyone know how to solve this? Am I missing some packages or does something need to be configured in Shiny server to make this work?

Thanks!

1
Is rstudio server installed on your server? You could try to run your code in rstudio and see if it can help you detect the problem you are having.MLavoie
We do have that installed on the same server, and I tried the app also from rstudio.. it works fine and plots are shown properly. The problem occurs when the app is deployed to shiny server.dwillemsen
I have a very similar issue as well. I get 500 error also if I use a example provided by them here: plotly-r.com/linking-views-with-shiny.htmlGiacomo

1 Answers

0
votes

I think what you may need is renderPlotly instead of renderPlot in your server. If you do this, the plot will show up on the viewer, but not in the shiny popup (blank graph) and won't work when deployed. An example of this where renderPlot has been replaced by renderPlotly is:

ui <- dashboardPage(skin = "blue",
                dashboardHeader(title= "Fig. 11: State Forecasting and Nowcasting", titleWidth = 450),
                dashboardSidebar(disable=TRUE),
                dashboardBody(
                  fillPage(padding = 0,
       box(title="", id="normal", solidHeader = TRUE, status = "primary", width=7,
                plotlyOutput("plot1", height = 250)), 
        box(title="State to Examine", background = "black", width=5, 
                selectInput("variable", "State:", choices= sort(unique(afteradd2$State)), selected="Ohio")),
       box(title="Forecast Length", background = "black", width=5, 
                selectInput("variable.two", "Forecast:", choices= c("Nowcast", "Three Month Forecast",
                                                             "Six Month Forecast"), selected="Nowcast"))
        )))
server <- function(input, output) {


output$plot1<- renderPlotly({
par(mfrow=c(1,1))
par(mar = c(4, 5, 4, 4)) 
fstate(input$variable, input$variable.two)})}


shinyApp(ui=ui, server=server)

Without seeing your code it's difficult to know for sure though. If this isn't your issue could you share your code so I can try to help?