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

[Bug]: Iterator of the for loop is treated as an object in get_code_dependency #237

Open
m7pr opened this issue Nov 27, 2024 · 2 comments
Open
Labels
bug Something isn't working core

Comments

@m7pr
Copy link
Contributor

m7pr commented Nov 27, 2024

If object is treated as an iterator in a for loop, like x below, then get_code treats this case as this object created in this for loop, hence even though it's a temporary object (just for the time of the for loop execution) it gets extracted in the get_code

library(teal.code)
q <- within(qenv(), {
  a <- 1
  b <- 2
}) |> within({
  for (x in c(1, 2)) {
    b <- a
    b <- b + a + 1
    b + 3 -> b
  }
}) |> within({
  x <- 5
})

get_code(q, names = 'x') |> cat()
#> a <- 1
#> b <- 2
#> for (x in c(1, 2)) {
#>     b <- a
#>     b <- b + a + 1
#>     b <- b + 3
#> }
#> x <- 5

Created on 2024-11-27 with reprex v2.1.1

@m7pr m7pr added bug Something isn't working core labels Nov 27, 2024
@llrs-roche
Copy link
Contributor

llrs-roche commented Dec 3, 2024

Perhaps it is more illustrative the following example, where x is "exported" even if it is not defined inside the loop:

library(teal.code)
q <- within(qenv(), {
  a <- 1
  b <- 2
}) |> within({
  for (x in c(1, 2)) {
    b <- a
    b <- b + a + 1
    b + 3 -> b
  }
})

q
#> <environment: 0x000002a89a708dc0> [L]
#> Parent: <environment: package:teal.code>
#> Bindings:
#> • x: <dbl> [L]
#> • a: <dbl> [L]
#> • b: <dbl> [L]

# Normal R code
ls()
#> [1] "q"
a <- 1
b <- 2
for (x in c(1, 2)) {
  b <- a
  b <- b + a + 1
  b + 3 -> b
}
ls()
#> [1] "a" "b" "q" "x"

Created on 2024-12-03 with reprex v2.1.1

I don't think this is a bug. In normal R code x appears as an object even if it is only defined as a variable to iterate with.

@m7pr
Copy link
Contributor Author

m7pr commented Dec 18, 2024

Thanks @llrs-roche for the reply, I need to rethink if we should return the code for x or not

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

No branches or pull requests

2 participants