insightsengineering / teal.data

Data model for teal applications
https://insightsengineering.github.io/teal.data/
Other
9 stars 8 forks source link

267 fix `@linksto` tag with `eval()` in last line of the evaluated code for `get_code` #268

Closed m7pr closed 9 months ago

m7pr commented 9 months ago

Close #267

Used @gogonzo solution from this PR to fix this https://github.com/insightsengineering/teal.data/pull/262/files#diff-66a61facc4f5ca86215d63af40e2645b364950439051784916316ef78d5073c8R102-R110

chlebowa commented 9 months ago
> data <- teal_data() |> eval_code(
+   code = '
+    expr <- quote(x <- x + 1)
+    x <- 0
+    eval(expr) # @linksto x
+  '
+ )
> get_code(data, datanames = "x") %>% cat
expr <- quote(x <- x + 1)
x <- 0
eval(expr)

That's better, thank you.

Is it possible to have eval(expr) be included automatically, like with assign and data?

m7pr commented 9 months ago

@chlebowa I'm not sure. We do extract library() automatically. For data() and assign() we only extended the detection so that it understands data() and assign() as ways of the assignment and they extract those lines if they refer to the object for which you are trying to get the code. For eval({}) if the object is inside {} then this is also tackled. But I would not automatically extract eval always, cause it sometimes can refer to different object than the one you are asking the code for.

chlebowa commented 9 months ago

Oh, I don't mean always.

> data <- teal_data() |> eval_code(
+   code = '
+   expr <- quote(x <- x + 1)
+   x <- 0
+   eval(expr) 
+ '
+ )
> get_code(data, datanames = "x") %>% cat
expr <- quote(x <- x + 1)
x <- 0

We know to return the line that creates expr but we don't know to return the line where it is evaluated.

m7pr commented 9 months ago

I think at some point this could be possible but I would like to avoid trying this out before the very close release. We can try and start an issue to think about it. And currently let's merge this one that fixes an issue where #@linksto x tag is in the last line which also contains eval

m7pr commented 9 months ago

Created an issue for that in here https://github.com/insightsengineering/teal.data/issues/270