Skip to content

A public way to redefine a database symbol or name after #449 #451

Closed

Description

@vorpalvorpal

Thanks for the quick and thorough rework in #449. The atomic install and the alias handling fix everything I reported in #448.

One thing it took away that I was relying on: redefining an id that the database already uses. #449 notes the change ("remove_unit() on a symbol/name from the database used to remove it; now it warns and does nothing"), and I can see why that is the safe default. But it was the only way to give a database id a different meaning for a session, and I can't find another public route now.

Example

In the database gal is a name of the galileo. In my field it always means the US gallon.

With units 1.0-1 (CRAN):

library(units)
remove_unit(name = "gal")
install_unit(name = "gal", def = "1 gallon")
set_units(set_units(1, "gal"), "L")
#> 3.785412 [L]

With main (1.0-1.7):

library(units)
remove_unit(name = "gal")
#> Warning: not installed by install_unit(), left unchanged: 'gal'
install_unit(name = "gal", def = "1 gallon")
#> Error: name "gal" already maps to existing but different unit
install_unit("gal", "1 gallon")   # as a symbol: accepted, but the name still wins
set_units(set_units(1, "gal"), "L")
#> Error: cannot convert gal into L

The same goes for any other database symbol or name a project wants to use differently.

Request

Would you consider a public way to override a database id on purpose? For example an argument such as install_unit(..., override = TRUE) (or remove_unit(..., force = TRUE)), recorded in the new registry so that remove_unit() puts the database's own mapping back afterwards. That would keep the new default, where nothing from the database is touched by accident, and make the deliberate case explicit and reversible.

If there is an intended route I have missed, a pointer would be just as welcome. load_units_xml() with an edited database works, but it replaces the whole unit system and drops everything installed so far, which is heavy for one id.

Happy to test a branch.

Activity

  1. Enchufa2 commented on Nov 13, 2026

    @Enchufa2
    Member

    I see. I'm not a fan of redefining the default units, but let me think about it.

  2. Enchufa2 commented on Nov 14, 2026

    @Enchufa2
    Member

    @vorpalvorpal, #452 adds an explicit way to do this: install_unit(..., override = TRUE). Could you give it a try?

    remotes::install_github("r-quantities/units@fix-install-shadowing")

    With your example:

    library(units)
    install_unit("gal", "1 gallon", override = TRUE)
    #> redefined symbol "gal" (was 0.01 meter·second⁻²)
    set_units(set_units(1, gal), L)
    #> 3.785412 [L]
    
    remove_unit("gal")                  # puts the database's own "gal" back
    set_units(set_units(1, gal), cm/s^2)
    #> 1 [cm/s^2]

    How it behaves:

    • The database mappings that give the id another meaning are set aside, and remove_unit() restores them. It works both as a symbol and as a name (install_unit(name = "gal", def = "1 gallon", override = TRUE)).
    • Prefixed forms follow the new meaning (e.g., kgal). Existing units objects that use the id are read with the new meaning too, so it's best done at the start of a session.
    • Two things are still refused, even with override = TRUE:
      • other ids a new name would shadow: names match regardless of case, so a name KG would also take over kg;
      • ids already installed with install_unit(): remove them first.

    While working on this I also found that install_unit() was silently redefining database units through the other kind of id or a prefix (e.g., a name "m", or a symbol "km"). #452 now refuses those unless you pass override = TRUE.

    It'd be great if you could try it on your real use cases, especially any edge cases you have in mind, for example:

    • several overrides in one session, or in one call;
    • ids that are a database unit's own symbol or name, not only aliases like gal;
    • removing and re-installing;
    • code that worked with the old remove_unit() + install_unit() idiom.

    Anything that's refused when you'd expect it to work, or the other way round, would be very useful to hear about.

  3. vorpalvorpal commented on Nov 14, 2026

    @vorpalvorpal
    ContributorAuthor

    Thanks, I tried the branch (80ad1b2) on the cases you listed and on my own. What works: several overrides in one session, database symbols and names as ids, install/remove cycles, redefining a unit in terms of its old meaning, and objects created earlier following the new meaning. The old remove_unit() + install_unit() idiom is refused with a clear pointer to override = TRUE.

    One bug. When the same id is overridden as both a symbol and a name, removing the symbol first (or both in one call) leaves the database's gal unreadable for the rest of the session:

    library(units)
    install_unit("gal", "1 gallon", name = "gal", override = TRUE)
    remove_unit("gal", name = "gal")
    ud_convert(1, "gal", "cm/s^2")
    #> Error: ut_get_converter(): NULL unit argument

    Removing the name first, or overriding only the symbol or only the name, restores correctly. The same happens with install_unit(c("gal","galn"), "1 gallon", name = c("gal","galname"), override = TRUE) followed by remove_unit(c("gal","galn"), c("gal","galname")).

    Cause, as far as I can tell: set_aside() sets aside both the database symbol and the database name for the id and files both under the symbol's entry, so the later name call finds nothing left to record. Removing the symbol first then tries to put the database name back while the new name still holds it (ut_map_name_to_unit() fails and the return value is ignored); removing the name afterwards unmaps the new name, and the database name is lost.

    #453 is a fix against fix-install-shadowing: after the set_aside() calls in ud_install(), each set-aside mapping is handed to the installed id of its own kind when there is one, so the name entry restores the name in any removal order. It adds a test for both removal orders, one call, the vector case and the single-kind cases; the suite passes with it here (858 passed).

    Two smaller observations, neither a defect in the restore logic. After a symbol-only override of gal, Gal and GAL are unreadable until removal, because they are only reached through the case-insensitive name that is set aside; that looks intended but may deserve a line in the docs. And remove_unit("gal", "gal") after a symbol-only override restores everything but warns "left unchanged" for the name half, which is harmless but a little misleading.

  4. Enchufa2 commented on Nov 16, 2026

    @Enchufa2
    Member

    Take another go, please, and I'll merge it if everything looks good.

  5. Enchufa2 commented on Nov 18, 2026

    @Enchufa2
    Member

    Merging, thanks again. Please let us know if you find anything else.

  6. vorpalvorpal commented on Nov 25, 2026

    @vorpalvorpal
    ContributorAuthor

    Thanks for merging #452, and for folding in the fix from #453.

    I've tested install_unit(..., override = TRUE) against main (d01bffe). Redefining gal, mph, nmi, pH, Ma and ka and removing them again all works. I also re-ran the cases from #448, #451 and #453. Refused installs leave nothing mapped, and degree and ohm keep their symbols. The database gal comes back in every removal order, including when the symbol and the name are overridden in separate calls.

    One small thing I noticed: an alias of a unit that has no symbol becomes that unit's symbol while it is installed, so the original prints differently:

    library(units)
    install_unit("zzgallon", "1 gallon")
    set_units(1, "gallon")
    #> 1 [zzgallon]
    remove_unit("zzgallon")
    set_units(1, "gallon")
    #> 1 [gallon]

    A unit that has a symbol (degree, s) keeps it. If this is intended, a line in install_unit()'s docs might help.

  7. Enchufa2 commented on Nov 25, 2026

    @Enchufa2
    Member

    Thanks. This is not new: gallon is a name, there is no symbol. If you install a symbol, the symbol wins unless you set the option auto_convert_names_to_symbols=FALSE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions