Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R match.fun does not see function from imported package

Tags:

function

r

match

I've imported package called "stabledist". It includes function "rstable"

When I do this

my_fun <- function(function_from_library)
{
  function_from_library <- match.fun(function_from_library)
  print(some_data <- function_from_library)
}

my_fun (5, rstable(5, alpha = 1.7, beta = 0, gamma = 1.0, delta = 1.0))

I get error:" Error in match.fun(some_distr) : 'rstable(5, alpha = 1.7, beta = 0, gamma = 1, delta = 1)' is not a function, character or symbol "

Everything works fine, whrn match.fun is deleted. Is there anyway to import library that it can be visible to others? Or I can just skip match.fun?

like image 409
Demaunt Avatar asked Mar 26 '26 17:03

Demaunt


1 Answers

This is how I implemented my suggestion:

 library(stabledist )
 my_fun <- function(function_from_library, ...)
 {
   function_from_library <- match.fun(function_from_library)
   print(some_data <- function_from_library(...))
 }

 my_fun ( rstable, 5, alpha = 1.7, beta = 0, gamma = 1.0, delta = 1.0)
#[1]  1.4600308688 -0.0004999279  1.9301805374 -1.3276383194  0.9137183709

It does require also knowing how to use the ellipsis-mechanism for passing lists of arbitrary length to functions as Roland had additionally commented. The print mechanism will not actually create a data-vector of values. To do that you would need to assign ("<-") the result "outside" the function body (and so the print() call is not needed either).

 library(stabledist )
 my_fun <- function(function_from_library, ...)
 {
   function_from_library <- match.fun(function_from_library)
   function_from_library(...)     }

 some_data <- my_fun ( rstable, 5, alpha = 1.7, beta = 0, gamma = 1.0, delta = 1.0)
 some_data
 # 5 random values are printed at console.
like image 87
IRTFM Avatar answered Mar 28 '26 05:03

IRTFM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!