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

get_code to return concatenated code string and removed format_expression #176

Merged
merged 6 commits into from
Nov 29, 2023

Conversation

kartikeyakirar
Copy link
Contributor

@kartikeyakirar kartikeyakirar commented Nov 27, 2023

this fixes #173

In this pull request, I've made updates to both the get_code and replace_code methods. Now, when the deparse arg is set to TRUE, these methods will return a concatenated string. Specifically, the get_code method will return character(0) when used with new_qenv(), provided no code has been set. As for replace_code, it will first split the input string using "\n", then replace the last string in this sequence. Finally, it concatenates these strings again before replacing the code.

Removed format_expression function.

Example


q <- new_qenv()
get_code(q) # return character(0)
replace_code(q, "i <- iris")  # @code return character(0)

qq <- within(q, i <- iris)
qq <- within(qq, m <- mtcars)
get_code(qq) # return "i <- iris\nm <- mtcars"

replacement <- "i <- mtcars"
qr <- replace_code(qq, replacement)
get_code(qr) #return "i <- iris\ni <- mtcars"

Also review following modules for PR for changes.

@kartikeyakirar kartikeyakirar marked this pull request as draft November 27, 2023 11:48
Copy link
Contributor

@vedhav vedhav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just have one comment regarding the changes for this issue. Other than that, I think everything looks fine to me. It's good that this issue is not a breaking change, so I believe updating just the NEWS.md is sufficient.

Could you please change the target branch to main instead of refactor in this and all the related child PRs? I think this issue is independent of the refactor changes, and we can merge it before or after the refactor without any issues.

Comment on lines +47 to +52
replace_code(object, code = paste(lang2calls(code), collapse = "\n"))
})

#' @keywords internal
setMethod("replace_code", signature = c("qenv", "expression"), function(object, code) {
replace_code(object, code = format_expression(code))
replace_code(object, code = paste(lang2calls(code), collapse = "\n"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having second thoughts about removing format_expression. It's used in multiple places. I think it can stay.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a similar thought previously, but following a conversation with @chlebowa it was clarified that the function simply uses paste with a designated collapse parameter after processing with lang2calls. The format_expression function originally had a more complex design but it was simplified into a simple one-liner in a past refactoring phase. Currently, it's essentially redundant.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. We don't want to have too simple wrappers. Check out the use of lapply(x, [[, y) throughout teal.slice. That used to be a function but we decided to use base calls.

On top of that, soon we will be down to just a few uses of format_expression anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format_expression sounds like a wrapper for paste, where you overwrite default collapse param :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you guys consider having lang2calls return the paste(...)?

It seems that it is only used this way in the package (other than in tests)

ps. and rename it to lang2str or lang2character?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is recursion in lang2calls and implementing the paste function with collapse directly inside lang2calls can lead to unintended results due to concatenation of results from each recursive call.

Co-authored-by: Vedha Viyash <49812166+vedhav@users.noreply.github.com>
Signed-off-by: kartikeya kirar <kirar.kartikeya1@gmail.com>
@m7pr
Copy link
Contributor

m7pr commented Nov 28, 2023

@vedhav

Could you please change the target branch to main instead of refactor in this and all the related child PRs? I think this issue is independent of the refactor changes, and we can merge it before or after the refactor without any issues.

We also have get_code.teal_data that exists only on @refactor in teal.data so I would keep this aligned with @refactor branch.

@m7pr
Copy link
Contributor

m7pr commented Nov 28, 2023

@kartikeyakirar why it only shortens the length for deparse = TRUE? For deparse=FALSE it returns an expression, but it could return an expression of length 1 as well as it returns pasted code into length 1 right?

@kartikeyakirar
Copy link
Contributor Author

why it only shortens the length for deparse = TRUE? For deparse=FALSE it returns an expression, but it could return an expression of length 1 as well as it returns pasted code into length 1 right?

For deparse=FALSE it returns an expression not necessarily of length 1, The choice not to collapse the expressions into a single expression (even if possible) due to maintain integrity of the code. Collapsing everything into a single expression could alter the behavior or interpretation of the code when it's later evaluated.

Here expression is of length 3

q1 <- new_qenv(env = list2env(list(a = 1)), code = quote(a <- 1))
q2 <- eval_code(q1, code = quote(b <- a))
q3 <- eval_code(q2, code = quote(d <- 2))
length(get_code(q3)) # 1
length(get_code(q3, deparse = FALSE)) # 3

even if you collpase it before converting it into expression result is same

expression <- parse(text = get_code(q3), keep.source = TRUE)
length(expression) # 3


@vedhav vedhav self-requested a review November 28, 2023 09:09
@m7pr
Copy link
Contributor

m7pr commented Nov 28, 2023

@kartikeyakirar yeah, because you need to paste code with { and } to make it one call, hence an expression of length 1

> code <- c('a<-1', 'b<-2', 'c<-2')
> length(parse(text = code))
[1] 3
> length(parse(text = paste(code, collapse = '\n')))
[1] 3
> length(parse(text = paste(c('{', code, '}'), collapse = '\n')))
[1] 1

@kartikeyakirar
Copy link
Contributor Author

kartikeyakirar commented Nov 28, 2023

because you need to paste code with { and } to make it one call,

yes. It won't alter the interpretation of the code when it's later evaluated. Why didn't I think of this! I will update test and make change in teal as well. thanks

Copy link
Contributor

@vedhav vedhav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have any other comments, Good job in reflecting the change in all repos. LGTM

kartikeyakirar added a commit to insightsengineering/teal.osprey that referenced this pull request Nov 29, 2023
kartikeyakirar added a commit to insightsengineering/teal.goshawk that referenced this pull request Nov 29, 2023
kartikeyakirar added a commit to insightsengineering/teal.modules.clinical that referenced this pull request Nov 29, 2023
kartikeyakirar added a commit to insightsengineering/teal.modules.general that referenced this pull request Nov 29, 2023
kartikeyakirar added a commit to insightsengineering/teal.data that referenced this pull request Nov 29, 2023
…ression` (#206)

part of insightsengineering/teal.code#176

---------

Signed-off-by: kartikeya kirar <kirar.kartikeya1@gmail.com>
Co-authored-by: Vedha Viyash <49812166+vedhav@users.noreply.github.com>
Co-authored-by: vedhav <vedhaviyash4@gmail.com>
kartikeyakirar added a commit to insightsengineering/teal that referenced this pull request Nov 29, 2023
@kartikeyakirar kartikeyakirar merged commit 7ba7e80 into refactor Nov 29, 2023
@kartikeyakirar kartikeyakirar deleted the 173_fix_get_code@refactor@main branch November 29, 2023 07:34
averissimo added a commit to insightsengineering/teal that referenced this pull request Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants