-2

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.

Sebastian Zeki
  • 6,430
  • 8
  • 47
  • 113

1 Answers1

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