-2

I would like to know how to run different functions from different R script files.

For example, in Main.R:

source("Database.R")
msci_data <- getIndex() #function from Database.R

source("Positions.R")
current_positions <- getPositions() #function from Positions.R

I realized after running getPositions() method , my msci_data data frame gets deleted. Is there anyway I can call multiple functions from two different source files?

Thanks very much

duckmayr
  • 16,303
  • 3
  • 35
  • 53
Fabian Tan
  • 15
  • 6
  • 2
    It's impossible to tell what's going on without knowing what's in, at the least, `Positions.R` – duckmayr May 02 '19 at 10:56
  • its just that when i run source("Positions.R") the msci_data is gone... – Fabian Tan May 02 '19 at 12:14
  • 1
    I understand that's the **effect** you are observing from running `source("Positions.R")`, but to uncover the **cause** of that effect, it will be necessary for us to see the *contents* of Positions.R. In other words, this is not general behavior of sourcing R scripts, so it must be something in the code of Positions.R that is causing this; therefore, we cannot help you without seeing the code in Positions.R – duckmayr May 02 '19 at 12:20
  • thanks for your prompt reply. i suspect that because source("Position.R") is executed, it sort of delete away the functions in source("Database.R")? – Fabian Tan May 02 '19 at 12:23
  • 1
    No, it does not. Not unless there is code **within** Positions.R that causes this behavior. It is **not** a general behavior of sourcing multiple R scripts. I will add a brief demonstration that sourcing multiple R scripts does not remove anything from your global environment, but no one can help you actually solve your problem without seeing your code. Please see [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – duckmayr May 02 '19 at 12:27
  • 1
    If data frame(s) are being removed, look for any `rm()` calls. Some scripts begin by cleaning out environment: `rm(list=ls())`. – Parfait May 02 '19 at 12:43
  • 1
    Thanks everyone for your reply. @Parfait is right... there is a rm() line...careless... – Fabian Tan May 02 '19 at 23:19

1 Answers1

1

Here is a short demonstration that, in general, sourcing multiple R scripts will not remove anything from your global environment.

I have in a file foo.R:

foo <- function(x) x^2

I then have in a file bar.R:

bar <- function(x) x^3

Then from main.R, I do the following:

x <- 1:10
ls()
# [1] "x"

source("foo.R")
foo(x)
# [1]   1   4   9  16  25  36  49  64  81 100
ls()
# [1] "foo" "x"  

source("bar.R")
bar(x)
# [1]    1    8   27   64  125  216  343  512  729 1000
ls()
# [1] "bar" "foo" "x"  

You can see the functions all work as expected, and nothing is ever removed from the global environment. It must be that something in your Positions.R file is what causes this behavior, so no one can help you solve your problem without seeing your code.

duckmayr
  • 16,303
  • 3
  • 35
  • 53