3

I have about 10 ploty graphs converted from ggplot is in my shiny app, which is refreshing every 10 seconds. Plotly works fine for few refresh, but then shows ERROR too many open devices.

My code is as follows (shorted to just show one graph):

server.R

pullData is function that pulls data from database.

library(lubridate)
library(shinyjs)
library(ggplot2)
library(plotly)
server <- function(input, output, session) {


d <- reactive({
  invalidateLater(10000, session)
  pullData() %>% filter(!is.na(time))
})

output$Run <- renderPlotly({
 pdf(NULL)
 ggplotly(ggplot(d(), aes(x = as.POSIXlt(time) , y = mile)) +
   geom_point() +
   theme_bw() +
   xlab('Time') +
   ylab('mile'))
})

ui.R

library(shinydashboard)
library(shiny)
library(shinyjs)
library(plotly)

ui <- dashboardPage(

  dashboardHeader(title = "Analytics DashBoard")
  ,skin = 'green'
    ,dashboardSidebar(
      tags$head(
      tags$style(HTML("
                      .sidebar { height: 90vh; overflow-y: auto; }
                      " )
      )
    ),
#       sidebarSearchForm(label = "Search...", "searchText", "searchButton"),
      sidebarMenu(


        , menuItem("Real Time Graphs", tabName = "RealTimeG", icon = icon("cog"))


      )

    )


   ,dashboardBody(
     tabItems(
     ,tabItem(
        tabName = "RealTimeG"


       ,fluidRow(
         box(
         title = "total Run Time"
         ,plotlyOutput("Run")
         , width = 6
         )
       )
   )
))

What is issue ? and How to solve it ?

1 Answer 1

3

I had the same problem. I solved it by using dev.off() in renderPlotly function. try doing something like this:

utput$Run <- renderPlotly({
 pdf(NULL)
 g<-ggplotly(ggplot(d(), aes(x = as.POSIXlt(time) , y = mile)) +
 geom_point() +
 theme_bw() +
 xlab('Time') +
 ylab('mile'))
 dev.off()
 g
 })

Looks like shiny creates/opens (not sure) new graphics device every time the plot is refreshed. Yo can check this by printing dev.list() in your shiny app. You will get something like this after few refresh:

RStudioGD       png       pdf       pdf       pdf       pdf       pdf       pdf       pdf       pdf 
        2         3         4         5         6         7         8         9        10        11 
  pdf       pdf       pdf       pdf       pdf       pdf       pdf       pdf       pdf       pdf 
   12        13        14        15        16        17        18        19        20        21 
  pdf       pdf       pdf       pdf       pdf       pdf       pdf       pdf       pdf       pdf 
   22        23        24        25        26        27        28        29        30        31 
  pdf       pdf       pdf       pdf       pdf       pdf       pdf       pdf       pdf       pdf 
   32        33        34        35        36        37        38        39        40        41 
  pdf       pdf       pdf       pdf       pdf 
   42        43        44        45        46 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.