R: from file import foo as bar

r import function
could not find function "import" r
r importfrom
r import module
r packages
import function r
r package dependencies
how to import packages in r studio

I am looking for a technique that combines the following two questions:

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

The R equivalent of Python from x import y as z

In other words, I want to import a specific function from someone else's .r file. Under a different name.

We call source with local=TRUE inside a new function, and return only the needed function:

source1 <- function(path,fun){
  source(path, local= TRUE)
  get(fun)
}

from x import y as z will be written:

z <- source1(x,y) # where y is a string

Example:

# create 'test.R' file in working directory
write("test  <- function(a,b) a + b
      test2 <- function(a,b) a - b",
      "test.R")

new_fun <- source1("test.R","test2")

new_fun
# function(a,b) a - b
# <environment: 0x0000000014873f08>

test
# Error: object 'test' not found

test2
# Error: object 'test2' not found

# clean up
unlink("test.R")

import, The import package provides an alternative approach to using external functionality in R File: bar.R # Desc: Functionality related to bars. # Imports from foo. So a file_A in package Foo imports file_B, also in Foo. In Package Bar, there's a file_X, and in file_X I want to import file_A. I can import file_B just fine into file_X, and file_A can import file_B just fine when running file_A, however, I cannot for the life of me import file_A into file_X, as it gives me this error:

You can use source as follows:

In test.R script:

test <- function() message("Hello")

Then, source that file using

someone <- new.env()
source("test.R", someone)

To call someone's code, use

someone$test()

If possible, ask that someone to write a R package.

[PDF] Package 'modules', (character | module) a module as file or folder name or a list representing a Now we create a module with foo and bar as member functions. You can declare imports similar to what we would do in a R package: we list  No matter how many times you use @importFrom foo bar you’ll only get one importFrom(foo, bar) in your NAMESPACE. This makes it easy to attach import directives to every function that need them, rather than trying to manage in one central place. Note that you can choose to use roxygen2 to generate just NAMESPACE, just man/*.Rd, or both.

AFAIK there is no such mechanism in R.

Of course you can do

x.R y <- function() {do_something}

z.R source("x.R") z <- y rm(y)

A better options would be to make x.R into a package. Then you simple do z <- x::y

The best solution is to convert both x.R and z.R into packages and use @importFrom x y into package z and don't bother with changing the name of the function.

klmr/modules: An alternative module system for R, This will import the code from a file with the name tools/strings.r , located either Assuming you have such a module foo , inside which is a submodule bar , you  The import package provides an alternative approach to using external functionality in R programs; first however, it is useful to describe the standard approach to clarify how import may serve as improvement. The most common way to include the functionality provided by a package is to use the library function:

Namespaces · R packages, The package namespace (as recorded in the NAMESPACE file) is one of the Namespaces make your packages self-contained in two ways: the imports and the No matter how many times you use @importFrom foo bar you'll only get one  I usually put this documentation in a file called <package-name>.R. It’s also a good place to put the package level import statements that you’ll learn about in imports. Documenting classes, generics and methods. It’s relatively straightforward to document classes, generics and methods. The details vary based on the object system you’re

namespace, imports, The package namespace (as recorded in the NAMESPACE file) is one of the No matter how many times you use @importFrom foo bar you'll only get one  Since the 709x was also a 36 bit machine, foobar may have been used as a common file name there. Foo and bar were also commonly used as file extensions. Since the text editors of the day operated on an input file and produced an output file, it was common to edit from a .foo file to a .bar file, and back again.

[PDF] A Simple Implementation of Name Spaces for R, R 2e)≡. # empty file. The purpose of baz is to import some of the exports of foo and bar and re-export them, using renaming in one case: bar's  Define all functions in one .R file, call them from another .R file. How, if possible? The R equivalent of Python from x import y as z. In other words, I want to import a specific function from someone else's .r file. Under a different name.

Comments
  • @musefan how can my question be made clearer?
  • Well you could write one for starters... you haven't put anything. You can't just link 2 questions and that's it. Your question needs to be able to stand on it's own, not rely on links
  • How awesome is that?! I still can't get used to how things by default are global and mutable.