I am trying to determine all the objects in a script. ( specifically to get all the dataframes but I'll settle for all the assigned objects ie vectors lists etc.) Is there a way of doing this. Should I make the script run in its own session and then somehow get the objects from that session rather than rely on the global environment.
Asked
Active
Viewed 86 times
-2
-
You could scrape up all the objects and check their class? Note also that you can source a script into its own environment, storing all the values hidden from plain sight. – Roman Luštrik Apr 27 '19 at 07:52
-
Thanks @RomanLustrik. I can check classes but how do you scrape up all the objects? – Sebastian Zeki Apr 27 '19 at 08:46
-
Check out these answers: https://stackoverflow.com/questions/5796508/loop-through-ls-or-objects – Roman Luštrik Apr 27 '19 at 10:32
1 Answers
1
Use the second argument to source() when you execute the script. For example, here's a script:
x <- y + 1
z <- 2
which I can put in script.R. Then I will execute it in its own environment using the following code:
x <- 1 # This value will *not* change
y <- 2 # This value will be visible to the script
env <- new.env()
source("script.R", local = env)
Now I can print the values, and see that the comments are correct
x # the original one
# [1] 1
ls(env) # what was created?
# [1] "x" "z"
env$x # this is the one from the script
# [1] 3
user2554330
- 30,741
- 4
- 34
- 76