Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work for list of functions (?) #17

Closed
savemark opened this issue Feb 1, 2018 · 3 comments
Closed

Does not work for list of functions (?) #17

savemark opened this issue Feb 1, 2018 · 3 comments
Assignees
Labels

Comments

@savemark
Copy link

savemark commented Feb 1, 2018

funlist <- function(par) {f <- function(x) x^par; return(list(f = f))}
myfun <- funlist(2) # x^2

# is.function(myfun$f)
# [1] TRUE

# myfun$f
# function(x) x^par
# <environment: 0x000000002e1b72b8>

Deriv(myfun$f)
Error in get(stch, mode = "function", envir = env) : 
  object 'myfun$f' of mode 'function' was not found
@savemark
Copy link
Author

savemark commented Feb 1, 2018

Nevermind, I did find a solution:

list2env(myfun, environment())
Deriv(f)
#function (x) 
#par * x^(par - 1)

I'll leave it here in case anyone else have the same problem.

@savemark savemark changed the title Does not work for closures? Does not work for list of functions (?) Feb 1, 2018
@sgsokol
Copy link
Owner

sgsokol commented Feb 1, 2018

Right. As the error message suggests, get() cannot use expression with $ or any subindexing to find an object. It must be a plain name like f in your second example.

@sgsokol sgsokol self-assigned this Feb 1, 2018
@sgsokol sgsokol closed this as completed Feb 1, 2018
@sgsokol
Copy link
Owner

sgsokol commented Feb 15, 2018

After some reflexion, I have found a way to circumvent get() limitations and now (i.e. starting from v3.8.4) your example gives:

(fd<-Deriv(myfun$f))
# function (x) 
# par * x^(par - 1)
# <environment: 0x29034b8>
fd(3)
# [1] 6

Please feel free to report any problem with proposed solution.

PS. The version 3.8.4 is not on CRAN yet (as of this writing) but you can test it with usual

devtools::install_github("sgsokol/Deriv")

@sgsokol sgsokol reopened this Feb 15, 2018
@sgsokol sgsokol closed this as completed Feb 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants