# souce code and reusing it

#### ledzep

##### Point Mass at Zero
This Stack-Overflow thread made me realize that I can reuse my custom functions.
Like that OP, I have some custom written functions which I use on regular basis. I am feeling a bit tired copying the code and pasting it on editor every-time I want to use it. I didn't follow the answers completely...

Consider the following simple example.
Code:
## Custom function to work out exponential probability
foo<- function(x,k){
if(x<0){
out<-"Not Allowed: Illegal x ranges"
} else {
out<-k*exp(-k*x)
}
return(out)
}
1. Can I save the code somewhere and read it from there and use it without having to paste the code on the editor?
[I know the answer is Yes].

2. What format do it save the script as? .txt or as .RFile and where? Can I create a working directory and put my script there and load it every-time I want to use?

3. How do I load it into Editor. Using source function?
Code:
setwd("C:/test") ## directory where code is
## Supose I call my code file exponential.
source("C:/test/exponential.RFile") ## this doesn't work...

#### Dason

R scripts are typically saved with a .R extension but that doesn't matter too much. Why doesn't source("C:/test/exponential.RFile") work for you? If what you have above is saved in that file then if you call source("C:/test/exponential.RFile") then you should be able to use foo now.

#### ledzep

##### Point Mass at Zero
Couldn't figure out the additional warning which says no such file..

Code:
> source("C:/test/exponential.RFile")
Error in file(filename, "r", encoding = encoding) :
cannot open the connection
In file(filename, "r", encoding = encoding) :
cannot open file 'C:/test/exponential.RFile': No such file or directory

#### Dason

What is the output of dir("C:/test")

#### ledzep

##### Point Mass at Zero
What is the output of dir("C:/test")
Ah, I see!

Code:
> dir("C:/test")
[1] "exponential.R"
So, the extension name I was using when loading the source dose was wrong before. Works fine now.
Code:
source("C:/test/exponential.R") ## load the source code
> foo(1,4)
[1] 0.07326256
Thanks.

#### Dason

If you have a lot of R scripts you want to source at any given time you could look at the example for source to see how you could write a function to easily source all of the .R files in a directory at one time. You could stick that function definition in the .First function in your .Rprofile file so that that function is available to you upon startup.

#### Dason

Or you could just make an R package

It's not as bad as it sounds.

#### trinker

##### ggplot2orBust
zep if you want to make a package I know I'm willing to help you through the process. Dason has given a ton to me so if I can pass it on I will. But the process of making a package (particularly if you don't want to put it on CRAN) is easy once you get it down.

#### ledzep

##### Point Mass at Zero
Dason said:
Or you could just make an R package
It's not as bad as it sounds.
I am pretty keen to learn package development [although the sound of writing a package scares me, I always think I ain't smart enough].

zep if you want to make a package I know I'm willing to help you through the process. Dason has given a ton to me so if I can pass it on I will. But the process of making a package (particularly if you don't want to put it on CRAN) is easy once you get it down.
Thanks trinker. I want to learn it.
May be start with something really simple. Something like, containing all my custom functions. I just load the package and use my functions. I am sure with all you guys around, I can get there.
Besides, being able to write a package makes you the cool guy out there
Where do I start?

#### victorxstc

##### Pirate
ledzep, shouldn't you use backslashes for the local addresses, instead of slashes? I see every address of yours is with slashes. I thought it might cause some error though I'm not pretty sure. Maybe R parses slashes automatically to backslashes.

#### Dason

ledzep, shouldn't you use backslashes for the local addresses, instead of slashes? I see every address of yours is with slashes. I thought it might cause some error though I'm not pretty sure. Maybe R parses slashes automatically to backslashes.
Why don't you give it a try? Windows typically uses backslashes for addresses (which is stupid). However a backslash is a way to "escape" a control character so if you want to use a Windows address you either have to use two backslashes (so write something like "C:\\path\\to\\my\\file.txt") or R is smart enough to say screw that - just use slashes like the sane file systems and we'll parse it just fine for you.

#### victorxstc

