Mary A. Marion
Mary A. Marion

Reputation: 800

calling functions without copying into script window

The default library for my R installation is C:\Users\mmstat\Documents\R\win-library\3.3\

I also have functions with .txt extensions saved in a second folder c:\S library\

My problem is that I don't know how to tell R about my function library 'S library' so I don't have to copy and paste the code for the wanted function in my script window and then executing it.

How can I do this?

Upvotes: 1

Views: 60

Answers (2)

Mitra
Mitra

Reputation: 855

NB: I don't have windows so the paths might need some editing to works properly on your own system.

NB 2: you might need to change the extension of the files from .txt to .R

if you want to create/use it as a package, you can give a try to the modules package. It was built with python users in mind.

If you are using these functions frequently, I would recommend adding a variable to your .Rprofile as well (for Unix-based system it is usually: ~/.Rprofile, no idea for windows)

import.path='c:/S library/' #Based on Ben Bolker recommendation

If you don't want or can't modify your profile, you can also do it from within R (but you would need to do it everytime).

And then in R:

#only the first time and if you want to update later on.
require(devtools) ## you will need to install it if you don't have it already
devtools::install_github('klmr/modules') 

And then everytime you want to use it, it would be something as the following:

library(modules)
options(import.path="c:\\S library\\")
Slib=import('nameOftextFile')#example myFun

and then you can use it:

foo=Slib$myFun(arg1,arg2,)

Please read the documentation for a better explanation.

Upvotes: 1

Ben Bolker
Ben Bolker

Reputation: 226871

Just to give the simplest base-R answer:

fList <- list.files(path="c:/S library/",pattern="*.txt")
lapply(fList,source)

Upvotes: 2

Related Questions