Re: [R] Newbie: Examples on functions callling a library etc.

2008-08-29 Thread Eduardo M. A. M.Mendes
Hi

Many thanks.  I got another post to this list that gives an example of what
I need exactly, that is, require and ::

Cheers

Ed

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Ben Bolker
Sent: Thursday, August 28, 2008 11:44 PM
To: [EMAIL PROTECTED]
Subject: Re: [R] Newbie: Examples on functions callling a library etc.

Eduardo M. A. M.Mendes  gmail.com> writes:

> R is pretty new to me. I need to write a function that returns three
> matrices of different dimensions.  In addition, I need to call a function
> from a contributed package with the function.   I have browsed several
> manuals and docs but the examples on them are either very simple or
> extremely hard to follow.
> 
> Many thanks
> 
> Ed
> 

  I think you need to try to specify your needs a little bit
more carefully.  Here is a function that technically meets
your needs:

library("example_pkg")  ## to load the contributed package
myfunction <- function() {  ## function with no arguments
   foo()## assuming the function "foo" is in the package
   list(matrix(nrow=2,ncol=2),matrix(nrow=3,ncol=3),matrix(nrow=4,ncol=4)
}

  But I suspect that doesn't really do what you need ...

  Ben Bolker

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Newbie: Examples on functions callling a library etc.

2008-08-29 Thread Eduardo M. A. M.Mendes
Hi Steve

That is exactly what I want. Many thanks.

Ed


-Original Message-
From: Steven McKinney [mailto:[EMAIL PROTECTED] 
Sent: Thursday, August 28, 2008 11:48 PM
To: Eduardo M. A. M.Mendes; r-help@r-project.org
Subject: RE: [R] Newbie: Examples on functions callling a library etc.


Hi Ed, 

Here's a simple example showing your needs:


myfun <- function(n1, n2, n3) {
  mat1 <- matrix(rep(1), nrow = n1, ncol = 3)
  mat2 <- matrix(rep(2), nrow = n2, ncol = 4)
  mat3 <- matrix(rep(3), nrow = n3, ncol = 5)

  require("survival") ## make sure the package you need is loaded
  mypkgfun <- survival::is.Surv ## Use the '::' and ':::' extractors to get
visible and hidden functions respectively from the package

  list(mat1 = mat1, mat2 = mat2, mat3 = mat3, mypkgfun = mypkgfun) ## Return
the items in a list
}

## Now invoke the function
foo <- myfun(n1 = 1, n2 = 1, n3 = 5)

## and look at the returned results
foo

> myfun <- function(n1, n2, n3) {
+   mat1 <- matrix(rep(1), nrow = n1, ncol = 3)
+   mat2 <- matrix(rep(2), nrow = n2, ncol = 4)
+   mat3 <- matrix(rep(3), nrow = n3, ncol = 5)
+ 
+   require("survival")
+   mypkgfun <- survival::is.Surv
+ 
+   list(mat1 = mat1, mat2 = mat2, mat3 = mat3, mypkgfun = mypkgfun)
+ }
> 
> foo <- myfun(n1 = 1, n2 = 1, n3 = 5)
> 
> foo
$mat1
 [,1] [,2] [,3]
[1,]111

$mat2
 [,1] [,2] [,3] [,4]
[1,]2222

$mat3
 [,1] [,2] [,3] [,4] [,5]
[1,]33333
[2,]33333
[3,]33333
[4,]33333
[5,]33333

$mypkgfun
function (x) 
inherits(x, "Surv")


> 

HTH

Steve McKinney


-Original Message-
From: [EMAIL PROTECTED] on behalf of Eduardo M. A. M.Mendes
Sent: Thu 8/28/2008 5:43 PM
To: r-help@r-project.org
Subject: [R] Newbie:  Examples on functions callling a library etc.
 
Hello

 

R is pretty new to me. I need to write a function that returns three
matrices of different dimensions.  In addition, I need to call a function
from a contributed package with the function.   I have browsed several
manuals and docs but the examples on them are either very simple or
extremely hard to follow.

 

Many thanks

 

Ed

 

 

 


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Newbie: Examples on functions callling a library etc.

2008-08-28 Thread Steven McKinney

Hi Ed, 

Here's a simple example showing your needs:


myfun <- function(n1, n2, n3) {
  mat1 <- matrix(rep(1), nrow = n1, ncol = 3)
  mat2 <- matrix(rep(2), nrow = n2, ncol = 4)
  mat3 <- matrix(rep(3), nrow = n3, ncol = 5)

  require("survival") ## make sure the package you need is loaded
  mypkgfun <- survival::is.Surv ## Use the '::' and ':::' extractors to get 
visible and hidden functions respectively from the package

  list(mat1 = mat1, mat2 = mat2, mat3 = mat3, mypkgfun = mypkgfun) ## Return 
the items in a list
}

## Now invoke the function
foo <- myfun(n1 = 1, n2 = 1, n3 = 5)

## and look at the returned results
foo

> myfun <- function(n1, n2, n3) {
+   mat1 <- matrix(rep(1), nrow = n1, ncol = 3)
+   mat2 <- matrix(rep(2), nrow = n2, ncol = 4)
+   mat3 <- matrix(rep(3), nrow = n3, ncol = 5)
+ 
+   require("survival")
+   mypkgfun <- survival::is.Surv
+ 
+   list(mat1 = mat1, mat2 = mat2, mat3 = mat3, mypkgfun = mypkgfun)
+ }
> 
> foo <- myfun(n1 = 1, n2 = 1, n3 = 5)
> 
> foo
$mat1
 [,1] [,2] [,3]
[1,]111

$mat2
 [,1] [,2] [,3] [,4]
[1,]2222

$mat3
 [,1] [,2] [,3] [,4] [,5]
[1,]33333
[2,]33333
[3,]33333
[4,]33333
[5,]33333

$mypkgfun
function (x) 
inherits(x, "Surv")


> 

HTH

Steve McKinney


-Original Message-
From: [EMAIL PROTECTED] on behalf of Eduardo M. A. M.Mendes
Sent: Thu 8/28/2008 5:43 PM
To: r-help@r-project.org
Subject: [R] Newbie:  Examples on functions callling a library etc.
 
Hello

 

R is pretty new to me. I need to write a function that returns three
matrices of different dimensions.  In addition, I need to call a function
from a contributed package with the function.   I have browsed several
manuals and docs but the examples on them are either very simple or
extremely hard to follow.

 

Many thanks

 

Ed

 

 

 


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Newbie: Examples on functions callling a library etc.

2008-08-28 Thread Ben Bolker
Eduardo M. A. M.Mendes  gmail.com> writes:

> R is pretty new to me. I need to write a function that returns three
> matrices of different dimensions.  In addition, I need to call a function
> from a contributed package with the function.   I have browsed several
> manuals and docs but the examples on them are either very simple or
> extremely hard to follow.
> 
> Many thanks
> 
> Ed
> 

  I think you need to try to specify your needs a little bit
more carefully.  Here is a function that technically meets
your needs:

library("example_pkg")  ## to load the contributed package
myfunction <- function() {  ## function with no arguments
   foo()## assuming the function "foo" is in the package
   list(matrix(nrow=2,ncol=2),matrix(nrow=3,ncol=3),matrix(nrow=4,ncol=4)
}

  But I suspect that doesn't really do what you need ...

  Ben Bolker

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Newbie: Examples on functions callling a library etc.

2008-08-28 Thread Eduardo M. A. M.Mendes
Hello

 

R is pretty new to me. I need to write a function that returns three
matrices of different dimensions.  In addition, I need to call a function
from a contributed package with the function.   I have browsed several
manuals and docs but the examples on them are either very simple or
extremely hard to follow.

 

Many thanks

 

Ed

 

 

 


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.