abraham
abraham

Reputation: 31

Writing and running R functions

I am having problem in running functions in R. Whenever I want to run them I always get the message that the function/object is not available. though I saved it in the working directory. For it to run, I have to paste it in the R window. Any idea on what I am missing highly appreciated.

Upvotes: 0

Views: 176

Answers (2)

Gabriel123
Gabriel123

Reputation: 456

So as exposed above, there are several steps to follow. It won't work if one of them is missing :-)

1) Set your source folder

setwd("///.../admin/Documents") 

2) Create your function and save it:

fun = function(...){
your function
}

dump("fun", file = "Code_fun.R")

3) Call your function

source("Code_fun.R")

Then you can go ahead

fun(3) = ...

Upvotes: 0

Matthew Lundberg
Matthew Lundberg

Reputation: 42629

Use the source function to read the file.

If you do this a lot, I recommend the package devtools, or RStudio. Or both.

Upvotes: 3

Related Questions