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

Avoid a duplicate id error in tab_spanner_delim(). #1821

Merged
merged 5 commits into from
Aug 14, 2024

Conversation

olivroy
Copy link
Collaborator

@olivroy olivroy commented Aug 12, 2024

Summary

Avoid spanner ids collision by initializing the existing spanner ids first and then try to resolve the new spanner id

I get the result I expected with the following code

  df <- data.frame(
    pop_rural_young_num = c(1000, 2000),
    pop_rural_young_pct = c(0.33, 0.66),
    pop_rural_old_num = c(1000, 2000),
    pop_rural_old_pct = c(0.33, 0.66),
    pop_urban_young_num = c(1000, 2000),
    pop_urban_young_pct = c(0.33, 0.66),
    pop_urban_old_num = c(1000, 2000),
    pop_urban_old_pct = c(0.33, 0.66)
  )

 gt::gt(df) %>%
    tab_spanner_delim(
      delim = "_",
      contains("rural"),
      limit = 2
    ) %>%
    tab_spanner_delim(
      delim = "_",
      contains("urban"),
      limit = 2
    )

image

Previously, I would get this:

Error in `tab_spanner()`:
! The spanner `id` provided ("spanner-young_num") is
  not unique.
• The `id` must be unique across existing spanner and column
  IDs.
• Provide a unique ID value for this spanner.

Note that my code worked if I had only one of the tab_spanner_delim() calls.

Checklist

@olivroy
Copy link
Collaborator Author

olivroy commented Aug 13, 2024

@rich-iannone I think I found a better way to fix the issue by initializing existing spanner ids!

R/tab_create_modify.R Outdated Show resolved Hide resolved
Copy link
Member

@rich-iannone rich-iannone left a comment

Choose a reason for hiding this comment

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

LGTM!

@rich-iannone rich-iannone merged commit 31029d5 into rstudio:master Aug 14, 2024
12 checks passed
@olivroy olivroy deleted the tab-spanner-multi branch August 14, 2024 14:21
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