get_code to return concatenated code string and removed format_expression - #176
Conversation
get_code to return concatenated code sting.get_code to return concatenated code string and removed format_expression
vedhav
left a comment
There was a problem hiding this comment.
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.
| 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")) |
There was a problem hiding this comment.
I'm having second thoughts about removing format_expression. It's used in multiple places. I think it can stay.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
format_expression sounds like a wrapper for paste, where you overwrite default collapse param :)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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>
We also have |
|
@kartikeyakirar why it only shortens the length for |
For deparse= Here expression is of length 3 even if you collpase it before converting it into expression result is same |
|
@kartikeyakirar yeah, because you need to paste > 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 |
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 |
vedhav
left a comment
There was a problem hiding this comment.
I don't have any other comments, Good job in reflecting the change in all repos. LGTM
…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>
this fixes #173
In this pull request, I've made updates to both the
get_codeandreplace_codemethods. Now, when thedeparsearg is set to TRUE, these methods will return a concatenated string. Specifically, theget_codemethod will return character(0) when used withnew_qenv(),provided no code has been set. As forreplace_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_expressionfunction.Example
Also review following modules for PR for changes.
get_codeto return concatenated code string teal#976get_codeto return concatenated code string and removedformat_expressionteal.data#206get_codeto return concatenated code string teal.modules.general#615get_codeto return concatenated code string teal.modules.clinical#898get_codeto return concatenated code string teal.goshawk#250get_codeto return concatenated code string teal.osprey#244