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
Global environment can be referred to as . GlobalEnv in R codes as well. We can use the ls() function to show what variables and functions are defined in the current environment. Moreover, we can use the environment() function to get the current environment.
env() . Another useful way to view an environment is ls. str() . It is more useful than str() because it shows each object in the environment.
Print values on R console or file using cat() or print()function can be used to print the argument. Print also returns the argument so it can be assigned.
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"
We can use mget
mget(ls(e1), envir = e1)
#$a
#[1] 1 2 3 4 5 6 7 8 9 10
#$b
#[1] "a" "b" "c" "d" "e"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With