Haribo
Haribo

Reputation: 2226

Avoid datatable greying out when recalculating

I have a very simple Shiny app to show the CPU usage and available memory of the shiny server :

ui <- fluidPage(
  titlePanel('Shiny Server Monitor'),
  DT::dataTableOutput("cpu")
)

server <- function(input, output, session) {


  output$cpu <- DT::renderDataTable({
     invalidateLater(1000)

    system("free -h > top.log")
    system("uptime > uptime.log")

    free <- readLines("top.log")
    uptime <- readLines("uptime.log")

    mem = strsplit(free, " ")
    available_mem =tail(mem[[2]],n=1)

    # I have 6 cores so normalized by deviding the load avarage to 6.
    load_ave = round(as.numeric(unlist(strsplit(unlist(strsplit(uptime, ","))[4],":"))[2])/6*100,2)

    dat = data.frame(load_ave,available_mem)
    colnames(dat) = c("CPU usage [%]", "Available memory [Gb]")


    DT::datatable(dat,rownames= FALSE)

  }) 

}

Since I'm using the invalidateLater command the output table is updating every second and therefore is blinking, which make everything very ugly.

Is there a way to fix this issue or present the result (especially the CPU usage) as htop command in Linux like : enter image description here

Upvotes: 1

Views: 293

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33417

Try adding

tags$style(type="text/css", ".recalculating {opacity: 1.0;}")

to your fluidPage.

Edit:

Sorry, the above works for a plotOutput. Concerning your datatable you will need to make it a client-side table (added server = FALSE in renderDataTable):

library(shiny)

ui <- fluidPage(
  titlePanel('Shiny Server Monitor'),
  DT::dataTableOutput("cpu")
)

server <- function(input, output, session) {



      output$cpu <- DT::renderDataTable({
        invalidateLater(1000)

        system("free -h > top.log")
        system("uptime > uptime.log")

        free <- readLines("top.log")
        uptime <- readLines("uptime.log")

        mem = strsplit(free, " ")
        available_mem =tail(mem[[2]], n=1)

        # I have 6 cores so normalized by deviding the load avarage to 6.
        load_ave = round(as.numeric(unlist(strsplit(unlist(strsplit(uptime, ","))[4],":"))[2])/6*100,2)

        dat = data.frame(Sys.time(), load_ave, available_mem)
        colnames(dat) = c("CPU usage [%]", "Available memory [Gb]")


        DT::datatable(dat, rownames= FALSE)

      }, server = FALSE) 

    }

    shinyApp(ui = ui, server = server)

Upvotes: 3

Related Questions