##### Pirate
I didn't know backslashes are Windows specific. I thought that since they are used in DOS (and DOS is copied somehow from UNIX) they are some cross-platform consensus that slashes are used for online addresses and backslashes for local ones. I should work with Ubuntu more. But anyways it is good that R parses them for the user's comfort.

#### trinker

##### ggplot2orBust
@LedZep

The easiest way to get started is with Rstudio. I also recommend you set up an account with github (this makes it easier to help you out with problems you're having). Here is a video I made on projects in bitbucket. Bitbucket is very similar to github. If you're not interested in bitbucket or github that's ok the video still shows you how to create a package.

1. Rstudio
2. devtools --> use: install.packages("devtools")

I've learned somethings about package building since I made the video but it's still good for learning how to make a package.

Also very good is this video that uses just Rstudio to make a package in under 2 minutes: http://www.youtube.com/watch?v=ScV7XXlBZww

This should get you started. Both videos show things the other may not. The pure Rstudio one is likely a more efficient approach but you may not understand as much about what RStudio is doing for you with it.

#### Dason

You don't really need git/github to get started with package development though. And you don't actually NEED RStudio - it's really devtools that does most of the work to make it easy.

#### trinker

##### ggplot2orBust
@Zep how's the building coming?

#### ledzep

##### Point Mass at Zero
Trinker, Thanks.
I really loved those two videos. They were very useful.

@Zep how's the building coming?
Singed up the github. I was very annoyed for the fact that my name (ledzep) was already taken up. So, I added talkstats next to my name . I have followed you there. github looks like a large community.
I was trying to convert an example function to a package. However, I had bit trouble with loading devtools. Something to do with Rtools..

Here's the package build and error.
Code:
* installing to library 'C:/Users/aaa/Documents/R/win-library/2.14'
* installing *source* package 'talkstats' ...
** R
[COLOR="red"]No man pages found in package  'talkstats' [/COLOR]
** help
*** installing help indices
** building package indices ...
** testing if installed package can be loaded

* DONE (talkstats)
[COLOR="#a0522d"]WARNING: Rtools is required to build R packages however is not currently installed. Please download and install the appropriate version of Rtools before proceeding:[/COLOR]
http://cran.r-project.org/bin/windows/Rtools/
And here's the warning when I loaded the devtools. Package seems to be unpacked and installed, but throws warning when loading it..
Code:
> install.packages("devtools")
Installing package(s) into ‘C:/Users/aaa/Documents/R/win-library/2.14’
(as ‘lib’ is unspecified)
Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/2.14
trying URL 'http://cran.ma.imperial.ac.uk/bin/windows/contrib/2.14/devtools_0.6.zip'
Content type 'application/zip' length 92101 bytes (89 Kb)
opened URL

package ‘devtools’ successfully unpacked and MD5 sums checked

> require(devtools)
[COLOR="red"]Rtools not on path and not installed in default location.
[/COLOR]Warning message:
package ‘devtools’ was built under R version 2.14.2

#### Dason

You'll need to install the appropriate version of Rtools. Make sure when you install it that the checkbox that appears at some point asking if you want to add Rtools to the PATH is checked. If you don't have it checked then you have to manually add Rtools to your path later and that's annoying.

#### ledzep

##### Point Mass at Zero
Thanks Dason. More errors.
Code:
==> Rcmd.exe INSTALL --no-multiarch talkstats2

* installing to library 'C:/Program Files/R/R-2.15.2/library'
Error: ERROR: no permission to install to directory 'C:/Program Files/R/R-2.15.2/library'

Exited with status 1
When I try to install devtools, I get further warning
Code:
> require(devtools)
Error in loadNamespace(i[[1L]], c(lib.loc, .libPaths())) :
there is no package called ‘RCurl’
and
Code:
Warning message:
package ‘RCurl’ is not available (for R version 2.15.2)

#### ledzep

##### Point Mass at Zero
Thanks Dason.

## Reminder/note to self: install Rcurl using the link below
Code:
install.packages("RCurl", contriburl = "http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/2.15/")