Show Memory Use in App

Is it possible to show real time memory use of my shiny app as an output in my app (similar fo RStudio memory allocation icon in the environment view)? Of course chatGPT says I can do this but then of course it does not work since the function apparently does not work:

Any suggestions on how to meet this requirement?

library(shiny)
library(profvis)

ui <- fluidPage(
  h1("Memory Usage"),
  verbatimTextOutput("memory")
)

server <- function(input, output) {
  output$memory <- renderPrint({
    profvis::profvisMemory()
  })
}

shinyApp(ui, server)

I just left a related example here using pryr::mem_used():

library(DT)
library(pryr)
library(shiny)

current_mem_used_pretty_size <- reactive({
  invalidateLater(1000L)
  format(structure(mem_used(), class="object_size"), units="auto", standard="SI")
})

ui <- fluidPage(
  uiOutput("start_new_session"),
  textOutput("memory_used_session_start"),
  textOutput("memory_currently_used"),
  div(actionLink("close_session", "Close this session"), style = "margin-bottom:20px;"),
  DTOutput("example_table")
)

server <- function(input, output, session) {
  observeEvent(input$close_session, {
    session$close()
  })
  example_data <- reactive({
    data.frame(replicate(100L, runif(10000L)))
  })
  output$example_table <- renderDT({example_data()}, options = list(scrollX = TRUE))
  output$start_new_session <- renderUI({
    clientData <- reactiveValuesToList(session$clientData)
    link <- with(clientData, paste0(url_protocol, "//", url_hostname, ":", url_port, url_pathname))
    div(a("Open new session", target="_blank", href = link, title = link), style = "margin-top:10px;")
  })
  output$memory_used_session_start <- renderText({
    mem_used_pretty_size <- format(structure(mem_used(), class = "object_size"), units = "auto", standard = "SI")
    paste0(mem_used_pretty_size, " memory used after starting this session (", session$token, ")")
  })
  output$memory_currently_used <- renderText({
    paste0(current_mem_used_pretty_size(), " memory currently in use (", format(Sys.time(), usetz = TRUE, digits = 0L), ")")
  })
  onStop(function(){
    cat("Stopped session", session$token, "\n")
    # gc(verbose = TRUE)
    print(mem_used())
  })
}

shinyApp(ui, server)

Also see this related post.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.