While I know I can break this out into multiple steps and solve this with a base R approach, I'm curious if there's a tidyevel/tidyselect pattern to allow renaming in a nested manner. Let's say I have this object:
x <- list(
rename_me = "foo",
leave_me = "bar",
rename_me_too = tibble::tibble(
rename_me = 1:3,
leave_me = 3:1
)
)
And I want to rename three elements: x$rename_me
, x$rename_me_too
, and the nested element x$rename_me_too$rename_me
.
Using tidyselect:::rename
, I can operate on the first two quite easily:
tidyselect:::rename(x, RenameMe = rename_me, RenameMeToo = rename_me_too)
... yielding:
$RenameMe
[1] 1 2 3
$leave_me
[1] 3 2 1
$RenameMeToo
# A tibble: 3 x 2
rename_me leave_me
<int> <int>
1 11 13
2 12 12
3 13 11
Great!
But the tidyeval semantics get tripped up if I try to 'nest' a rename command to handle the dataframe's rename_me
column . None of these work, e.g.:
## data masking to access rename_me_too doesn't work:
tidyselect:::rename(x, RenameMe = rename_me, RenameMeToo = tidyselect:::rename(rename_me_too, RenameMe = rename_me))
## neither does trying to reference rename_me_too with the dot (.):
tidyselect:::rename(x, RenameMe = rename_me, RenameMeToo = tidyselect:::rename(.$rename_me_too, RenameMe = rename_me))
## nor does referencing x$rename_me_too directly:
tidyselect:::rename(x, RenameMe = rename_me, RenameMeToo = tidyselect:::rename(x$rename_me_too, RenameMe = rename_me))
... there are a few problems here, but the largest one is that the RenameMeToo = something
argument is being defused by tidyselect:::rename
and I'm not sure how (if possible at all) to cleanly combine rlang's defusing of the RenameMe
arg with the desired referential transparency of the RenameMeToo
arg.
Does anyone here know of a general tidyselect pattern that would allow for syntax and semantics similar to the above?
(And again yes, I know I can break this into a few separate statements ... this is largely an exploration of what is doable, not necessarily what should be done )