Search code examples
r

How to Display or Print Contents of Environment in R


I hope this question is not a duplicate, because I searched it didn't find any answer(If its a dupe, please let me know I shall remove it).

I am trying to print/display the contents of an environment, but I am unable to do it.

library(rlang)
e1 <- env(a = 1:10, b= letters[1:5])

When I use print, It just give me memory address not the contents(names and values) of that environment.

 > print(e1)
<environment: 0x00000000211fbae8>

Note: I can see the env. contents in R studio Environments tab, I am using R version: "R version 3.4.2" and rlang: rlang_0.2.0

My question is : What is the right function to print contents of an environment, Sorry the question may be naive, but I am unable to figure out. Thanks in advance


Solution

  • We can use get with envir parameter to get values out of specific environment

    sapply(ls(e1), function(x) get(x, envir = e1))
    
    #$a
    # [1]  1  2  3  4  5  6  7  8  9 10
    
    #$b
    #[1] "a" "b" "c" "d" "e"
    

    where

    ls(e1) # gives
    #[1] "a" "b"