R error: could not find function when loading from file

mgcdanny

This is a bit tricky for me to describe but please see the example below. I am trying to isolate scope of some R scripts by loading the scripts into a function. But this doesn't work when loading 'nested' functions. Per example below, the function 'inside' can be called after being loaded, but then the function 'outside' errors out saying it can not find the function 'inside.'

#this would be in some file
inside <- function(a, b){
  return(a+b)
}    

outside <- function(c, d){
  inside(c, d)
}    

save.image("my_r_functions.model")    

rm(list = ls())    

#this would be in some other file
wrapper <- function(d, e){
  load("my_r_functions.model")
  print(paste('inside works: ', inside(d,e)))
  print('but outside can not find inside')
  outside(d,e)
}    

wrapper(1,2)

output:

[1] "inside works:  3"
[1] "but outside can not find inside"
Error in outside(d, e) : could not find function "inside"
jeborsel

You didn't specify where you wanted it loaded. Just add envir=globalenv() (or envir=environment(wrapper)) to the call to load.

 wrapper <- function(d, e){
     load("my_r_functions.model",envir=environment(wrapper))
     print(paste('inside works: ', inside(d,e) ))
     print('but outside can not find inside')
     outside(d,e)
 }    

wrapper(1,2)

will work

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Could not find function "year" after installation from zip file R

From Dev

R Shiny error: could not find function "shell"

From Dev

Error "Could not find function" in 'caret' package, R

From Dev

by() error in r: could not find function "FUN"

From Dev

r :Error: could not find function -User defined

From Dev

r :Error: could not find function -User defined

From Dev

R Shiny error: could not find function "shell"

From Dev

R parallel: error "could not find function "%do%""

From Dev

"could not find function" only when in the R debugger

From Java

Error: could not find function "%>%"

From Dev

Error: could not find function

From Dev

"Could not cast" error in XCTest when loading from storyboard

From Dev

Could not find function in r

From Dev

R error "could not find function 'multiplot' " using Cookbook example

From Dev

### ** Examples ... Error: could not find function building packages in R

From Dev

Error: could not find function “rxGetOption” in Revolution R Open

From Dev

R - Parallelization in EasyABC. Error: ... could not find function

From Dev

R Parallel Programming: Error in { : task 1 failed - "could not find function "%>%""

From Dev

Error: could not find function "guides"

From Dev

R Markdown could not find function

From Dev

R nls : could not find function "a"

From Dev

Unbound variable error when loading a procedure from an external file

From Dev

Getting error when loading image file from Parse

From Dev

Why won't this code display Error: Could not find file when e is thrown by a nonexistant file?

From Dev

Function not found in R doParallel 'foreach' - Error in { : task 1 failed - "could not find function "raster""

From Dev

Function not found in R doParallel 'foreach' - Error in { : task 1 failed - "could not find function "raster""

From Dev

foreach error "could not find function "%do%""

From Dev

Error: could not find function "read.DNAStringSet"

From Dev

Data table error could not find function "."

Related Related

  1. 1

    Could not find function "year" after installation from zip file R

  2. 2

    R Shiny error: could not find function "shell"

  3. 3

    Error "Could not find function" in 'caret' package, R

  4. 4

    by() error in r: could not find function "FUN"

  5. 5

    r :Error: could not find function -User defined

  6. 6

    r :Error: could not find function -User defined

  7. 7

    R Shiny error: could not find function "shell"

  8. 8

    R parallel: error "could not find function "%do%""

  9. 9

    "could not find function" only when in the R debugger

  10. 10

    Error: could not find function "%>%"

  11. 11

    Error: could not find function

  12. 12

    "Could not cast" error in XCTest when loading from storyboard

  13. 13

    Could not find function in r

  14. 14

    R error "could not find function 'multiplot' " using Cookbook example

  15. 15

    ### ** Examples ... Error: could not find function building packages in R

  16. 16

    Error: could not find function “rxGetOption” in Revolution R Open

  17. 17

    R - Parallelization in EasyABC. Error: ... could not find function

  18. 18

    R Parallel Programming: Error in { : task 1 failed - "could not find function "%>%""

  19. 19

    Error: could not find function "guides"

  20. 20

    R Markdown could not find function

  21. 21

    R nls : could not find function "a"

  22. 22

    Unbound variable error when loading a procedure from an external file

  23. 23

    Getting error when loading image file from Parse

  24. 24

    Why won't this code display Error: Could not find file when e is thrown by a nonexistant file?

  25. 25

    Function not found in R doParallel 'foreach' - Error in { : task 1 failed - "could not find function "raster""

  26. 26

    Function not found in R doParallel 'foreach' - Error in { : task 1 failed - "could not find function "raster""

  27. 27

    foreach error "could not find function "%do%""

  28. 28

    Error: could not find function "read.DNAStringSet"

  29. 29

    Data table error could not find function "."

HotTag

Archive