Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing object memory usage in app on R shiny server

Tags:

r

shiny

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?

like image 570
robanche Avatar asked Nov 27 '25 05:11

robanche


1 Answers

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))
        }))
      )
    })
  }
))
like image 102
Yihui Xie Avatar answered Nov 29 '25 19:11

Yihui Xie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!