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

foodweb() cannot differentiate identically-named functions and variables #2

Open
lewinfox opened this issue May 31, 2021 · 3 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@lewinfox
Copy link
Owner

If a variable in a function has the same name as another function, a link will be erroneously created.

f1 <- function() {
  1
}

f2 <- function() {
  f1 <- 10 # This variable `f1` will be confused with the function `f1()`
  2
}

# The foodweb mistakenly believes that function `f2()` calls function `f1()`
foodweb()
#> # A `foodweb`: 2 nodes and 1 edges
#> digraph 'foodweb' {
#>   f1()
#>   f2() -> { f1() }
#> }

We need to differentiate between the symbol f1 and the call f1().

@lewinfox lewinfox added bug Something isn't working help wanted Extra attention is needed labels May 31, 2021
lewinfox pushed a commit that referenced this issue May 31, 2021
To highlight the issue of name masking (issue #2)
@raneam
Copy link

raneam commented Jan 27, 2022

Hey, is there a minimal reproducible example that you have? I can have a look to see if I understand anything.

This is what I tried.

f1 <- function() {
  1
}

f2 <- function() {
  f1 <- 10 # This variable `f1` will be confused with the function `f1()`
  2
}

foodweb <- function() {
  f1()
  f2() -> f1
}
foodweb() # Runs okay
f1() # Still returns 1

@lewinfox
Copy link
Owner Author

Hi @raneam! Thanks for taking an interest.

I think there's some confusion - you don't want to override the foodweb() function like you've done there. foodweb()'s job is to map out the interdependencies of all the functions in the environment and give you a graph of how they relate to one another.

This may be clearer - plot(foodweb()) will draw the call graph for you:

f1 <- function() {
  1
}

f2 <- function() {
  f1 <- 10 # This variable `f1` will be confused with the function `f1()`
  2
}

# The foodweb mistakenly believes that function `f2()` calls function `f1()`
plot(foodweb())

The problem isn't with what the functions themselves return, it's a problem with the foodweb graph. If you run the example at the top here you see that within function f2() the algorithm sees a variable f1 and assumes this is the same as the function f1().

The graph output then shows that f2() calls f1() when in reality it doesn't.

The correct output of foodweb() for the example would be

#> # A `foodweb`: 2 nodes and 1 edges
#> digraph 'foodweb' {
#>   f1()
#>   f2()
#> }

i.e. two totally unconnected functions. My ASCII art skills are limited but plot(foodweb()) should give you two blobs with no connecting lines at all.

+------+    +------+
| f1() |    | f2() |
+------+    +------+

Does that make sense?

@telenskyt
Copy link

Also posted here: https://stackoverflow.com/q/78538718/684229

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants