Reputation: 47
I'm not sure about this. Here is an example of a function which does not work:
myfunction<-function(){
mydata=read_excel("Square_data.xlsx", sheet = "Data", skip=0)
mydata$Dates=as.Date(mydata$Dates, format= "%Y-%m-%d")
mydata.ts=ts(mydata, start=2006, frequency=1)
}
The files do not load. When I execute each command line by line in R the files are loaded, so there's no problem with the commands. My question is, can I run a function such as myfunction to load the files? Thanks.
Upvotes: 2
Views: 16407
Reputation: 269556
Last statement in function is an assignment If the last executed statement in a function is an assignment then it will not display on the console unless you use print
but if the function result is assigned then you can print the assigned value later. For example, using the built in BOD
data frame:
> f <- function() bod <- BOD
> f() # no result printed on console because f() was not explicitly printed
> print(f()) # explicitly print
Time demand
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8
> X <- f() # assign and then print the assigned value
> X
Time demand
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8
Last statement in function is expression producing a result If the last statement produces a value rather than being an assignment then a result is printed on the console. For example:
> g <- function() BOD
> g()
Time demand
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8
Thus make sure that the last statement in your function is not an assignment if you want it to display on the console automatically.
Note 1: sourcing code Also, note that if your code is sourced using a source()
statement or if the code is called by another function then it also won't print automatically on the console unless you use a print
.
Note 2: Two results Regarding some comments to the question, if you want to output two results then output them in a named list. For example. this outputs a list with components named BOD and BOD2:
h <- function() list(BOD = BOD, BOD2 = 2*BOD)
h()
$BOD
Time demand
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8
$BOD2
Time demand
1 2 16.6
2 4 20.6
3 6 38.0
4 8 32.0
5 10 31.2
6 14 39.6
We could refer to them like this:
> H <- h()
> H$BOD
Time demand
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8
> H$BOD2
Time demand
1 2 16.6
2 4 20.6
3 6 38.0
4 8 32.0
5 10 31.2
6 14 39.6
Note 3: <<- operator Regarding the comments to the question, in general, using the <<-
operator should be avoided because it undesirably links the internals of your function to the global workspace in an invisible and therefore error-prone way. If you want to return a value it is normally best to return it as the output of the function. There are some situations where <<-
is warranted but they are relatively uncommon.
Upvotes: 6
Reputation: 21497
Sure. Just give it a value to be returned:
myfunction<-function(){
mydata=read_excel("Square_data.xlsx", sheet = "Data", skip=0)
mydata$Dates=as.Date(mydata$Dates, format= "%Y-%m-%d")
ts(mydata, start=2006, frequency=1) # The last object is returned by an R function
}
so calling dat <- myfunction()
will make dat
the ts-object that was created inside the function.
P.S.: There also in a return
function in R. As a best practice only use this if you want to return an object early, e.g. in combination with if
Upvotes: 1