robanche
robanche

Reputation: 63

Listing object memory usage in app on R shiny server

I have been trying to display the memory usage of each object used in my R Shiny app when run on R Shiny Server, so as to identify memory leakages. In R, I can call ls() or objects() and get all that information. As soon as I try displaying this info in a Shiny app, either through renderText() or renderDataTable() off of a dataframe, it's all blank. I am guessing there is an issue with the environment the functions ls() and objects() are looking in. Anybody ran into that issue before?

Upvotes: 3

Views: 2455

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30184

Here is a short example (the key is to specify the environment that you want to investigate):

library(shiny)
runApp(list(
  ui = fluidPage(
    tableOutput('foo')
  ),
  server = function(input, output) {
    x1 <- 1:100
    x2 <- rbind(mtcars, mtcars)
    env <- environment()  # can use globalenv(), parent.frame(), etc
    output$foo <- renderTable({
      data.frame(
        object = ls(env),
        size = unlist(lapply(ls(env), function(x) {
          object.size(get(x, envir = env, inherits = FALSE))
        }))
      )
    })
  }
))

Upvotes: 6

Related Questions