Define all functions in one .R file, call them from another .R file. How, if possible?

G Shah

How do I call functions defined in abc.R file in another file, say xyz.R?

A supplementary question is, how do I call functions defined in abc.R from the R prompt/command line?

A_K

You can call source("abc.R") followed by source("xyz.R") (assuming that both these files are in your current working directory.

If abc.R is:

fooABC <- function(x) {
    k <- x+1
    return(k)
}

and xyz.R is:

fooXYZ <- function(x) {
    k <- fooABC(x)+1
    return(k)
}

then this will work:

> source("abc.R")
> source("xyz.R")
> fooXYZ(3)
[1] 5
> 

Even if there are cyclical dependencies, this will work.

E.g. If abc.R is this:

fooABC <- function(x) {
    k <- barXYZ(x)+1
    return(k)
}

barABC <- function(x){
    k <- x+30
    return(k)
}

and xyz.R is this:

fooXYZ <- function(x) {
    k <- fooABC(x)+1
    return(k)
}

barXYZ <- function(x){
    k <- barABC(x)+20
    return(k)
}

then,

> source("abc.R")
> source("xyz.R")
> fooXYZ(3) 
[1] 55
>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to extract one or more words from a string and search for them in two different columns form another file in R

Export and import functions and code from one file to another in R

How to call functions from another php file?

How to call functions from another file in python

Bash: How to call all functions of a file in one single call from the command line?

Is it possible to define more than one function per file in MATLAB, and access them from outside that file?

How to call one file from another file in protractor angular

How to import all classes in one file and use them from there typescript

Call dataframe from one R file to a second file

Understanding how to call functions from another source file in C

Is it possible to define the title in an R Markdown file as a header?

How to move one file from a directory to another no R using just a part of the file name?

How to import shell functions from one file into another?

Not able to call functions from another JS file

React: Cannot call functions from another file

How can I find all the possible combinations of elements in column vector and cross check them from another dataframe in R?

How to call function from one js file to another

Import functions from one python file to another

How to concatenate using ## when one of them is return from an another define

Call from one file a UserForm in another

How to define mysqli connection in one php file and then use it on another file?

How to define constant in one file and access in another file in angularjs

How to find all file owned by one user and copy them to another directory in RHEL 8?

Compare one column from one file with all columns in another file

pip install -r all dependencies from the file except one

How to call methods from class located in one python file from another python file

How to displaying all data from all file_get_contents & search one of them

How to define functions and data from another namespace, not in the global one?

Exporting all functions in a workspace to an R source file