Listing dataframes, matrices and so on in the application

Hi all,

I have used the below code to see the dataframes, matrices, functions and so on in my environment.
But is there a way to use this below code in server.R so that I can see what all dataframes, matrices are there in my application? I tried below code in my server.R but it not listing . Can anyone please help?

purrr::map_dfr(ls(envir=.GlobalEnv),~tibble(object_name = .,object_class=class(get(.))[[1]]))

Are you looking for something like this?

library(shiny)
library(purrr)
ui <- pageWithSidebar(
  headerPanel("My First Shiny App"),
  
  sidebarPanel( 

),     
mainPanel(
  tableOutput("myTable")
)
)

server <- function(input, output, session){
  
  output$myTable <-renderTable({  
    purrr::map_dfr(ls(envir=.GlobalEnv),
                   ~tibble(object_name = .,
                           object_class=class(get(.))[[1]]))
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

The result of ls() depends on the environment for which it is called. You specified the Global Environment so it returns objects at that level. I modified the code to have ls() calls in three different places with the result stored in the variable called Obj. If you change which one is active (be sure to have comment marks on the other two), you can see the different results.

library(shiny)
library(purrr)
MyDF <- data.frame(A = 3)
MAT <- matrix(1:4, nrow = 2)
X <- 34
#Obj <- ls()
ui <- pageWithSidebar(
  headerPanel("My First Shiny App"),
  
  sidebarPanel( 
    
  ),     
  mainPanel(
    tableOutput("myTable")
  )
)

server <- function(input, output, session){
  #Obj <- ls()
  output$myTable <-renderTable({
    InnerDF <- data.frame(Z = 1:5)
    Obj <- ls()
    purrr::map_dfr(Obj,
                   ~tibble(object_name = .,
                           object_class=class(get(.))[[1]]))
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

yes exactly. But actually this is listing functions that is used outside my shiny application. MAy I know why?

Thanks for the time you have given. Almost 80 of the problem is solved. :slight_smile: I have modified your code a little bit. Actually let us say there are lots of functions in server.R. Do i have to name Obj <- ls() everytime ? Please refer below section3

Also from your logic the output I get is below in section1

section1 below.

object_name	     object_class
InnerDF	         data.frame

Can we add another column and tell user that it is coming from myTable as shown below is section2

section2 below. Expected output

object_name	     object_class        output
InnerDF	         data.frame          myTable

section3 below

library(shiny)
library(purrr)

asd2 <- function(a,b){
  c <- a + b
  return(c)
}

MyDF <- data.frame(A = 3)
MAT <- matrix(1:4, nrow = 2)
X <- 34
#Obj <- ls()       #declared first time
ui <- pageWithSidebar(
  headerPanel("My First Shiny App"),
  
  sidebarPanel( 
    
  ),     
  mainPanel(
    tableOutput("myTable"),
    tableOutput("myTable1")
  )
)

server <- function(input, output, session){
  #Obj <- ls()
  output$myTable <-renderTable({
    InnerDF <- data.frame(Z = 1:5)
    Obj <- ls()   #declared second time . Can we make this one time decalaration?
    purrr::map_dfr(Obj,
                   ~tibble(object_name = .,
                           object_class=class(get(.))[[1]]))
  })
  
  output$myTable1 <-renderTable({
    InnerDF1 <- data.frame(Z = 1:32)
    Obj <- ls()
    purrr::map_dfr(Obj,
                   ~tibble(object_name = .,
                           object_class=class(get(.))[[1]]))
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

Adding the name of the output element is easy, though inelegant, with code like this.

  output$myTable <-renderTable({
    InnerDF <- data.frame(Z = 1:5)
    Obj <- ls()
    lsDF <- purrr::map_dfr(Obj,
                   ~tibble(object_name = .,
                           object_class=class(get(.))[[1]]))
    lsDF$output <- "MyTable"
    lsDF
  })

Since every function defines a new environment when it is called, I think you have to have a ls() inside of each function to capture the objects unique to it. It is possible to look at environments preceding the function, but that is getting beyond anything I have done. If you want to look into that, there is a chapter about environments in the Advanced R book.

Thanks, But your are passing lsDF$output <- "MyTable". Actually it should be reactive right. because we have myTable in output$myTable. What i meant actually was this output. But actually you tried passing as a object. Makes sense?