mysum <- function(x,y){
#this is my sum function and it is used for..
#john's updated version, x in int, y in double.
k <- sum(x,y)
return(k)
}
say I have a mysum function and wanted to see the functionality and some basic info, how do one extract the function's info?
Instead of looking into the script directly, would like to have something similar like ?mysum or mysum.info that can print out that two line of comment/info from my console. Thanks.
Related
I need help with creating a custom function in R.
I don't know how to create a function Y, where the 3 inputs of the function must be: n as in integer, mu (dimension m) as a vector, and sigma as a matrix (dimension mxm).
Does anyone know how to do that or have any idea of how can I reach it?
Thank you all :)
Note: I know how to create functions where the inputs are integers, but I have no idea how to proceed in this case
MyFun <- function(x,y,z)
{
# write a custom function
x*y*z
}
output1 <- MyFun(1,2,3)
output1
I'm having troubles understanding how the ...parameter works in R, I can't find a complete documentation, is it some kind of editable object?
I'm writing an R package and I would like to write a function that uses the dots, to pass them to another function (plot), but I want to pass some parameters to the inner function only if they are not in .... I know that using list(...) I can check if they have been specified, but I don't know if I can add them to ... otherwise. What's the best way to make this work?
I tried to pass an edited pars= list(...) to the inner function plot(...= pars), but it didn't work. Actually in the end I found a solution, which is updating the list pars trough modifyList and then using do.call to pass them to the inner function, but this feels a bit intricate to me, there is any simpler solution?
You can just pass ... along as so:
# weird way to construct a linear function
f0 <- function (x, b=1) x+b
f1 <- function (x, a=2, ...){
# INITIAL WRONG ANSWER stopifnot(exists("b")) # see comments
if("b" %in% names(list(...))){
f0(a*x, ...)
}else{
f0(a*x, b=4, ...) # Only makes sense if b=4 is a default that has meaning
# in f1 but not outside of f1 ; or if you cannot change
# the definition of f0 (imported functions). Otherwise,
# you'd better change the default in f0.
}
}
f1(10)
f1(10, b=3)
Arthur and Janhoo made some good suggestions, but in the end I see that the straightforward way I hoped to find doesn't exist. So in the end the best solution to me is the one I sketched in the question:
pars_user= list(...)
pars_default= list(a= 1, b= 2)
pars_fixed= list(c= 3, d= 4)
pars= modifyList(pars_default, pars_user)
pars= modifyList(pars, pars_fixed)
do.call(function, pars)
x<-2, y<-4
sum.xy <- function(x){
function(y){x + y}
}
In the above code, I do not figure out why sum.xy() just give a non-numerical value, can anybody explain?
the ouput of any function in R is either inside return or if there is no return (like in your case) then the last "printed object" is returned. In your, the function sum.xy returns a function: namely the function function(y){x+y}.
You can test this like this:
x<-2; y<-4
sum.xy <- function(x){
function(y){x + y}
}
class(sum.xy)
class(sum.xy(7))
sum.xy(7)
So sum.xy(7) is actually a function which for given y returns y + 7.
If you write sum.xy(7)(4) then 7+4 is returned.
I also recommend that you take a look at this chapter of advanced R.
I'd like to be able to create a vector with R but determine its name within the function call. In SAS I would use the macro language to perform a loop but with R, I can't find how to refer to a variable by name e.g. (Obviously this does not work but it describes what I'd like to do)
fun <- function(X, vectorName) {
paste(vectorName) <- 1:X
}
I'd like to be able to call fun(5, v) and get a vector v = c(1,2,3,4,5) out the end.
Although this is possible, it's not something you should do. A function should only have a return value, which you then can assign, e.g.:
v <- seq_len(5)
Or if you have to pass a variable name programmatically:
myname <- "w"
assign(myname, seq_len(5))
(Though I can't think of a reason why you'd need that.)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
General suggestions for debugging R?
When debugging, I would often like to know the value of a variable used in a function that has completed executing. How can that be achieved?
Example:
I have function:
MyFunction <- function() {
x <- rnorm(10) # assign 10 random normal numbers to x
return(mean(x))
}
I would like to know the values stored in x which are not available to me after the function is done executing and the function's environment is cleaned up.
You mentioned debugging, so I assume the values are not needed later in the script, you just want to check what is happening. In that case, what I always do is use browser:
MyFunction <- function() {
browser()
x <- rnorm(10) # assign 10 random normal numbers to x
return(mean(x))
}
This drops you into an interactive console inside the scope of the function, allowing you to inspect what is happening inside.
For general information about debugging in R I suggest this SO post.
MyFunction <- function() {
x <- rnorm(10) # assign 10 random normal numbers to x
return(list(x,mean(x)))
}
This will return a list where the first element is x and the second is its mean
You have many options here. The easiest is to use the <<- operator when you assign to x. It's also the most likely to get you into trouble.
> test <- function() x <- runif(1)
> x <- NA
> test()
> x
[1] NA
> test <- function() x <<- runif(1)
> test()
> x
[1] 0.7753325
Edit
#PaulHeimstra points out that you'd like this for debugging. Here's a pointer to some general tricks:
General suggestions for debugging in R
I'd recommend either setting options(error=recover) or using trace() in combination with browser().
There are already some good solutions, I'd like to add one possibility. I emphasize on the fact that you want to know the value of a variable used in a function that has completed executing. So there is maybe no need to assign those values, and you don't want (a priori) to stop execution. The solution is to simply use print. So it is not used by default but only when you want to debug, the option to print or not can be passed as a function argument:
MyFunction <- function(x, y, verbose = FALSE) {
a <- x * y
if (verbose) print(a)
b <- x - y
if (verbose) print(b)
return(a * b)
}
In general, you would run your function like this: MyFunction(10, 4) but when you want to see those intermediate results, do MyFunction(10, 4, verbose = TRUE).