rshinypinggauge

Reactive gauge with ping results in R shiny


I'm building a dashboard for work which needs to display some ping results to different servers. My idea was to build a R shiny dashboard to display some gauges with these ping results. I've already made a working gauge and a working loop which can ping different servers. I've searched for a solution on the internet for this but I can't seem to get these two things to work together. I think I need to use reactive gauges but i'm not really used to programming in R.

This is what I got so far in the body:

#Body
#-----------------------------------------------------------------------
body <- dashboardBody(
          tabItems(
            #First tab content
            tabItem(tabName = "dashboard",
              #Connection speeds
              #-----------------------------------------------------------------------
              fluidRow(
                h2("Server connections and speeds"),
                column(6,box(flexdashboard::gaugeOutput("out1"),width=12,title="Gauge Graph")),
                column(6,box(flexdashboard::gaugeOutput("out2"),width=12,title="Gauge Graph 2"))
              )
            )
          )
        )

And these are my server functions:

#Server functions
#=======================================================================
server <- shinyServer(function(input, output, session) {

#server section
    ping_result_cw <- ping_port("google.com")
    print(ping_result_cw[1])
    output$out1 <- flexdashboard::renderGauge({
      gauge(ping_result_cw[1], min = 0, max = 250, symbol = 'ms', label = paste("Verbinding Careware"),gaugeSectors(
        success = c(250, 150), warning = c(150,50), danger = c(0, 50), colors = c("#CC6699")
      ))

    })
    output$out2 <- flexdashboard::renderGauge({
      gauge(26, min = 0, max = 250, symbol = 'ms', label = paste("Verbinding Ksyos"),gaugeSectors(
        success = c(100, 6), warning = c(5,1), danger = c(0, 1), colors = c("#CC6699")
      ))
    })
  })
}
#Run UI
#=======================================================================
shinyApp(ui = ui, server = server)

The second gauge is a control gauge here to see if my changes have come through. I haven't put a loop in this part of the code, because when I do it will fail. Does anybody know how I can get that my code will ping every 5 seconds (probably with Sys.sleep(5) in the loop) and will update the gauge? I got it to work with the ping loop running once, but then the gauge would only show the first ping result.

So in short: I need a dashboard in R shiny with mulptiple gauges which display the ping result that update every 5 seconds. Thank you in advance!


Solution

  • We can use reactive and invalidateLater from Shiny to do it.

    ## Read in necessary libraries, function, and data 
    library(shiny)
    library(shinydashboard)
    library(flexdashboard)
    ui <- dashboardBody(
        tabItems(
            #First tab content
            tabItem(tabName = "dashboard",
                    #Connection speeds
                    #-----------------------------------------------------------------------
                    fluidRow(
                        h2("Server connections and speeds"),
                        column(6,box(flexdashboard::gaugeOutput("out1"),width=12,title="Gauge Graph")),
                        column(6,box(flexdashboard::gaugeOutput("out2"),width=12,title="Gauge Graph 2"))
                    )
            )
        )
    )
    #Server functions
    #=======================================================================
    server <- shinyServer(function(input, output, session) {
        
        #server section
        ping_result_cw <- reactive({
            invalidateLater(5000)
            pingr::ping_port("google.com", timeout = 3)
        })
        output$out1 <- flexdashboard::renderGauge({
            gauge(ping_result_cw()[1], min = 0, max = 250, symbol = 'ms', label = paste("Verbinding Careware"),gaugeSectors(
                success = c(250, 150), warning = c(150,50), danger = c(0, 50), colors = c("#CC6699")
            ))
            
        })
        output$out2 <- flexdashboard::renderGauge({
            gauge(26, min = 0, max = 250, symbol = 'ms', label = paste("Verbinding Ksyos"),gaugeSectors(
                success = c(100, 6), warning = c(5,1), danger = c(0, 1), colors = c("#CC6699")
            ))
        })
    })
    }
    #Run UI
    #=======================================================================
    shinyApp(ui = ui, server = server)
    

    enter image description here