Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Functions into Current Namespace

Tags:

r

Let's say I have an R source file comprised of some functions, doesn't matter what they are, e.g.,

fnx = function(x){(x - mean(x))/sd(x)} 

I would like to be able to access them in my current R session (without typing them in obviously). It would be nice if library("/path/to/file/my_fn_lib1.r") worked, as "import" works in Python, but it doesn't. One obvious solution is to create an R Package, but i want to avoid that overhead just to import a few functions.

like image 534
doug Avatar asked Nov 21 '09 16:11

doug


People also ask

How do you import a namespace in Python?

Importing is a way of pulling a name from somewhere else into the desired namespace. To refer to a variable, function, or class in Python one of the following must be true: The name is in the Python built-in namespace. The name is the current module's global namespace.

How do I import a function from a module?

To make use of the functions in a module, you'll need to import the module with an import statement. An import statement is made up of the import keyword along with the name of the module. In a Python file, this will be declared at the top of the code, under any shebang lines or general comments.


1 Answers

Use the source() command. In your case:

source("/path/to/file/my_fn_lib1.r") 

Incidentally, creating a package is fairly easy with the package.skeleton() function (if you're planning to reuse this frequently).

like image 54
Shane Avatar answered Sep 24 '22 17:09

Shane