Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Display or Print Contents of Environment in R

Tags:

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

like image 987
PKumar Avatar asked May 07 '18 04:05

PKumar


People also ask

How do you show environment in R?

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.

How do you access the object of the environment in R?

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.

How do I print the console in R?

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.


2 Answers

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"
like image 56
Ronak Shah Avatar answered Oct 12 '22 10:10

Ronak Shah


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"
like image 39
akrun Avatar answered Oct 12 '22 09:10

akrun