Displaying 20 results from an estimated 221 matches for "myfun".
2010 Sep 03
3
S4 Method Signatures
Hello,
If the signature of a method defines which generic it implements then I'm confused about why this minimal example I invented won't work :
setGeneric("myFun", function(rs, ...){standardGeneric("myFun")})
setGeneric("myFun", function(cs, ...){standardGeneric("myFun")})
setMethod("myFun", "numeric", function(rs, colour = "Blue")
{
cat(rs*100, colour)
})
setMethod("myFun", "...
2003 Apr 17
4
A function as argument of another function
Dear all,
I would like to write a function like:
myfun<-function(x,fn) {xx<-exp(x); x*fn(xx)}
where fn is a symbolic description of any function with its argument to be
specified. Therefore
myfun(5,"2+0.3*y^2")
should return 5*(2+0.3*exp(5)^2),
myfun(5,"log(y)") should return 5*log(exp(5)) and so on.
I tried with "expres...
2006 Feb 02
2
How to get the namespace of a function?
I declared the environment of the function myfun to be NULL as follows:
environment(myfun) <- NULL
Later on I called that myfun and got an error message because the
function index() in the zoo package was called inside myfun and was
not visible:
Error in myfun(args) : couldn't find function "index"
I tried to use zoo::index()...
2011 Sep 26
4
Testing for arguments in a function
I don't understand how this function can subset by i when i is missing....
## My function:
myfun = function(vec, i){
ret = vec[i]
ret
}
## My data:
i = 10
vec = 1:100
## Expected input and behavior:
myfun(vec, i)
## Missing an argument, but error is not caught!
## How is subsetting even possible here???
myfun(vec)
Is there a way to check for missing function arguments, *and* which...
2011 Feb 02
1
pass nrow(x) to dots in function(x){plot(x,...)}
Dear Rers,
I have a function to barplot() a matrix, eg
myfun <- function(x, ...) { barplot(x , ... )}
(The real function is more complicated, it does things to the matrix first.)
So I can do:
m1 <- matrix(1:20,4)
myfun(m1)
myfun(m1, main="My title")
I'd like to be able to add the number of rows of the matrix passed to
the function t...
2015 Oct 22
2
Changed behaviour when passing a function?
..., as far as it goes, but the symbol being resolved is
FUN, so when looking for a function it doesn't find the function version of
round.
Did you perhaps have a function named FUN in your global environment? If so
you are being bitten by what I mentioned before.
> FUN = function(...) 1
> myfun <- function(x, FUN, ...){
+ FUN(x, ...)
+ }
> round <- 2
> myfun(0.85, FUN = round, digits=1)
[1] 1
~G
On Thu, Oct 22, 2015 at 2:25 PM, Duncan Murdoch <murdoch.duncan at gmail.com>
wrote:
> On 22/10/2015 1:59 PM, Joris Meys wrote:
>
>> Hi all,
>>
>>...
2013 Jun 07
1
It seams that fast99 function (sensitivity package) does not work out for norm distribution.
...problem as mine when using the fast99
(extended-FAST method) to perform SA of model with norm distribution inputs?
See the simple example given following.
Any suggestion will be greatly appreciated.
Thank you!
Marino
# Simple example
# 1. uniform version (It works well)
library(sensitivity)
Myfun<-function(x){return(rowSums(x))}
SA1 <- fast99(model = Myfun, factors = 3, n = 1000, q = "qunif", q.arg =
list(min = 0, max = 1))
> SA1
Call:
fast99(model = Myfun, factors = 3, n = 1000, q = "qunif", q.arg =
list(min = 0, max = 1))
Model runs: 3000
Estimations o...
2023 Aug 03
3
feature request: optim() iteration of functions that return multiple values
Dear all,
I have used optim a lot in contexts where it would useful to be able to iterate function myfun that, in addition to the primary objective to be minimized ('minimize.me'), could return other values such as alternative metrics of the minimization, informative intermediate values from the calculations, etc.
myfun <- function()
{
...
return(list(minimize.me = minimize.me, R2 = R2, p...
2009 Jun 12
1
how to trigger variable creation?
Hello R users,
i'm wondering how to trigger variable creation.
Whenever a variable is created i want my own function myFun(...) to be
started.
if (exists("x")) {rm(x)} # after removal of x
# any of these calls
x<-10 # should call myFun
x=10 # should call myFun
assign(x,10) # should call myFun
etc.
# aft...
2002 Jan 07
1
Mishandling missing "..." (PR#1247)
R> myfun <- function(x, ...) {x[...] <- 0; x}
R> myfun(3)
Error in myfun(3) : SubAssignArgs: invalid number of arguments
It fails because no ... was passed. The workaround (and desired behavior) is:
R> myfun <- function(x, ...) {if (missing(...)) x[] <- 0 else x[...] <- 0; x}
R>...
2011 Jul 23
1
call a function with explicitly not setting an argument
Is there a way to call a function, and explicitly set an argument to 'not
specified'? My situation is the following. I have a function which passes on
most of its arguments to another function. The second function, myfun2,
serializes all arguments and is out of my control.
myfun <- function(...){
return(myfun2(...));
}
now, the value for arguments of myfun are stored in variables. Say I want to
call it with arguments 'foo' and 'bar', which are stored in variables
'myfoo' and 'myb...
2009 Jun 26
3
changing default arguments of a function and return the modified function as a result
Dear R-users,
I am trying to develop a function that takes another function as an argument,
changes its default values and returns a list of things, among which the
initial function with its default arguments changed. An example of what i
will like to obtain below:
## initial function
myfun <- function(x, a=19, b=21){ return(a * x + b) }
## this is the function i will like to create
## (does not work as it is written here)
mysecond.fun <- function(a, b, c = myfun(a=2, b=15)){
return(list(a=a, b=b c=c))
}
So I would be able to call:
mysecond.fun$c(x=12)
And this will be equ...
2006 Sep 27
2
How to pass expression as an argument
Hi,
I am writing a function and need to pass a function expression as an argument, for instance,
myfun <- function( express) {
x<- c(1,2,3)
y<-express
}
if I call the above function by myfun( x*2 ), I get 2 as the result, instead of 2,4,6 , could someone help me to
fix this problem ?
Furthermore, is that possible to operate thi...
2011 Oct 11
2
stop()
Suppose I have a function, such as the toy example below:
myFun <- function(x, max.iter = 5) {
for(i in 1:10){
result <- x + i
iter <- i
if(iter == max.iter) stop('Max reached')
}
result
}
I can of course do this:
myFun(10, max.iter = 11)...
2007 Feb 23
1
how to use apply with two variables
Hi,
this is a made-up example. Function "myfun" returns two arguments. Can
"apply" be used so that "myfun" is called only once?
Thanks
Serguei
mat<-matrix(runif(50),nrow=10,ncol=5)
myfun<-function(x) {
mymean<-mean(x)
mysd<-sd(x)
return(mymean,mysd)
}
out1<-t(apply(mat,1,function(x) myfun(x)$mymean...
2011 Feb 03
1
mapply question (?)
Hi,
I have a function myFun which I want to call multiple times, using different
argument lists.
myFun("v1", "2009", 1)
myFun("v2", "2008", 1)
myFun("q", "2001")
How can I easily do this in R? Should I use mapply?
I unsuccessfully tried something like:
x <- lis...
2012 Jul 05
1
Extracting srcref for S4 methods
...2.15.1 (2012-06-22) (Platform: i686-pc-linux-gnu (32-bit))
sourced functions have srcref attached as an attribute.
Are such data also available for S4 generics and methods? How? (See
sample code below)
Thank you.
Bests,
Renaud
f <- textConnection(
"
f <- function(){}
setGeneric('myfun', function(x, ...) standardGeneric('myfun'))
setMethod('myfun', 'numeric', function(x, ...){ 'myfun,numeric' })
")
source(f, keep.source=TRUE)
# there are srcref data for functions
str(f)
# no srcref data
str(myfun)
str(selectMethod(myfun, 'numeric'...
2015 Oct 22
3
Changed behaviour when passing a function?
Hi all,
When teaching this year's class, I was quite amazed that one of my examples
didn't work any longer. I wanted to illustrate the importance of
match.fun() with following code:
myfun <- function(x, FUN, ...){
FUN(x, ...)
}
round <- 2
myfun(0.85, FUN = round, digits=1)
I expected to see an error, but this code doesn't generate one. It seems as
if in the current R version match.fun() is added automatically.
I've scrolled through the complete R News section spe...
2006 Apr 20
1
Bootstrap error message: Error in statistic(data, origina l, ...) : unused argument(s) ( ...)
...In all other cases statistic must take at least two
arguments. The first argument passed will always be
the original data. The second will be a vector of
indices, frequencies or weights which define the
bootstrap sample. [...]
Your "myFun" clearly does not fit that description. boot() tried to call
"myFun" with a second argument, but "myFun" doesn't know what to do with a
second argument, and that's where you get the error.
Andy
> What's wrong with my bootstraping code?
>
> Thanks...
2010 Aug 11
2
storing the results of an apply call
Hi R-users,
I have a function (myfun) that I want to apply to the rows of a matrix.
Basically, "myfun" takes the values from the matrix ("exp.des"), which
represent the different combinations of my experimental design, and pass
them as arguments to some other functions (fun1 and fun2). As I want to
replicate the re...