Skip to content

Refactor draw_legend()#463

Merged
vincentarelbundock merged 7 commits intograntmcdermott:mainfrom
vincentarelbundock:tinyplot_main_refactor
Sep 9, 2025
Merged

Refactor draw_legend()#463
vincentarelbundock merged 7 commits intograntmcdermott:mainfrom
vincentarelbundock:tinyplot_main_refactor

Conversation

@vincentarelbundock
Copy link
Copy Markdown
Collaborator

The purpose of this PR is to simplify the logic of draw_legends() somewhat. The main strategy is to have a cleaner separate of concerns, with a cleaner difference between functions that are called to compute coordinates/settings and functions that are called to create side-effects.

I want to do this in a few more ways and think it will open up big long term benefits, perhaps in the same vein as the type_*() refactor.

Copy link
Copy Markdown
Owner

@grantmcdermott grantmcdermott left a comment

Choose a reason for hiding this comment

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

I'm a little nervous to split the legend functionality into a serious of nested functions, given how hard tricky it can be to debug and troubleshoot already. But these simple parameter assignments look fine and you do have a track record of effective modularization so I'm willing to take it on faith ;-)

Minor nits remaining.

Comment thread R/utils.R Outdated
}


swap_variables <- function(env, ...) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Nit: = instead of '<-` please.

(I should add a CLAUDE.md to catch these nits at some point...)

Comment thread R/utils.R Outdated
Comment on lines +54 to +67
pairs <- list(...)
for (p in pairs) {
tmp <- get(p[1], envir = env)
assign(p[1], get(p[2], envir = env), envir = env)
assign(p[2], tmp, envir = env)
}
}

swap_columns <- function(dp, a, b) {
va <- dp[[a]]
vb <- dp[[b]]
dp[[a]] <- if (!is.null(vb)) vb else NULL
dp[[b]] <- if (!is.null(va)) va else NULL
dp
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Ditto

Comment thread R/tinyplot.R Outdated
Comment on lines +867 to +870
if (!is.null(log)) log <- chartr("xy", "yx", log)
datapoints <- swap_columns(datapoints, "x", "y")
datapoints <- swap_columns(datapoints, "xmin", "ymin")
datapoints <- swap_columns(datapoints, "xmax", "ymax")
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

=

@grantmcdermott grantmcdermott changed the title Refactor draw_legends() Refactor draw_legend() Sep 9, 2025
@vincentarelbundock
Copy link
Copy Markdown
Collaborator Author

I'm a little nervous to split the legend functionality into a serious of nested functions, given how hard tricky it can be to debug and troubleshoot already.

Oh yeah, totally.

Honestly, I don't think I would have gotten as good of an appreciation for your point before trying to make that PR work. The execution order with plot.new() and friends is SUPER tricky, especially with hooks, etc.

My hope/expectation is that changes like this actually make things easier to navigate, because it'll be easier for us to see when/where there are side effects.

Minor nits remaining.

Should be done.

@grantmcdermott
Copy link
Copy Markdown
Owner

Out atm but go for it if you're happy and tests are passing.

@vincentarelbundock vincentarelbundock merged commit 518b5d6 into grantmcdermott:main Sep 9, 2025
3 checks passed
@vincentarelbundock vincentarelbundock deleted the tinyplot_main_refactor branch September 9, 2025 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants